Class: TuiTui::Theme
- Inherits:
-
Data
- Object
- Data
- TuiTui::Theme
- Defined in:
- lib/tui_tui/theme.rb,
lib/tui_tui/theme.rb,
lib/tui_tui/theme.rb
Overview
Semantic Style roles shared by the built-in widgets. Themes combine a background surface with an accent hue.
Constant Summary collapse
- SURFACES =
Background-dependent neutral roles.
{ dark: { text: Style.new, muted: Style.new(fg: 245), bar: Style.new(fg: 252, bg: 238), selection_dim: Style.new(fg: 247, bg: 238), success: Style.new(fg: 71), warning: Style.new(fg: 179), danger: Style.new(fg: 167), info: Style.new(fg: 110) }, light: { text: Style.new, muted: Style.new(fg: 240), bar: Style.new(fg: 16, bg: 252), selection_dim: Style.new(fg: 240, bg: 252), success: Style.new(fg: 28), warning: Style.new(fg: 130), danger: Style.new(fg: 124), info: Style.new(fg: 25) } }.freeze
- ACCENTS =
Accent roles per hue and background.
{ cool: { dark: {line: 66, title: 109, accent: 73, sel: [231, 60]}, light: {line: 30, title: 25, accent: 30, sel: [16, 152]} }, warm: { dark: {line: 95, title: 137, accent: 173, sel: [231, 95]}, light: {line: 95, title: 94, accent: 130, sel: [16, 180]} }, mono: { dark: {line: 240, title: 252, accent: 252, sel: [16, 250]}, light: {line: 240, title: 236, accent: 236, sel: [16, 250]} } }.freeze
- LIGHT_FGBG =
COLORFGBG background values meaning “light”.
%w[7 15].freeze
- TABLE =
Shared palettes for every surface/hue pair.
SURFACES.keys.product(ACCENTS.keys).to_h { |bg, hue| [[bg, hue], compose(bg, hue)] }.freeze
- DARK =
Theme.build(background: :dark, hue: :cool)
- LIGHT =
Theme.build(background: :light, hue: :cool)
- DEFAULT =
Theme::DARK
- WARM =
Theme.build(background: :dark, hue: :warm)
- MONO =
Theme.build(background: :dark, hue: :mono)
- PRESETS =
{ default: DEFAULT, cool: DEFAULT, dark: DARK, light: LIGHT, warm: WARM, mono: MONO }.freeze
Instance Attribute Summary collapse
-
#accent ⇒ Object
readonly
Returns the value of attribute accent.
-
#bar ⇒ Object
readonly
Returns the value of attribute bar.
-
#cursor ⇒ Object
readonly
Returns the value of attribute cursor.
-
#danger ⇒ Object
readonly
Returns the value of attribute danger.
-
#frame ⇒ Object
readonly
Returns the value of attribute frame.
-
#info ⇒ Object
readonly
Returns the value of attribute info.
-
#muted ⇒ Object
readonly
Returns the value of attribute muted.
-
#scroll_thumb ⇒ Object
readonly
Returns the value of attribute scroll_thumb.
-
#scroll_track ⇒ Object
readonly
Returns the value of attribute scroll_track.
-
#selection ⇒ Object
readonly
Returns the value of attribute selection.
-
#selection_dim ⇒ Object
readonly
Returns the value of attribute selection_dim.
-
#success ⇒ Object
readonly
Returns the value of attribute success.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#warning ⇒ Object
readonly
Returns the value of attribute warning.
Class Method Summary collapse
-
.auto(hue: :cool, env: ENV) ⇒ Object
The hue palette tuned for the detected background.
- .bold(fg) ⇒ Object
- .build(background: :dark, hue: :cool) ⇒ Object
-
.compose(background, hue) ⇒ Object
Build the palette for a (background, hue) pair.
-
.detect_background(env: ENV) ⇒ Object
Best-effort terminal background (:light/:dark).
-
.named(name) ⇒ Object
Fetch a preset by name (Symbol/String); unknown names fall back to DEFAULT.
Instance Method Summary collapse
-
#status(kind) ⇒ Object
Map a symbolic status to its semantic role Style (with common aliases).
Instance Attribute Details
#accent ⇒ Object (readonly)
Returns the value of attribute accent
8 9 10 |
# File 'lib/tui_tui/theme.rb', line 8 def accent @accent end |
#bar ⇒ Object (readonly)
Returns the value of attribute bar
8 9 10 |
# File 'lib/tui_tui/theme.rb', line 8 def @bar end |
#cursor ⇒ Object (readonly)
Returns the value of attribute cursor
8 9 10 |
# File 'lib/tui_tui/theme.rb', line 8 def cursor @cursor end |
#danger ⇒ Object (readonly)
Returns the value of attribute danger
8 9 10 |
# File 'lib/tui_tui/theme.rb', line 8 def danger @danger end |
#frame ⇒ Object (readonly)
Returns the value of attribute frame
8 9 10 |
# File 'lib/tui_tui/theme.rb', line 8 def frame @frame end |
#info ⇒ Object (readonly)
Returns the value of attribute info
8 9 10 |
# File 'lib/tui_tui/theme.rb', line 8 def info @info end |
#muted ⇒ Object (readonly)
Returns the value of attribute muted
8 9 10 |
# File 'lib/tui_tui/theme.rb', line 8 def muted @muted end |
#scroll_thumb ⇒ Object (readonly)
Returns the value of attribute scroll_thumb
8 9 10 |
# File 'lib/tui_tui/theme.rb', line 8 def scroll_thumb @scroll_thumb end |
#scroll_track ⇒ Object (readonly)
Returns the value of attribute scroll_track
8 9 10 |
# File 'lib/tui_tui/theme.rb', line 8 def scroll_track @scroll_track end |
#selection ⇒ Object (readonly)
Returns the value of attribute selection
8 9 10 |
# File 'lib/tui_tui/theme.rb', line 8 def selection @selection end |
#selection_dim ⇒ Object (readonly)
Returns the value of attribute selection_dim
8 9 10 |
# File 'lib/tui_tui/theme.rb', line 8 def selection_dim @selection_dim end |
#success ⇒ Object (readonly)
Returns the value of attribute success
8 9 10 |
# File 'lib/tui_tui/theme.rb', line 8 def success @success end |
#text ⇒ Object (readonly)
Returns the value of attribute text
8 9 10 |
# File 'lib/tui_tui/theme.rb', line 8 def text @text end |
#title ⇒ Object (readonly)
Returns the value of attribute title
8 9 10 |
# File 'lib/tui_tui/theme.rb', line 8 def title @title end |
#warning ⇒ Object (readonly)
Returns the value of attribute warning
8 9 10 |
# File 'lib/tui_tui/theme.rb', line 8 def warning @warning end |
Class Method Details
.auto(hue: :cool, env: ENV) ⇒ Object
The hue palette tuned for the detected background.
133 |
# File 'lib/tui_tui/theme.rb', line 133 def self.auto(hue: :cool, env: ENV) = build(background: detect_background(env: env), hue: hue) |
.bold(fg) ⇒ Object
88 |
# File 'lib/tui_tui/theme.rb', line 88 def self.bold(fg) = Style.new(fg: fg, attrs: [:bold]) |
.build(background: :dark, hue: :cool) ⇒ Object
117 |
# File 'lib/tui_tui/theme.rb', line 117 def self.build(background: :dark, hue: :cool) = TABLE.fetch([background, hue]) |
.compose(background, hue) ⇒ Object
Build the palette for a (background, hue) pair.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/tui_tui/theme.rb', line 91 def self.compose(background, hue) surface = SURFACES.fetch(background) a = ACCENTS.fetch(hue).fetch(background) selection = Style.new(fg: a[:sel][0], bg: a[:sel][1]) new( frame: Style.new(fg: a[:line]), title: bold(a[:title]), text: surface[:text], muted: surface[:muted], accent: bold(a[:accent]), selection: selection, selection_dim: surface[:selection_dim], bar: surface[:bar], cursor: selection, scroll_track: Style.new(fg: a[:line]), scroll_thumb: Style.new(bg: a[:sel][1]), success: surface[:success], warning: surface[:warning], danger: surface[:danger], info: surface[:info] ) end |
.detect_background(env: ENV) ⇒ Object
Best-effort terminal background (:light/:dark).
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/tui_tui/theme.rb', line 120 def self.detect_background(env: ENV) case env["TUITUI_BACKGROUND"]&.downcase when "light" :light when "dark" :dark else bg = env["COLORFGBG"]&.split(";")&.last bg && LIGHT_FGBG.include?(bg) ? :light : :dark end end |
Instance Method Details
#status(kind) ⇒ Object
Map a symbolic status to its semantic role Style (with common aliases).
35 36 37 38 39 40 41 42 43 |
# File 'lib/tui_tui/theme.rb', line 35 def status(kind) case kind when :ok, :success then success when :warn, :warning then warning when :error, :danger then danger when :info then info else text end end |