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
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
else
Brut::TUI::Themes::Light.new
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
94
|
# File 'lib/brut/tui/terminal_theme.rb', line 94
def bold = esc("1")
|
#bold_off ⇒ Object
95
|
# File 'lib/brut/tui/terminal_theme.rb', line 95
def bold_off = normal
|
#bright ⇒ Object
97
|
# File 'lib/brut/tui/terminal_theme.rb', line 97
def bright = esc("1")
|
#bright_off ⇒ Object
98
|
# File 'lib/brut/tui/terminal_theme.rb', line 98
def bright_off = normal
|
#code ⇒ Object
109
|
# File 'lib/brut/tui/terminal_theme.rb', line 109
def code = underline
|
#code_off ⇒ Object
110
|
# File 'lib/brut/tui/terminal_theme.rb', line 110
def code_off = underline_off
|
#error ⇒ Object
91
|
# File 'lib/brut/tui/terminal_theme.rb', line 91
def error = bold + bright_red
|
#heading ⇒ Object
116
|
# File 'lib/brut/tui/terminal_theme.rb', line 116
def heading = bold + underline + bright_blue
|
#italic ⇒ Object
100
|
# File 'lib/brut/tui/terminal_theme.rb', line 100
def italic = esc("3")
|
#italic_off ⇒ Object
101
|
# File 'lib/brut/tui/terminal_theme.rb', line 101
def italic_off = esc("23")
|
#normal ⇒ Object
112
|
# File 'lib/brut/tui/terminal_theme.rb', line 112
def normal = esc("22")
|
#reset ⇒ Object
114
|
# File 'lib/brut/tui/terminal_theme.rb', line 114
def reset = esc("0")
|
#strike ⇒ Object
103
|
# File 'lib/brut/tui/terminal_theme.rb', line 103
def strike = esc("9")
|
#strike_off ⇒ Object
104
|
# File 'lib/brut/tui/terminal_theme.rb', line 104
def strike_off = esc("29")
|
#success ⇒ Object
90
|
# File 'lib/brut/tui/terminal_theme.rb', line 90
def success = bold + bright_green
|
#warning ⇒ Object
92
|
# File 'lib/brut/tui/terminal_theme.rb', line 92
def warning = bold + yellow
|
#weak ⇒ Object
106
|
# File 'lib/brut/tui/terminal_theme.rb', line 106
def weak = esc("2")
|
#weak_off ⇒ Object
107
|
# File 'lib/brut/tui/terminal_theme.rb', line 107
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.
45
46
47
48
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
85
86
87
88
|
# File 'lib/brut/tui/terminal_theme.rb', line 45
def with_markup(string, text: :normal, reset: true)
result = +""
regular_text_code = if self.respond_to?(text)
send(text)
else
self.normal
end
result << regular_text_code
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
in :strike
result << strike_off
in :code
result << code_off
in :weak
result << weak_off
end
if regular_text_code != normal
result << regular_text_code
end
in :text
result << value
end
end
if reset
result << self.reset
end
result
end
|