Class: Neetob::CLI::UI

Inherits:
Object
  • Object
show all
Defined in:
lib/neetob/cli/ui.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUI

Returns a new instance of UI.



12
13
14
# File 'lib/neetob/cli/ui.rb', line 12

def initialize
  @shell = Thor::Base.shell.new
end

Instance Attribute Details

#shellObject

Returns the value of attribute shell.



10
11
12
# File 'lib/neetob/cli/ui.rb', line 10

def shell
  @shell
end

Instance Method Details

#ask(question, echo = true) ⇒ Object



20
21
22
# File 'lib/neetob/cli/ui.rb', line 20

def ask(question, echo = true)
  shell.ask(question, echo:)
end

#error(statement, print_to_audit_log: true) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/neetob/cli/ui.rb', line 28

def error(statement, print_to_audit_log: true)
  shell.say(statement, Thor::Shell::Color::RED)
  if print_to_audit_log && (markdown_file = Thread.current[:markdown_file_name]).present?
    File.open(markdown_file, "a") do |f|
      f.puts statement
    end
  end
end

#indent(count) ⇒ Object



55
56
57
58
59
# File 'lib/neetob/cli/ui.rb', line 55

def indent(count)
  shell.indent(count) do
    yield
  end
end

#info(statement, print_to_audit_log: true) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/neetob/cli/ui.rb', line 46

def info(statement, print_to_audit_log: true)
  shell.say(statement)
  if print_to_audit_log && (markdown_file = Thread.current[:markdown_file_name]).present?
    File.open(markdown_file, "a") do |f|
      f.puts statement
    end
  end
end


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/neetob/cli/ui.rb', line 61

def print_table(data, options = {})
  shell.print_table(data, options)
  if (markdown_file = Thread.current[:markdown_file_name]).present?
    header = data.first
    rows = data[1..]
    column_widths = header.map.with_index do |_, col|
      [header[col].to_s.length, *rows.map { |row| row[col].to_s.length }].max
    end
    markdown_table = "| " + header.map.with_index { |h, i| h.to_s.ljust(column_widths[i]) }.join(" | ") + " |\n"
    markdown_table += "|-" + column_widths.map { |w| "-" * w }.join("-|-") + "-|\n"
    rows.each do |row|
      markdown_table += "| " + row.map.with_index { |cell, i| cell.to_s.gsub("\|", "\\|").ljust(column_widths[i]) }.join(" | ") + " |\n"
    end
    File.open(markdown_file, "a") do |f|
      f.puts markdown_table
    end
  end
end

#say(statement, color = Thor::Shell::Color::YELLOW) ⇒ Object



16
17
18
# File 'lib/neetob/cli/ui.rb', line 16

def say(statement, color = Thor::Shell::Color::YELLOW)
  shell.say(statement, color)
end

#success(statement, print_to_audit_log: true) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/neetob/cli/ui.rb', line 37

def success(statement, print_to_audit_log: true)
  shell.say(statement, Thor::Shell::Color::GREEN)
  if print_to_audit_log && (markdown_file = Thread.current[:markdown_file_name]).present?
    File.open(markdown_file, "a") do |f|
      f.puts statement
    end
  end
end

#yes?(question) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/neetob/cli/ui.rb', line 24

def yes?(question)
  shell.yes?(question)
end