Class: Brut::TUI::TerminalTheme

Inherits:
Object
  • Object
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

#initialize(terminal) ⇒ TerminalTheme

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

Returns:

  • (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

#boldObject



90
# File 'lib/brut/tui/terminal_theme.rb', line 90

def bold     = esc("1")

#bold_offObject



91
# File 'lib/brut/tui/terminal_theme.rb', line 91

def bold_off = normal

#brightObject



93
# File 'lib/brut/tui/terminal_theme.rb', line 93

def bright     = esc("1")

#bright_offObject



94
# File 'lib/brut/tui/terminal_theme.rb', line 94

def bright_off = normal

#codeObject



105
# File 'lib/brut/tui/terminal_theme.rb', line 105

def code     = underline

#code_offObject



106
# File 'lib/brut/tui/terminal_theme.rb', line 106

def code_off = underline_off

#errorObject



87
# File 'lib/brut/tui/terminal_theme.rb', line 87

def error   = bold + bright_red

#headingObject



112
# File 'lib/brut/tui/terminal_theme.rb', line 112

def heading = bold + underline + bright_blue

#italicObject



96
# File 'lib/brut/tui/terminal_theme.rb', line 96

def italic     = esc("3")

#italic_offObject



97
# File 'lib/brut/tui/terminal_theme.rb', line 97

def italic_off = esc("23")

#normalObject



108
# File 'lib/brut/tui/terminal_theme.rb', line 108

def normal = esc("22")

#resetObject



110
# File 'lib/brut/tui/terminal_theme.rb', line 110

def reset = esc("0")

#strikeObject



99
# File 'lib/brut/tui/terminal_theme.rb', line 99

def strike     = esc("9")

#strike_offObject



100
# File 'lib/brut/tui/terminal_theme.rb', line 100

def strike_off = esc("29")

#successObject



86
# File 'lib/brut/tui/terminal_theme.rb', line 86

def success = bold + bright_green

#warningObject



88
# File 'lib/brut/tui/terminal_theme.rb', line 88

def warning = bold + yellow

#weakObject



102
# File 'lib/brut/tui/terminal_theme.rb', line 102

def weak     = esc("2")

#weak_offObject



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