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'
Class Method Summary collapse
-
.abort(title = nil, message = nil, exit_script = true, preceding_blank_line = true) ⇒ Object
Displays error and exits script by default.
- .any_key_to_continue(text = "\n\x1B[90mPress any key to continue\x1B[0m => ") ⇒ Object deprecated Deprecated.
-
.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) ⇒ 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 = true) ⇒ Object
Displays error and exits script by default.
91 92 93 94 95 96 97 98 99 |
# File 'lib/core/terminal.rb', line 91 def self.abort(title = nil, = nil, exit_script = true, preceding_blank_line = true) 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.
274 275 276 277 278 279 |
# File 'lib/core/terminal.rb', line 274 def self.any_key_to_continue(text = "\n\x1B[90mPress any key to continue\x1B[0m => ") STDOUT.flush print "#{text}" STDIN.gets nil end |
.command(commands, location = nil, verbose = true, verbose_cd = true, capture_real_output = false) ⇒ Object
Runs a series of commands in the terminal.
20 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 |
# File 'lib/core/terminal.rb', line 20 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).
61 62 63 |
# File 'lib/core/terminal.rb', line 61 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.
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/core/terminal.rb', line 103 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.
222 223 224 225 226 227 228 |
# File 'lib/core/terminal.rb', line 222 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.
158 159 160 |
# File 'lib/core/terminal.rb', line 158 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.
164 165 166 |
# File 'lib/core/terminal.rb', line 164 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.
170 171 172 |
# File 'lib/core/terminal.rb', line 170 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.
176 177 178 |
# File 'lib/core/terminal.rb', line 176 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.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/core/terminal.rb', line 182 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.
202 203 204 205 206 207 208 |
# File 'lib/core/terminal.rb', line 202 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.
212 213 214 215 216 217 218 |
# File 'lib/core/terminal.rb', line 212 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
290 291 292 293 |
# File 'lib/core/terminal.rb', line 290 def self.get_terminal_height terminal_size = HighLine::SystemExtensions.terminal_size terminal_size[1] end |
.get_terminal_width ⇒ Object
Gets the Terminal width
283 284 285 286 |
# File 'lib/core/terminal.rb', line 283 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.
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/core/terminal.rb', line 119 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) ⇒ Object
Outputs messages to the terminal.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/core/terminal.rb', line 67 def self.output( = 'No message', type = MSG_INFO) 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" 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.
265 266 267 268 269 |
# File 'lib/core/terminal.rb', line 265 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.
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/core/terminal.rb', line 232 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.
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/core/terminal.rb', line 132 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.
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/core/terminal.rb', line 145 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 |