Class: Shai::UI
- Inherits:
-
Object
- Object
- Shai::UI
- Defined in:
- lib/shai/ui.rb
Instance Method Summary collapse
-
#ask(question, **options) ⇒ Object
Interactive prompts.
- #blank ⇒ Object
- #bold(text) ⇒ Object
- #cyan(text) ⇒ Object
-
#diff(text) ⇒ Object
Diff output.
-
#dim(text) ⇒ Object
Formatting.
-
#display_configuration(config, detailed: false) ⇒ Object
Configuration display.
-
#display_file_operation(operation, path) ⇒ Object
File operation display.
-
#display_tree(items, prefix: "") ⇒ Object
Tree display.
- #error(message) ⇒ Object
- #green(text) ⇒ Object
- #header(title) ⇒ Object
- #indent(text, spaces: 2) ⇒ Object
- #info(message) ⇒ Object
-
#initialize(color: true) ⇒ UI
constructor
A new instance of UI.
- #mask(question, **options) ⇒ Object
- #red(text) ⇒ Object
- #select(question, choices, **options) ⇒ Object
-
#spinner(message) ⇒ Object
Spinner for async operations.
-
#success(message) ⇒ Object
Output helpers.
-
#table(headers, rows) ⇒ Object
Table output.
- #warning(message) ⇒ Object
- #yellow(text) ⇒ Object
- #yes?(question, default: false) ⇒ Boolean
Constructor Details
#initialize(color: true) ⇒ UI
Returns a new instance of UI.
10 11 12 13 |
# File 'lib/shai/ui.rb', line 10 def initialize(color: true) @pastel = Pastel.new(enabled: color) @prompt = TTY::Prompt.new end |
Instance Method Details
#ask(question, **options) ⇒ Object
Interactive prompts
71 72 73 |
# File 'lib/shai/ui.rb', line 71 def ask(question, **) @prompt.ask(question, **) end |
#blank ⇒ Object
32 33 34 |
# File 'lib/shai/ui.rb', line 32 def blank puts end |
#bold(text) ⇒ Object
50 51 52 |
# File 'lib/shai/ui.rb', line 50 def bold(text) @pastel.bold(text) end |
#cyan(text) ⇒ Object
54 55 56 |
# File 'lib/shai/ui.rb', line 54 def cyan(text) @pastel.cyan(text) end |
#diff(text) ⇒ Object
Diff output
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/shai/ui.rb', line 129 def diff(text) text.each_line do |line| case line[0] when "+" puts @pastel.green(line) when "-" puts @pastel.red(line) when "@" puts @pastel.cyan(line) else puts line end end end |
#dim(text) ⇒ Object
Formatting
46 47 48 |
# File 'lib/shai/ui.rb', line 46 def dim(text) @pastel.dim(text) end |
#display_configuration(config, detailed: false) ⇒ Object
Configuration display
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/shai/ui.rb', line 145 def display_configuration(config, detailed: false) name = config["slug"] || config["name"] # owner can be a string (username) or a hash with "username" key owner = config["owner"] owner = owner["username"] if owner.is_a?(Hash) full_name = owner ? "#{owner}/#{name}" : name visibility = config["visibility"] stars = config["stars_count"] || 0 description = config["description"] line = " #{full_name}" line += " (#{visibility})" if visibility == "private" line += " #{yellow("★")} #{stars}" if stars.positive? puts line if description && !description.empty? puts " #{dim(description)}" end if detailed = config["tags"] || [] puts " #{dim("Tags: #{.join(", ")}")}" if .any? end end |
#display_file_operation(operation, path) ⇒ Object
File operation display
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/shai/ui.rb', line 180 def display_file_operation(operation, path) case operation when :created puts " #{green("Created")} #{path}" when :modified puts " #{yellow("Modified")} #{path}" when :updated puts " #{yellow("Updated")} #{path}" when :deleted puts " #{red("Deleted")} #{path}" when :uploaded puts " #{cyan("Uploading")} #{path}" when :would_create puts " #{path}" when :would_update puts " #{yellow("~")} #{path}" when :conflict puts " #{red(path)}" end end |
#display_tree(items, prefix: "") ⇒ Object
Tree display
171 172 173 174 175 176 177 |
# File 'lib/shai/ui.rb', line 171 def display_tree(items, prefix: "") items.each_with_index do |item, index| is_last = index == items.length - 1 connector = is_last ? "└── " : "├── " puts "#{prefix}#{connector}#{item}" end end |
#error(message) ⇒ Object
20 21 22 |
# File 'lib/shai/ui.rb', line 20 def error() puts @pastel.red("Error: #{}") end |
#green(text) ⇒ Object
62 63 64 |
# File 'lib/shai/ui.rb', line 62 def green(text) @pastel.green(text) end |
#header(title) ⇒ Object
36 37 38 |
# File 'lib/shai/ui.rb', line 36 def header(title) puts @pastel.bold(title) end |
#indent(text, spaces: 2) ⇒ Object
40 41 42 43 |
# File 'lib/shai/ui.rb', line 40 def indent(text, spaces: 2) prefix = " " * spaces puts text.lines.map { |line| "#{prefix}#{line}" }.join end |
#info(message) ⇒ Object
28 29 30 |
# File 'lib/shai/ui.rb', line 28 def info() puts end |
#mask(question, **options) ⇒ Object
75 76 77 |
# File 'lib/shai/ui.rb', line 75 def mask(question, **) @prompt.mask(question, **) end |
#red(text) ⇒ Object
66 67 68 |
# File 'lib/shai/ui.rb', line 66 def red(text) @pastel.red(text) end |
#select(question, choices, **options) ⇒ Object
106 107 108 |
# File 'lib/shai/ui.rb', line 106 def select(question, choices, **) @prompt.select(question, choices, **) end |
#spinner(message) ⇒ Object
Spinner for async operations
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/shai/ui.rb', line 111 def spinner() spinner = TTY::Spinner.new("[:spinner] #{}", format: :dots) spinner.auto_spin result = yield spinner.success result rescue => e spinner.error raise e end |
#success(message) ⇒ Object
Output helpers
16 17 18 |
# File 'lib/shai/ui.rb', line 16 def success() puts @pastel.green("✓ #{}") end |
#table(headers, rows) ⇒ Object
Table output
123 124 125 126 |
# File 'lib/shai/ui.rb', line 123 def table(headers, rows) table = TTY::Table.new(header: headers, rows: rows) puts table.render(:unicode, padding: [0, 1]) end |
#warning(message) ⇒ Object
24 25 26 |
# File 'lib/shai/ui.rb', line 24 def warning() puts @pastel.yellow("Warning: #{}") end |
#yellow(text) ⇒ Object
58 59 60 |
# File 'lib/shai/ui.rb', line 58 def yellow(text) @pastel.yellow(text) end |
#yes?(question, default: false) ⇒ Boolean
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/shai/ui.rb', line 79 def yes?(question, default: false) require "io/console" hint = default ? "(Y/n)" : "(y/N)" print "#{question} #{hint} " $stdout.flush loop do key = $stdin.getch case key when "y", "Y" puts "y" return true when "n", "N" puts "n" return false when "\r", "\n" puts default ? "y" : "n" return default when "\u0003" # Ctrl+C puts raise Interrupt end # Ignore other keys, keep waiting end end |