Class: ErrorToCommunicate::Theme

Inherits:
Object
  • Object
show all
Defined in:
lib/error_to_communicate/theme.rb

Instance Method Summary collapse

Instance Method Details

#bri_redObject



104
105
106
# File 'lib/error_to_communicate/theme.rb', line 104

def bri_red
  fg_rgb 5, 0, 0
end

#classname(classname) ⇒ Object



10
11
12
# File 'lib/error_to_communicate/theme.rb', line 10

def classname(classname)
  "#{white}#{classname}#{none}"
end

#color_filename(str) ⇒ Object



61
62
63
# File 'lib/error_to_communicate/theme.rb', line 61

def color_filename(str)
  "\e[38;5;49;1m#{str}\e[39m" # fg r:0, g:5, b:3 (out of 0..5)
end

#color_linenum(linenum) ⇒ Object



44
45
46
# File 'lib/error_to_communicate/theme.rb', line 44

def color_linenum(linenum)
  "\e[34m#{linenum}\e[39m"
end

#color_path(str) ⇒ Object



40
41
42
# File 'lib/error_to_communicate/theme.rb', line 40

def color_path(str)
  "\e[38;5;36m#{str}\e[39m" # fg r:0, g:3, b:2 (out of 0..5)
end

#columns(*content) ⇒ Object

—– Semantic —– TODO: Not good enough, see note on FormatTerminal#format



6
7
8
# File 'lib/error_to_communicate/theme.rb', line 6

def columns(*content)
  content.join(' | ') + "\n"
end

#context(context) ⇒ Object



22
23
24
# File 'lib/error_to_communicate/theme.rb', line 22

def context(context)
  "#{dim_red}#{context}#{none}"
end

#desaturate(str) ⇒ Object



65
66
67
68
69
# File 'lib/error_to_communicate/theme.rb', line 65

def desaturate(str)
  nocolor = str.gsub(/\e\[[\d;]+?m/, "")
  allgray = nocolor.gsub(/^(.*?)\n?$/, "\e[38;5;240m\\1\e[39m\n")
  allgray
end

#details(details) ⇒ Object



26
27
28
# File 'lib/error_to_communicate/theme.rb', line 26

def details(details)
  "#{white}#{details}#{none}"
end

#dim_redObject



108
109
110
# File 'lib/error_to_communicate/theme.rb', line 108

def dim_red
  fg_rgb 3, 0, 0
end

#explanation(explanation) ⇒ Object



18
19
20
# File 'lib/error_to_communicate/theme.rb', line 18

def explanation(explanation)
  "#{bri_red}#{explanation}#{none}"
end

#fg_rgb(red, green, blue, max = 6) ⇒ Object



133
134
135
# File 'lib/error_to_communicate/theme.rb', line 133

def fg_rgb(red, green, blue, max=6)
  "\e[38;5;#{rgb red, green, blue, max}m"
end

#highlight_text(code, index, text) ⇒ Object



88
89
90
91
92
93
# File 'lib/error_to_communicate/theme.rb', line 88

def highlight_text(code, index, text)
  lines = code.lines.to_a
  return code unless text && lines[index]
  lines[index].gsub! text, invert(text)
  lines.join("")
end

#indent(str, indentation_str) ⇒ Object



95
96
97
# File 'lib/error_to_communicate/theme.rb', line 95

def indent(str, indentation_str)
  str.gsub /^/, indentation_str
end

#invert(text) ⇒ Object



48
49
50
# File 'lib/error_to_communicate/theme.rb', line 48

def invert(text)
  "\e[7m#{text}\e[27m"
end

#mark_linenum(linenum) ⇒ Object



30
31
32
# File 'lib/error_to_communicate/theme.rb', line 30

def mark_linenum(linenum)
  "#{bri_red}#{linenum}#{none}"
end

#message(message) ⇒ Object



14
15
16
# File 'lib/error_to_communicate/theme.rb', line 14

def message(message)
  "#{bri_red}#{message}#{none}"
end

#noneObject



112
113
114
# File 'lib/error_to_communicate/theme.rb', line 112

def none
  "\e[39m"
end

#remove_ansi_codes_after_last_newline(text) ⇒ Object



82
83
84
85
86
# File 'lib/error_to_communicate/theme.rb', line 82

def remove_ansi_codes_after_last_newline(text)
  *neck, ass = text.lines # ... kinda like head/tail :P
  return text unless neck.any? && ass[/^(\e\[(\d+;?)*m)*$/]
  neck.join("").chomp << ass
end

#rgb(red, green, blue, max = 6) ⇒ Object

Each of r, g, b, can have a value 0-5. If you want to do 0-255, you need to pass 255 as max. The terminal cans till only display 0-5, but you can pass your rgb values in, and we’ll translate them here



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/error_to_communicate/theme.rb', line 120

def rgb(red, green, blue, max=6)
  max = max.to_f
  n   = 6

  # translate each colour to 0-5
  r = (n * red   / max).to_i
  g = (n * green / max).to_i
  b = (n * blue  / max).to_i

  # move them to their offsets, I think the first 16 are for the system colors
  16 + r*(n**2) + g*(n**1) + b*(n**0)
end

#screaming_red(text) ⇒ Object



52
53
54
55
# File 'lib/error_to_communicate/theme.rb', line 52

def screaming_red(text)
  return "" if text.empty?
  "\e[38;5;255;48;5;88m #{text} \e[39;49m" # bright white on medium red
end

#separator_lineObject




36
37
38
# File 'lib/error_to_communicate/theme.rb', line 36

def separator_line
  ("="*70) << "\n"
end

#syntax_highlight(raw_code) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/error_to_communicate/theme.rb', line 73

def syntax_highlight(raw_code)
  formatter = Rouge::Formatters::Terminal256.new theme: 'github'
  lexer     = Rouge::Lexers::Ruby.new
  tokens    = lexer.lex raw_code
  formatted = formatter.format(tokens)
  formatted << "\n" unless formatted.end_with? "\n"
  remove_ansi_codes_after_last_newline(formatted)
end

#underline(str) ⇒ Object



57
58
59
# File 'lib/error_to_communicate/theme.rb', line 57

def underline(str)
  "\e[4m#{str}\e[24m"
end

#whiteObject

TODO: rename these to all imply foreground



100
101
102
# File 'lib/error_to_communicate/theme.rb', line 100

def white
  fg_rgb 5, 5, 5
end