Class: RBWatch::Theme

Inherits:
Object
  • Object
show all
Defined in:
lib/rbwatch/theme.rb

Defined Under Namespace

Classes: Definition, UnknownThemeError

Constant Summary collapse

DEFINITIONS =
{
  ruby: Definition.new(
    banner_border: :thick,
    status_border: :light,
    restart_border: :thick,
    border_color: :red,
    accent_color: :red,
    info_color: :cyan,
    success_color: :green,
    warning_color: :yellow,
    error_color: :red,
    restart_color: :magenta,
    muted_color: :bright_black
  ),
  neon: Definition.new(
    banner_border: :thick,
    status_border: :light,
    restart_border: :thick,
    border_color: :cyan,
    accent_color: :magenta,
    info_color: :cyan,
    success_color: :green,
    warning_color: :yellow,
    error_color: :red,
    restart_color: :magenta,
    muted_color: :bright_black
  ),
  minimal: Definition.new(
    banner_border: :ascii,
    status_border: :ascii,
    restart_border: :ascii,
    border_color: :bright_black,
    accent_color: :white,
    info_color: :white,
    success_color: :white,
    warning_color: :white,
    error_color: :white,
    restart_color: :white,
    muted_color: :bright_black
  ),
  matrix: Definition.new(
    banner_border: :light,
    status_border: :light,
    restart_border: :thick,
    border_color: :green,
    accent_color: :green,
    info_color: :green,
    success_color: :green,
    warning_color: :yellow,
    error_color: :red,
    restart_color: :green,
    muted_color: :bright_black
  )
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :ruby, color: true) ⇒ Theme

Returns a new instance of Theme.



116
117
118
119
120
121
# File 'lib/rbwatch/theme.rb', line 116

def initialize(name = :ruby, color: true)
  @name = self.class.normalize_name(name) || :ruby
  @definition = self.class::DEFINITIONS.fetch(@name, self.class::DEFINITIONS.fetch(:ruby))
  @color = color
  @pastel = Pastel.new(enabled: color)
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



114
115
116
# File 'lib/rbwatch/theme.rb', line 114

def definition
  @definition
end

#nameObject (readonly)

Returns the value of attribute name.



114
115
116
# File 'lib/rbwatch/theme.rb', line 114

def name
  @name
end

Class Method Details

.availableObject



83
84
85
# File 'lib/rbwatch/theme.rb', line 83

def available
  DEFINITIONS.keys
end

.default(color: true) ⇒ Object



87
88
89
# File 'lib/rbwatch/theme.rb', line 87

def default(color: true)
  new(:ruby, color: color)
end

.normalize_name(name) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/rbwatch/theme.rb', line 91

def normalize_name(name)
  return nil if name.nil?

  value = name.to_s.strip.downcase
  return nil if value.empty?

  value.to_sym
end

.resolve(name, color: true, interactive: false, input: $stdin, output: $stderr) ⇒ Object

Raises:



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rbwatch/theme.rb', line 100

def resolve(name, color: true, interactive: false, input: $stdin, output: $stderr)
  key = normalize_name(name)
  return default(color: color) if key.nil?
  return new(key, color: color) if DEFINITIONS.key?(key)

  if interactive && input.tty? && output.tty?
    choice = TTY::Prompt.new.select("Choose a theme", available.map(&:to_s), default: "ruby")
    return new(choice.to_sym, color: color)
  end

  raise UnknownThemeError, "Unknown theme `#{name}`. Available themes: #{available.map(&:to_s).join(', ')}"
end

Instance Method Details

#accent(text) ⇒ Object



143
144
145
# File 'lib/rbwatch/theme.rb', line 143

def accent(text)
  paint(text, definition.accent_color)
end

#border(kind) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/rbwatch/theme.rb', line 127

def border(kind)
  case kind
  when :banner then definition.banner_border
  when :restart then definition.restart_border
  else definition.status_border
  end
end

#box_options(kind = :status) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/rbwatch/theme.rb', line 135

def box_options(kind = :status)
  {
    border: border(kind),
    enable_color: color?,
    style: { border: { fg: definition.border_color } }
  }
end

#color?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/rbwatch/theme.rb', line 123

def color?
  @color
end

#command(text) ⇒ Object



197
198
199
# File 'lib/rbwatch/theme.rb', line 197

def command(text)
  info(text)
end

#delay(text) ⇒ Object



205
206
207
# File 'lib/rbwatch/theme.rb', line 205

def delay(text)
  accent(text)
end

#error(text) ⇒ Object



159
160
161
# File 'lib/rbwatch/theme.rb', line 159

def error(text)
  paint(text, definition.error_color)
end

#files(text) ⇒ Object



201
202
203
# File 'lib/rbwatch/theme.rb', line 201

def files(text)
  accent(text)
end

#info(text) ⇒ Object



147
148
149
# File 'lib/rbwatch/theme.rb', line 147

def info(text)
  paint(text, definition.info_color)
end

#label(text) ⇒ Object



171
172
173
# File 'lib/rbwatch/theme.rb', line 171

def label(text)
  muted(text)
end

#muted(text) ⇒ Object



167
168
169
# File 'lib/rbwatch/theme.rb', line 167

def muted(text)
  paint(text, definition.muted_color)
end

#paint(text, color) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/rbwatch/theme.rb', line 213

def paint(text, color)
  return text.to_s unless color?

  pastel.public_send(color, text.to_s)
rescue NoMethodError
  text.to_s
end

#pid(text) ⇒ Object



209
210
211
# File 'lib/rbwatch/theme.rb', line 209

def pid(text)
  accent(text)
end

#prefix(level = :info) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rbwatch/theme.rb', line 175

def prefix(level = :info)
  text = "[RBWatch]"
  return text unless color?

  color = case level
          when :success then :green
          when :warning then :yellow
          when :error then :red
          when :restart then :magenta
          else :cyan
          end
  text.colorize(color)
end

#restart(text) ⇒ Object



163
164
165
# File 'lib/rbwatch/theme.rb', line 163

def restart(text)
  paint(text, definition.restart_color)
end

#status(text) ⇒ Object



193
194
195
# File 'lib/rbwatch/theme.rb', line 193

def status(text)
  success(text)
end

#success(text) ⇒ Object



151
152
153
# File 'lib/rbwatch/theme.rb', line 151

def success(text)
  paint(text, definition.success_color)
end

#timestamp(time = Time.now) ⇒ Object



189
190
191
# File 'lib/rbwatch/theme.rb', line 189

def timestamp(time = Time.now)
  muted("[#{time.strftime('%H:%M:%S')}]")
end

#warning(text) ⇒ Object



155
156
157
# File 'lib/rbwatch/theme.rb', line 155

def warning(text)
  paint(text, definition.warning_color)
end