Class: Cli::Display

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

Class Method Summary collapse

Class Method Details

.confirmation(title) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cli/display.rb', line 32

def self.confirmation(title)
    while true do
        puts(title.colorize(mode: :bold))
        print("y/N: ".colorize(mode: :bold))
        response = STDIN.gets.chomp.downcase

        if response.empty?
            puts("Response required".colorize(:red))
        elsif response == "y"
            return true
        elsif response == "n"
            return false
        else
            puts("Invalid response. Must be one letter, case insenstive".colorize(:red))
        end
    end
end

.dispaly_diff(original_content, new_content) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cli/display.rb', line 93

def self.dispaly_diff(original_content, new_content)
    Diffy::Diff.new(original_content, new_content, source: 'strings').each do |line|
        if line.start_with?("+")
            puts line.colorize(:green)
        elsif line.start_with?("-")
            puts line.colorize(:red)
        else
            puts line
        end
    end
end

.error_message(message) ⇒ Object



81
82
83
# File 'lib/cli/display.rb', line 81

def self.error_message(message)
    puts(message.colorize(:red))
end

.get_input(input) ⇒ Object

def self.get_input(input)

print(input.colorize(mode: :bold))
lines = []
begin
    while line = STDIN.readline&.chomp
        lines << line
    end
rescue EOFError
end
lines.join("\n")

end



71
72
73
74
75
# File 'lib/cli/display.rb', line 71

def self.get_input(input)
    prompt = TTY::Prompt.new
    lines = prompt.multiline(input.colorize(mode: :bold))
    lines.join("\n")
end

.info_message(message) ⇒ Object



85
86
87
# File 'lib/cli/display.rb', line 85

def self.info_message(message)
    puts(message.colorize(color: :light_black))
end

.message(role, content) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/cli/display.rb', line 9

def self.message(role, content)
    if role == "user"
        puts("you".colorize(background: :green) + ": " + content)
    else
        puts("assistant".colorize(background: :yellow) + ": " + content)
    end
end

.select(title, options) ⇒ Object



27
28
29
30
# File 'lib/cli/display.rb', line 27

def self.select(title, options)
    prompt = TTY::Prompt.new
    prompt.select(title.colorize(mode: :bold), options, per_page: 100, columnize: 2)
end

.spinner(title = "running") ⇒ Object



89
90
91
# File 'lib/cli/display.rb', line 89

def self.spinner(title="running")
    TTY::Spinner.new("[:spinner] #{title}", format: :spin_2)
end

.success_message(message) ⇒ Object



77
78
79
# File 'lib/cli/display.rb', line 77

def self.success_message(message)
    puts(message.colorize(:green))
end

.table(rows, headers) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/cli/display.rb', line 17

def self.table(rows, headers)
    rows = rows.map(&:values)        
    table = Terminal::Table.new(
      headings: headers,
      rows: rows
    )

    puts table
end