Class: Brut::CLI::TerminalTheme

Inherits:
Object
  • Object
show all
Defined in:
lib/brut/cli/terminal_theme.rb

Instance Method Summary collapse

Constructor Details

#initialize(terminal:) ⇒ TerminalTheme

Returns a new instance of TerminalTheme.



3
4
5
# File 'lib/brut/cli/terminal_theme.rb', line 3

def initialize(terminal:)
  @terminal = terminal
end

Instance Method Details

#bullet(n) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/brut/cli/terminal_theme.rb', line 25

def bullet(n)
  @bullets ||= [
    Lipgloss::Style.new.foreground(yellow_strong).margin_right(1).margin_left(2),
    Lipgloss::Style.new.foreground(purple_strong).margin_right(1),
    Lipgloss::Style.new.foreground(yellow_weak).margin_right(1),
    Lipgloss::Style.new.foreground(purple_weak).margin_right(1),
  ]
  @bullets[n] || none
end

#codeObject



22
23
24
# File 'lib/brut/cli/terminal_theme.rb', line 22

def code
  @code ||= Lipgloss::Style.new.foreground(cyan_strong).bold(true)
end

#errorObject



47
48
49
# File 'lib/brut/cli/terminal_theme.rb', line 47

def error
  @error ||= Lipgloss::Style.new.foreground(red_strong).bold(true)
end

#exceptionObject



43
44
45
# File 'lib/brut/cli/terminal_theme.rb', line 43

def exception
  @exception ||= Lipgloss::Style.new.foreground(red_weak)
end

#headerObject



10
11
12
# File 'lib/brut/cli/terminal_theme.rb', line 10

def header
  @header ||= Lipgloss::Style.new.foreground(blue_strong).bold(true)
end

#noneObject



35
36
37
# File 'lib/brut/cli/terminal_theme.rb', line 35

def none
  @none ||= Lipgloss::Style.new
end

#subheaderObject



13
14
15
# File 'lib/brut/cli/terminal_theme.rb', line 13

def subheader
  @subheader ||= Lipgloss::Style.new.foreground(cyan_strong).italic(true)
end

#successObject



39
40
41
# File 'lib/brut/cli/terminal_theme.rb', line 39

def success
  @success ||= Lipgloss::Style.new.foreground(green_weak).bold(true)
end

#titleObject



7
8
9
# File 'lib/brut/cli/terminal_theme.rb', line 7

def title
  @title ||= Lipgloss::Style.new.bold(true).foreground(white_strong)
end

#urlObject



19
20
21
# File 'lib/brut/cli/terminal_theme.rb', line 19

def url
  @url ||= Lipgloss::Style.new.underline(true).bold(true)
end

#warningObject



51
52
53
# File 'lib/brut/cli/terminal_theme.rb', line 51

def warning
  @warning ||= Lipgloss::Style.new.foreground(yellow_strong).bold(true)
end

#weakObject



16
17
18
# File 'lib/brut/cli/terminal_theme.rb', line 16

def weak
  @weak ||= Lipgloss::Style.new.faint(true)
end

#wrap(text, first_indent: true, indent:, max_width: nil, newline: "\n") ⇒ Object



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
# File 'lib/brut/cli/terminal_theme.rb', line 55

def wrap(text, first_indent: true, indent:, max_width: nil, newline: "\n")
  max_width ||= @terminal.cols
  text_width = [ max_width, @terminal.cols ].min - newline.length
  lines = []
  text.split(/\s+/).each do |word|
    current_line = lines.last
    if current_line.nil?
      lines << []
      current_line = lines.last
    end
    if (current_line.join(" ").length + word.length + 1) > (text_width - indent)
      lines << [ word ]
    else
      current_line << word
    end
  end
  prefix = " " * indent
  lines.map.with_index { |line,index|
    if index == 0 && !first_indent
      line.join(" ")
    else
      prefix + line.join(" ")
    end
  }.join(newline)
end