class Misfortune::Render::Terminal

Constants

BLOCK_DELIMITER
BLOCK_MAX_WITH
LIST_INDENTATION
LIST_ORDERED_ITEM_CHAR
LIST_ORDERED_ITEM_PREFIX
LIST_UNORDERED_ITEM_CHAR

Attributes

pastel[R]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/misfortune/render/terminal.rb, line 15
def initialize
  @pastel = Pastel.new(enabled: $stdout.tty?, eachline: new_line)

  super
end

Public Instance Methods

block_code(code, language) click to toggle source
# File lib/misfortune/render/terminal.rb, line 26
def block_code(code, language)
  text = ''
  text << labeled_hrule(language)
  text << indent(code(code))
  text << hrule

  surround_with_line_breaks(text, 1, 2)
end
block_quote(quote) click to toggle source
# File lib/misfortune/render/terminal.rb, line 35
def block_quote(quote)
  text = pastel.cyan(quote)
  surround_with_line_breaks(text, 1, 2)
end
codespan(code) click to toggle source
# File lib/misfortune/render/terminal.rb, line 91
def codespan(code)
  code(code)
end
double_emphasis(text) click to toggle source
# File lib/misfortune/render/terminal.rb, line 71
def double_emphasis(text)
  pastel.bold(text)
end
emphasis(text) click to toggle source
# File lib/misfortune/render/terminal.rb, line 67
def emphasis(text)
  pastel.underline(text)
end
header(text, _header_level) click to toggle source
# File lib/misfortune/render/terminal.rb, line 21
def header(text, _header_level)
  text = pastel.underline(text)
  surround_with_line_breaks(text, 0, 2)
end
hrule() click to toggle source
# File lib/misfortune/render/terminal.rb, line 63
def hrule
  labeled_hrule('')
end
list(contents, list_type) click to toggle source
# File lib/misfortune/render/terminal.rb, line 44
def list(contents, list_type)
  contents = enumerate_list(contents) if list_type == :ordered
  contents = indent(contents)

  surround_with_line_breaks(contents, 0, 1)
end
list_item(text, list_type) click to toggle source
# File lib/misfortune/render/terminal.rb, line 51
def list_item(text, list_type)
  prefix =
    case list_type
    when :ordered
      LIST_ORDERED_ITEM_PREFIX
    when :unordered
      LIST_UNORDERED_ITEM_CHAR
    end

  "#{prefix} #{text}"
end
paragraph(text) click to toggle source
# File lib/misfortune/render/terminal.rb, line 40
def paragraph(text)
  surround_with_line_breaks(text, 0, 2)
end
strikethrough(text) click to toggle source
# File lib/misfortune/render/terminal.rb, line 79
def strikethrough(text)
  pastel.strikethrough(text)
end
triple_emphasis(text) click to toggle source
# File lib/misfortune/render/terminal.rb, line 75
def triple_emphasis(text)
  emphasis(double_emphasis(text))
end

Protected Instance Methods

clear_surrounding_line_breaks(text) click to toggle source
# File lib/misfortune/render/terminal.rb, line 102
def clear_surrounding_line_breaks(text)
  text.sub(/\A[\r\n]+/, '').sub(/[\r\n]+\z/, '')
end
code(text) click to toggle source
# File lib/misfortune/render/terminal.rb, line 122
def code(text)
  pastel.green(text.chomp)
end
enumerate_list(text) click to toggle source
# File lib/misfortune/render/terminal.rb, line 112
def enumerate_list(text)
  text.gsub(LIST_ORDERED_ITEM_PREFIX).with_index do |_, i|
    "#{i + 1}#{LIST_ORDERED_ITEM_CHAR}"
  end
end
indent(text) click to toggle source
# File lib/misfortune/render/terminal.rb, line 106
def indent(text)
  text.strip
    .gsub(new_line, "#{new_line}#{LIST_INDENTATION}")
    .prepend(LIST_INDENTATION)
end
labeled_hrule(text) click to toggle source
# File lib/misfortune/render/terminal.rb, line 126
def labeled_hrule(text)
  text = (text || '').to_s.upcase
  text = text.center(10, ' ') unless text.empty?
  text = text.center(BLOCK_MAX_WITH, BLOCK_DELIMITER)

  surround_with_line_breaks(text, 1, 1)
end
new_line() click to toggle source
# File lib/misfortune/render/terminal.rb, line 118
def new_line
  "\n"
end
surround_with_line_breaks(text, prepend_count, append_count) click to toggle source
# File lib/misfortune/render/terminal.rb, line 97
def surround_with_line_breaks(text, prepend_count, append_count)
  text = clear_surrounding_line_breaks(text)
  (new_line * prepend_count) << text << (new_line * append_count)
end