Class: App::Terminal
- Inherits:
-
Object
- Object
- App::Terminal
- Extended by:
- Columnist
- Defined in:
- lib/core/terminal.rb
Constant Summary collapse
- MSG_INFO =
'info'
- MSG_WARNING =
'warning'
- MSG_ERROR =
'error'
- MSG_TODO =
'todo'
- MSG_AUTOMATIC =
'automatic'
- MSG_GENERATED =
'generated'
- MSG_PROCESSED =
'processed'
- MSG_CUSTOM =
'custom'
Class Method Summary collapse
-
.abort(title = nil, message = nil, exit_script = true, preceding_blank_line = false) ⇒ Object
Displays error and exits script by default.
-
.any_key_to_continue(text = "\n\x1B[90mPress any key to continue\x1B[0m => ") ⇒ Object
Gives a prompt where ANY KEY will continue executing the script.
-
.automatic(title = nil, message = nil, preceding_blank_line = true) ⇒ Object
Displays automatic message.
-
.command(commands, location = nil, verbose = true, verbose_cd = true, capture_real_output = false) ⇒ Object
Runs a series of commands in the terminal.
-
.command_capture(commands, location = nil, verbose = true, verbose_cd = true) ⇒ Object
Runs a series of commands in the terminal (and captures the output).
-
.error(title = nil, message = nil, exit_script = true, preceding_blank_line = true, error_text = 'Error') ⇒ Object
Displays error and exits script by default.
-
.fill(length = 0, string = ' ') ⇒ Object
Generates ‘filler’ string.
-
.format_action(action_text) ⇒ Object
Returns action text in consistent, uniform manner.
-
.format_branch(branch_name) ⇒ Object
Returns branch name in consistent, uniform manner.
-
.format_command(command_name) ⇒ Object
Returns command name in consistent, uniform manner.
-
.format_directory(directory_name) ⇒ Object
Returns directory name in consistent, uniform manner.
-
.format_flag(flag_letter, display_flag_text = true) ⇒ Object
Returns flag name in consistent, uniform manner.
-
.format_highlight(highlighted_text, capitalize = false) ⇒ Object
Returns action text in consistent, uniform manner.
-
.format_invalid(invalid_string, capitalize = false) ⇒ Object
Returns invalid data in consistent, uniform manner.
-
.get_terminal_height ⇒ Object
Gets the Terminal height.
-
.get_terminal_width ⇒ Object
Gets the Terminal width.
-
.info(title = nil, message = nil, preceding_blank_line = true) ⇒ Object
Displays info message.
-
.output(message = 'No message', type = MSG_INFO, title = nil, bg_color = nil) ⇒ Object
Outputs messages to the terminal.
-
.prompt_for_input(text) ⇒ Object
Shows a prompt waiting for user input.
-
.prompt_yes_no(title = nil, message = nil, confirmation_message = nil, preceding_blank_line = true) ⇒ Object
Gives a prompt where ‘y/Y’ will return TRUE, ‘n/N’ will return false, and ANY other key will do nothing.
-
.success(title = nil, message = nil, preceding_blank_line = true) ⇒ Object
Displays success message.
-
.warning(title = nil, message = nil, preceding_blank_line = true) ⇒ Object
Displays warning message.
Class Method Details
.abort(title = nil, message = nil, exit_script = true, preceding_blank_line = false) ⇒ Object
Displays error and exits script by default.
97 98 99 100 101 102 103 104 105 |
# File 'lib/core/terminal.rb', line 97 def self.abort(title = nil, = nil, exit_script = true, preceding_blank_line = false) if title.nil? title = 'Abandon ship!' end if .nil? = "You have chosen to \x1B[38;5;9mABORT\x1B[38;5;240m the script." end App::Terminal::error(title, , exit_script, preceding_blank_line, 'ABORT') end |
.any_key_to_continue(text = "\n\x1B[90mPress any key to continue\x1B[0m => ") ⇒ Object
Gives a prompt where ANY KEY will continue executing the script.
293 294 295 296 297 298 299 |
# File 'lib/core/terminal.rb', line 293 def self.any_key_to_continue(text = "\n\x1B[90mPress any key to continue\x1B[0m => ") STDOUT.flush print "#{text}" STDIN.gets puts nil end |
.automatic(title = nil, message = nil, preceding_blank_line = true) ⇒ Object
Displays automatic message.
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/core/terminal.rb', line 125 def self.automatic(title = nil, = nil, preceding_blank_line = true) if title.nil? title = 'Something is happening automatically...' end if preceding_blank_line puts end puts "\x1B[48;5;96m Automatic \x1B[0m \xe2\x86\x92 #{title}" () end |
.command(commands, location = nil, verbose = true, verbose_cd = true, capture_real_output = false) ⇒ Object
Runs a series of commands in the terminal.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/core/terminal.rb', line 21 def self.command(commands, location = nil, verbose = true, verbose_cd = true, capture_real_output = false) unless commands.is_a?(Array) commands = [commands] end unless location.nil? unless File.directory?(File.(location)) error('Directory not found.', "Cannot find the following directory: \x1B[38;5;205m#{location}\x1B[0m", true) end end output = [] if verbose_cd && verbose && commands.any? && !location.nil? puts "\x1B[42m Executing \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m #{App::Terminal::format_command("cd #{location}")}" end if commands.any? commands.each do |command| if verbose puts "\x1B[42m Executing \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m #{App::Terminal::format_command("#{command}")}" end if location.nil? if capture_real_output output << `#{command}` else output << system("#{command}") end else if capture_real_output output << `cd #{location} && #{command}` else output << system("cd #{location} && #{command}") end end end end if capture_real_output output.map! { |v| v.gsub('\n', "\n") } end output end |
.command_capture(commands, location = nil, verbose = true, verbose_cd = true) ⇒ Object
Runs a series of commands in the terminal (and captures the output).
62 63 64 |
# File 'lib/core/terminal.rb', line 62 def self.command_capture(commands, location = nil, verbose = true, verbose_cd = true) command(commands, location, verbose, verbose_cd, true) end |
.error(title = nil, message = nil, exit_script = true, preceding_blank_line = true, error_text = 'Error') ⇒ Object
Displays error and exits script by default.
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/core/terminal.rb', line 109 def self.error(title = nil, = nil, exit_script = true, preceding_blank_line = true, error_text = 'Error') if title.nil? title = "It seems you're trying to do something silly." end if preceding_blank_line puts end puts " \x1B[48;5;196m #{error_text} \x1B[0m \xe2\x86\x92 #{title}" () if exit_script exit end end |
.fill(length = 0, string = ' ') ⇒ Object
Generates ‘filler’ string.
242 243 244 245 246 247 248 |
# File 'lib/core/terminal.rb', line 242 def self.fill(length = 0, string = ' ') fill_string = '' (0..length - 1).each { fill_string = "#{fill_string}#{string}" } fill_string end |
.format_action(action_text) ⇒ Object
Returns action text in consistent, uniform manner.
178 179 180 |
# File 'lib/core/terminal.rb', line 178 def self.format_action(action_text) "\x1B[38;5;170m#{action_text.upcase}\x1B[0m" end |
.format_branch(branch_name) ⇒ Object
Returns branch name in consistent, uniform manner.
184 185 186 |
# File 'lib/core/terminal.rb', line 184 def self.format_branch(branch_name) "\x1B[38;5;40m[#{branch_name}]\x1B[0m" end |
.format_command(command_name) ⇒ Object
Returns command name in consistent, uniform manner.
190 191 192 |
# File 'lib/core/terminal.rb', line 190 def self.format_command(command_name) "\x1B[38;5;220m#{command_name}\x1B[0m" end |
.format_directory(directory_name) ⇒ Object
Returns directory name in consistent, uniform manner.
196 197 198 |
# File 'lib/core/terminal.rb', line 196 def self.format_directory(directory_name) "\x1B[38;5;48m#{directory_name}\x1B[0m" end |
.format_flag(flag_letter, display_flag_text = true) ⇒ Object
Returns flag name in consistent, uniform manner.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/core/terminal.rb', line 202 def self.format_flag(flag_letter, display_flag_text = true) letter_array = [] if flag_letter.is_a? String letter_array = Array[flag_letter] elsif flag_letter.is_a? Array letter_array = flag_letter else App::Terminal::error('Terminal::format_flag expects either String or Array.', nil, true) end flag_txt = '' letter_array.each do |letter| flag_txt = "#{flag_txt}, -#{letter}" end xtra_txt = letter_array.length > 1 ? ' flags' : ' flag' flag_txt = flag_txt[2..-1] "\x1B[38;5;152m#{flag_txt}#{display_flag_text ? xtra_txt : ''}\x1B[0m" end |
.format_highlight(highlighted_text, capitalize = false) ⇒ Object
Returns action text in consistent, uniform manner.
222 223 224 225 226 227 228 |
# File 'lib/core/terminal.rb', line 222 def self.format_highlight(highlighted_text, capitalize = false) if capitalize return "\x1B[38;5;117m#{highlighted_text.upcase}\x1B[0m" else return "\x1B[38;5;117m#{highlighted_text}\x1B[0m" end end |
.format_invalid(invalid_string, capitalize = false) ⇒ Object
Returns invalid data in consistent, uniform manner.
232 233 234 235 236 237 238 |
# File 'lib/core/terminal.rb', line 232 def self.format_invalid(invalid_string, capitalize = false) if capitalize return "\x1B[38;5;196m#{invalid_string.upcase}\x1B[0m" else return "\x1B[38;5;196m#{invalid_string}\x1B[0m" end end |
.get_terminal_height ⇒ Object
Gets the Terminal height
310 311 312 313 |
# File 'lib/core/terminal.rb', line 310 def self.get_terminal_height terminal_size = HighLine::SystemExtensions.terminal_size terminal_size[1] end |
.get_terminal_width ⇒ Object
Gets the Terminal width
303 304 305 306 |
# File 'lib/core/terminal.rb', line 303 def self.get_terminal_width terminal_size = HighLine::SystemExtensions.terminal_size terminal_size[0] end |
.info(title = nil, message = nil, preceding_blank_line = true) ⇒ Object
Displays info message.
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/core/terminal.rb', line 139 def self.info(title = nil, = nil, preceding_blank_line = true) if title.nil? title = 'Whatever you did, worked.' end if preceding_blank_line puts end puts " \x1B[48;5;240m Message \x1B[0m \xe2\x86\x92 #{title}" () end |
.output(message = 'No message', type = MSG_INFO, title = nil, bg_color = nil) ⇒ Object
Outputs messages to the terminal. If CUSTOM, title must be 11 characters to match existing scheme.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/core/terminal.rb', line 69 def self.output( = 'No message', type = MSG_INFO, title = nil, bg_color = nil) case type when MSG_INFO puts "\x1B[48;5;2m Executing \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m \x1B[0m#{}\x1B[0m" when MSG_WARNING puts "\x1B[48;5;202m Warning \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m \x1B[0m#{}\x1B[0m" when MSG_ERROR puts "\x1B[48;5;196m Error \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m \x1B[0m#{}\x1B[0m" when MSG_TODO puts "\x1B[48;5;199m @TODO \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m \x1B[0m#{}\x1B[0m" when MSG_AUTOMATIC puts "\x1B[48;5;96m Automatic \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m \x1B[0m#{}\x1B[0m" when MSG_GENERATED puts "\x1B[48;5;96m Generated \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m \x1B[0m#{}\x1B[0m" when MSG_PROCESSED puts "\x1B[48;5;238m Processed \x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m \x1B[0m#{}\x1B[0m" when MSG_CUSTOM raise RuntimeError, "'title' cannot be nil." if title.nil? raise RuntimeError, "'bg_color' cannot be nil." if bg_color.nil? puts "\x1B[48;5;#{bg_color}m#{title}\x1B[0m \x1B[0m\xe2\x86\x92\x1B[0m \x1B[0m#{}\x1B[0m" else puts "'#{type}' is not a valid Terminal::output() type." exit end end |
.prompt_for_input(text) ⇒ Object
Shows a prompt waiting for user input.
285 286 287 288 289 |
# File 'lib/core/terminal.rb', line 285 def self.prompt_for_input(text) response = Readline.readline(text, true) exit if %w(quit exit q x).include?(response) response end |
.prompt_yes_no(title = nil, message = nil, confirmation_message = nil, preceding_blank_line = true) ⇒ Object
Gives a prompt where ‘y/Y’ will return TRUE, ‘n/N’ will return false, and ANY other key will do nothing.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/core/terminal.rb', line 252 def self.prompt_yes_no(title = nil, = nil, = nil, preceding_blank_line = true) if title.nil? title = 'Please confirm YES or NO.' end if .nil? = 'Would you like to continue?'; end if preceding_blank_line puts end puts " \x1B[48;5;56m Confirm \x1B[0m \xe2\x86\x92 #{title}" () response = '' until %w[y Y n N x X a A].include? response response = ask(" \x1B[38;5;89m#{} \x1B[0m[y/n]\x1B[90m => \x1B[0m") end case response.downcase when 'y' puts "\n" return true when 'n' puts "\n" return false when 'a' App::Terminal::error('Abandon ship!', ["You have chosen to \x1B[38;5;9mABORT\x1B[38;5;240m the script.", nil, 'Please note that whenever you do this, any scripted tasks which were running', 'will have been interrupted mid-script. This may (or may not) cause problems.'], true) when 'x' App::Terminal::error('Abandon ship!', ["You have chosen to \x1B[38;5;9mABORT\x1B[38;5;240m the script.", nil, 'Please note that whenever you do this, any scripted tasks which were running', 'will have been interrupted mid-script. This may (or may not) cause problems.'], true) else end end |
.success(title = nil, message = nil, preceding_blank_line = true) ⇒ Object
Displays success message.
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/core/terminal.rb', line 152 def self.success(title = nil, = nil, preceding_blank_line = true) if title.nil? title = 'Whatever you did, worked.' end if preceding_blank_line puts end puts " \x1B[48;5;12m Success \x1B[0m \xe2\x86\x92 #{title}" () end |
.warning(title = nil, message = nil, preceding_blank_line = true) ⇒ Object
Displays warning message.
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/core/terminal.rb', line 165 def self.warning(title = nil, = nil, preceding_blank_line = true) if title.nil? title = 'Whatever you did, generated a warning.' end if preceding_blank_line puts end puts " \x1B[48;5;202m Warning \x1B[0m \xe2\x86\x92 #{title}" () end |