Class: Brut::TUI::TerminalTheme
- Inherits:
-
Object
- Object
- Brut::TUI::TerminalTheme
show all
- Defined in:
- lib/brut/tui/terminal_theme.rb
Overview
For subclass implementors, there are various private
methods that do encapsulate the ANSI escape codes, and these
can be used or overridden.
This particular implementation avoids the use of black or white, and uses
the normal ANSI colors. This should, in theory, work with any terminal
theme where all colors are legible on the chosen background.
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of TerminalTheme.
37
38
39
|
# File 'lib/brut/tui/terminal_theme.rb', line 37
def initialize(terminal)
@terminal = terminal
end
|
Class Method Details
.based_on_background(terminal) ⇒ Object
22
23
24
25
26
27
28
|
# File 'lib/brut/tui/terminal_theme.rb', line 22
def self.based_on_background(terminal)
if dark_background?(terminal)
Brut::TUI::Themes::Dark.new(terminal)
else
Brut::TUI::Themes::Light.new(terminal)
end
end
|
.dark_background?(terminal) ⇒ Boolean
30
31
32
33
34
35
|
# File 'lib/brut/tui/terminal_theme.rb', line 30
def self.dark_background?(terminal)
r, g, b = terminal.background_color.map { it / 255.0 }
luminance = (0.21 * r) + (0.72 * g) + (0.07 * b)
return luminance < 0.5
end
|
Instance Method Details
#bold ⇒ Object
90
|
# File 'lib/brut/tui/terminal_theme.rb', line 90
def bold = esc("1")
|
#bold_off ⇒ Object
91
|
# File 'lib/brut/tui/terminal_theme.rb', line 91
def bold_off = normal
|
#bright ⇒ Object
93
|
# File 'lib/brut/tui/terminal_theme.rb', line 93
def bright = esc("1")
|
#bright_off ⇒ Object
94
|
# File 'lib/brut/tui/terminal_theme.rb', line 94
def bright_off = normal
|
#code ⇒ Object
105
|
# File 'lib/brut/tui/terminal_theme.rb', line 105
def code = underline
|
#code_off ⇒ Object
106
|
# File 'lib/brut/tui/terminal_theme.rb', line 106
def code_off = underline_off
|
#error ⇒ Object
87
|
# File 'lib/brut/tui/terminal_theme.rb', line 87
def error = bold + bright_red
|
#heading ⇒ Object
112
|
# File 'lib/brut/tui/terminal_theme.rb', line 112
def heading = bold + underline + bright_blue
|
#italic ⇒ Object
96
|
# File 'lib/brut/tui/terminal_theme.rb', line 96
def italic = esc("3")
|
#italic_off ⇒ Object
97
|
# File 'lib/brut/tui/terminal_theme.rb', line 97
def italic_off = esc("23")
|
#normal ⇒ Object
108
|
# File 'lib/brut/tui/terminal_theme.rb', line 108
def normal = esc("22")
|
#reset ⇒ Object
110
|
# File 'lib/brut/tui/terminal_theme.rb', line 110
def reset = esc("0")
|
#strike ⇒ Object
99
|
# File 'lib/brut/tui/terminal_theme.rb', line 99
def strike = esc("9")
|
#strike_off ⇒ Object
100
|
# File 'lib/brut/tui/terminal_theme.rb', line 100
def strike_off = esc("29")
|
#success ⇒ Object
86
|
# File 'lib/brut/tui/terminal_theme.rb', line 86
def success = bold + bright_green
|
#warning ⇒ Object
88
|
# File 'lib/brut/tui/terminal_theme.rb', line 88
def warning = bold + yellow
|
#weak ⇒ Object
102
|
# File 'lib/brut/tui/terminal_theme.rb', line 102
def weak = esc("2")
|
#weak_off ⇒ Object
103
|
# File 'lib/brut/tui/terminal_theme.rb', line 103
def weak_off = normal
|
#with_markup(string, text: :normal, reset: true) ⇒ Object
Returns a string with its markup turned into escape codes.
Note that due to the way ANSI escape codes work, the state
of the terminal may not be in the same state you found it.
This method tries to turn features on and off (e.g. after a bold
string, the codes to turn off bold are applied), but if a subclass
mixes colors and styles, any text output after this one may
not look like the text before it.
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/brut/tui/terminal_theme.rb', line 49
def with_markup(string, text: :normal, reset: true)
result = +""
result << send(text)
Brut::TUI::MarkupString.from_string(string).parse do |directive, value|
case directive
in :start
case value
in :bold
result << bold
in :strike
result << strike
in :code
result << code
in :weak
result << weak
end
in :stop
case value
in :bold
result << bold_off << send(text)
in :strike
result << strike_off << send(text)
in :code
result << code_off << send(text)
in :weak
result << weak_off << send(text)
end
in :text
result << value
end
end
if reset
result << self.reset
end
result
end
|