Class: Foruiman::TUI::Theme

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

Overview

Use the terminal's palette and default background, so its theme owns the actual colors (and can change them while Foruiman is running).

Constant Summary collapse

COLORS =
{
  text: 39, muted: 90, faint: 90, border: 90, accent: 36,
  green: 32, red: 31, amber: 33, cyan: 36, blue: 34, mauve: 35
}.freeze
PROCESS_COLORS =
%i[cyan amber mauve green blue red].freeze
STATUS_COLORS =
{
  pending: :faint, running: :green, restarting: :amber, stopping: :amber,
  stopped: :muted, exited: :muted, failed: :red
}.freeze
STATUS_MARKS =
{
  pending: "○", running: "▶", restarting: "↻", stopping: "◼",
  stopped: "■", exited: "✓", failed: "✕"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: ENV) ⇒ Theme

Returns a new instance of Theme.



25
26
27
# File 'lib/foruiman/tui/theme.rb', line 25

def initialize(env: ENV)
  @enabled = env.fetch("NO_COLOR", "").empty? && env["TERM"] != "dumb"
end

Instance Attribute Details

#enabled ⇒ Object (readonly)

Returns the value of attribute enabled.



23
24
25
# File 'lib/foruiman/tui/theme.rb', line 23

def enabled
  @enabled
end

Instance Method Details

#base ⇒ Object



29
30
31
# File 'lib/foruiman/tui/theme.rb', line 29

def base
  enabled ? "\e[39m\e[49m" : ""
end

#log(text, color: :text) ⇒ Object

A child reset restores the UI's defaults. Explicit child colors, including background colors, still win; they cannot bleed into borders or later rows.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/foruiman/tui/theme.rb', line 51

def log(text, color: :text)
  return text.gsub(Foruiman::ANSI::SGR, "") unless enabled

  defaults = "#{code(color)}\e[49m"
  styles = Foruiman::ANSI::Styles.new
  content = text.gsub(Foruiman::ANSI::SGR) do |sequence|
    styles.apply(sequence)
    Foruiman::ANSI::RESET + defaults + styles.prefix
  end
  defaults + content + reset
end

#paint(text, color = :text, selected: false, bold: false) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/foruiman/tui/theme.rb', line 33

def paint(text, color = :text, selected: false, bold: false)
  emphasis = "#{"\e[7m" if selected}#{"\e[1m" if bold}"
  return emphasis.empty? ? text : "#{emphasis}#{text}#{reset}" unless enabled

  defaults = selected ? base : "#{code(color)}\e[49m"
  "#{defaults}#{emphasis}#{text}#{reset}"
end

#process(index) ⇒ Object



45
46
47
# File 'lib/foruiman/tui/theme.rb', line 45

def process(index)
  PROCESS_COLORS[index % PROCESS_COLORS.size]
end

#reset ⇒ Object



41
42
43
# File 'lib/foruiman/tui/theme.rb', line 41

def reset
  enabled ? Foruiman::ANSI::RESET + base : Foruiman::ANSI::RESET
end