Module: CLIHelper
- Defined in:
- lib/cli_helper.rb
Overview
CLI Helper
Defined Under Namespace
Modules: HashWithSearch Classes: ShowTable
Constant Summary collapse
- FILTER_OPS =
Available operators for filtering operations
['=', '!=', '<', '<=', '>', '>=', '~']
- LIST =
CLI general options
{ :name => 'list', :short => '-l x,y,z', :large => '--list x,y,z', :format => Array, :description => 'Selects columns to display with list command' }
- LISTCONF =
{ :name => 'listconf', :short => '-c conf', :large => '--listconf conf', :format => String, :description => 'Selects a predefined column list' }
- CSV_OPT =
{ :name => 'csv', :large => '--csv', :description => 'Write table in csv format' }
- CSV_DEL =
{ :name => 'csv_del', :large => '--csv-del del', :format => String, :description => 'Set delimiter for csv output' }
- FILTER =
{ :name => 'filter', :short => '-f x,y,z', :large => '--filter x,y,z', :format => Array, :description => "Filter data. An array is specified with\n" << ' ' * 31 << 'column=value pairs.' << ' ' * 31 << "Valid operators #{FILTER_OPS.join(',')}" << ' ' * 31 << 'e.g. NAME=test (match name with test)' << ' ' * 31 << 'NAME~test (match every NAME containing' << ' ' * 31 << 'the substring \'test\')' }
- OPERATOR =
{ :name => 'operator', :large => '--operator operator', :format => String, :description => 'Logical operator used on filters: AND, OR. ' \ 'Default: AND.' }
- NO_HEADER =
{ :name => 'no_header', :large => '--no-header', :description => 'Hides the header of the table' }
- DELAY =
{ :name => 'delay', :short => '-d x', :large => '--delay x', :format => Integer, :description => 'Sets the delay in seconds for top command' }
- NO_PAGER =
{ :name => 'no_pager', :large => '--no-pager', :description => 'Disable pagination', :proc => lambda {|_o, | ENV['ONE_PAGER'] = 'cat' if File.exist?('/bin/cat') } }
- ADJUST =
{ :name => 'adjust', :large => '--adjust x,y,z', :format => Array, :description => 'Adjust size to not truncate selected columns' }
- SIZE =
{ :name => 'size', :short => '-s x=size,y=size', :large => '--size x=size,y=size', :format => Array, :description => 'Change the size of selected columns. ' \ 'For example: ' \ '$ onevm list --size "name=20" ' \ 'will make column name size 20.' }
- EXPAND =
{ :name => 'expand', :large => '--expand [x=prop,y=prop]', :format => Array, :description => 'Expands the columns size to fill the terminal. ' \ 'For example: ' \ '$onevm list --expand name=0.4,group=0.6 ' \ 'will expand name 40% and group 60%. ' \ '$onevm list --expand name,group ' \ 'will expand name and group based on its size.' \ '$onevm list --expand will expand all columns.' }
- NO_EXPAND =
{ :name => 'no_expand', :large => '--no-expand', :description => 'Disable expand' }
- OPTIONS =
[LIST, LISTCONF, DELAY, FILTER, OPERATOR, CSV_OPT, CSV_DEL, NO_PAGER, NO_HEADER, ADJUST, SIZE, EXPAND, NO_EXPAND]
- ANSI_RED =
CLI state colors
"\33[31m"- ANSI_GREEN =
"\33[32m"- ANSI_RESET =
"\33[0m"- ANSI_YELLOW =
"\33[33m"- OK_STATES =
CLI states
['runn', 'rdy', 'on', 'SUCCESS', 'RUNNING']
- BAD_STATES =
['fail', 'err', 'error', 'ERROR', 'FAILED_DEPLOYING', 'FAILED_DEPLOYING_NETS', 'FAILED_UNDEPLOYING', 'FAILED_UNDEPLOYING_NETS', 'FAILED_SCALING']
- REGULAR_STATES =
['PENDING', 'DEPLOYING', 'DEPLOYING_NETS', 'UNDEPLOYING', 'UNDEPLOYING_NETS', 'CONFIGURING', 'WARNING']
Class Method Summary collapse
-
.base64?(value) ⇒ Boolean
Check if value is in base64.
-
.color_state(state) ⇒ Object
Set state color.
-
.fail(message) ⇒ Object
Show error message and exit with error.
-
.green(text) ⇒ Object
Get text in green colour.
-
.print_header(str, underline = true) ⇒ Object
Print header.
-
.print_tty_header(str, underline = true) ⇒ Object
Print pretty header.
-
.render_html(text) ⇒ Object
Render a small HTML subset for terminal output Supports: p, br, ul/ol/li, strong/b, em/i, code, a.
-
.render_md(text) ⇒ Object
Render a small Markdown subset for terminal output.
-
.scr_bold ⇒ Object
Sets bold font.
-
.scr_cls ⇒ Object
Clears screen.
-
.scr_move(cord_x, cord_y) ⇒ Object
Moves the cursor.
-
.scr_restore ⇒ Object
Restore normal font.
-
.scr_underline ⇒ Object
Sets underline.
Class Method Details
.base64?(value) ⇒ Boolean
Check if value is in base64
425 426 427 428 429 |
# File 'lib/cli_helper.rb', line 425 def self.base64?(value) re = %r(^([A-Za-z0-9+\/]{4})*([A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)?$) !value.match(re).nil? end |
.color_state(state) ⇒ Object
Set state color
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/cli_helper.rb', line 206 def self.color_state(state) if $stdout.tty? case state.strip when *OK_STATES ANSI_GREEN + state + ANSI_RESET when *BAD_STATES ANSI_RED + state + ANSI_RESET when *REGULAR_STATES ANSI_YELLOW + state + ANSI_RESET else state end else state end end |
.fail(message) ⇒ Object
Show error message and exit with error
262 263 264 265 266 |
# File 'lib/cli_helper.rb', line 262 def self.fail() STDERR.puts exit(-1) end |
.green(text) ⇒ Object
Get text in green colour
226 227 228 229 230 231 232 |
# File 'lib/cli_helper.rb', line 226 def self.green(text) if $stdout.tty? ANSI_GREEN + text + ANSI_RESET else text end end |
.print_header(str, underline = true) ⇒ Object
Print header
238 239 240 241 242 243 244 245 246 |
# File 'lib/cli_helper.rb', line 238 def self.print_header(str, underline = true) if $stdout.tty? print_tty_header(str, underline) else print str end puts end |
.print_tty_header(str, underline = true) ⇒ Object
Print pretty header
252 253 254 255 256 257 |
# File 'lib/cli_helper.rb', line 252 def self.print_tty_header(str, underline = true) scr_bold scr_underline if underline print str scr_restore end |
.render_html(text) ⇒ Object
Render a small HTML subset for terminal output Supports: p, br, ul/ol/li, strong/b, em/i, code, a
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/cli_helper.rb', line 270 def self.render_html(text) return '' if text.nil? || text.empty? use_ansi = $stdout.tty? bold_on = use_ansi ? "\33[1m" : '' faint_on = use_ansi ? "\33[2m" : '' reset = use_ansi ? "\33[0m" : '' mono_on = use_ansi ? "\33[36m" : '' bullet = use_ansi ? '•' : '*' # OSC 8 hyperlink escape (clickable links) osc8_link = lambda do |label, url| return "#{label} (#{url})" unless use_ansi "\e]8;;#{url}\a#{label}\e]8;;\a" end s = text.to_s.gsub("\r\n", "\n") # Block tags -> newlines s = s.gsub(%r{<\s*br\s*/?\s*>}i, "\n") s = s.gsub(%r{<\s*/\s*p\s*>}i, "\n") s = s.gsub(/<\s*p\b[^>]*>/i, '') s = s.gsub(%r{<\s*/\s*(div|section|article)\s*>}i, "\n\n") s = s.gsub(/<\s*(div|section|article)\b[^>]*>/i, '') # Links: <a href="url">label</a> s = s.gsub(%r{<\s*a\b[^>]*href\s*=\s*["']([^"']+)["'][^>]*>(.*?)<\s*/\s*a\s*>}im) do url = Regexp.last_match(1).strip label = Regexp.last_match(2).to_s osc8_link.call(label, url) end # Lists s = s.gsub(/<\s*li\b[^>]*>/i, "#{bullet} ") # Inline formatting if use_ansi s = s.gsub(%r{<\s*/\s*(strong|b)\s*>}i, reset) s = s.gsub(/<\s*(strong|b)\b[^>]*>/i, bold_on) s = s.gsub(%r{<\s*/\s*(em|i)\s*>}i, reset) s = s.gsub(/<\s*(em|i)\b[^>]*>/i, faint_on) # <code> s = s.gsub(%r{<\s*code\b[^>]*>(.*?)<\s*/\s*code\s*>}im) do "#{faint_on}#{mono_on}#{Regexp.last_match(1)}#{reset}" end else s = s.gsub(%r{<\s*/?\s*(strong|b|em|i|code)\b[^>]*>}i, '') end # Remove any remaining tags s = s.gsub(/<[^>]+>/, '') # Decode minimal HTML entities s = s.gsub('&', '&') .gsub('<', '<') .gsub('>', '>') .gsub('"', '"') .gsub(''', "'") .gsub(' ', ' ') # Normalize whitespace s = s.lines.map(&:rstrip).join("\n") s.gsub(/\n{3,}/, "\n\n").strip end |
.render_md(text) ⇒ Object
Render a small Markdown subset for terminal output. Supports headings, bold, italic, inline code, and unordered lists.
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/cli_helper.rb', line 341 def self.render_md(text) return '' if text.nil? || text.empty? use_ansi = $stdout.tty? bold_on = use_ansi ? "\33[1m" : '' reset = use_ansi ? "\33[0m" : '' faint_on = use_ansi ? "\33[2m" : '' mono_on = use_ansi ? "\33[36m" : '' bullet = use_ansi ? '•' : '*' # OSC 8 hyperlink sequences osc8_link = lambda do |label, url| return "#{label} (#{url})" unless use_ansi "\e]8;;#{url}\a#{label}\e]8;;\a" end lines = text.to_s.gsub("\r\n", "\n").split("\n", -1) out = lines.map do |line| # Preserve empty lines if line.strip.empty? '' # Headings (#, ##, ###) elsif (m = line.match(/\A\s{0,3}(\#{1,3})\s+(.*)\z/)) title = m[2].strip "#{bold_on}#{title}#{reset}" # Unordered list items (*, -, +) elsif (m = line.match(/\A(\s*)[*+-]\s+(.*)\z/)) indent = m[1] item = m[2] "#{indent}#{bullet} #{item}" else line end end.join("\n") # Links [label](url) out = out.gsub(/\[([^\]]+)\]\(([^)]+)\)/) do label = Regexp.last_match(1) url = Regexp.last_match(2).strip osc8_link.call(label, url) end # Autolinks <https://example.com> out = out.gsub(/<((?:https?|mailto):[^>\s]+)>/) do url = Regexp.last_match(1) osc8_link.call(url, url) end # Inline formatting (simple, non-nested) if use_ansi # Inline code: `code` out = out.gsub(/`([^`]+)`/) do m = Regexp.last_match(1) "#{faint_on}#{mono_on}#{m}#{reset}" end # Bold: **text** out = out.gsub(/\*\*([^\*]+)\*\*/) do m = Regexp.last_match(1) "#{bold_on}#{m}#{reset}" end # Italic: *text* out = out.gsub(/(^|[^*])\*([^*\n]+)\*(?!\*)/) do m1 = Regexp.last_match(1) m2 = Regexp.last_match(2) "#{m1}#{faint_on}#{m2}#{reset}" end else # Non-tty, keep markdown mostly as-is, but normalize list bullets out = out.gsub(/\A(\s*)[*+-]\s+/m, "\\1#{bullet} ") end out end |
.scr_bold ⇒ Object
Sets bold font
151 152 153 |
# File 'lib/cli_helper.rb', line 151 def self.scr_bold print "\33[1m" end |
.scr_cls ⇒ Object
Clears screen
166 167 168 |
# File 'lib/cli_helper.rb', line 166 def self.scr_cls print "\33[2J\33[H" end |
.scr_move(cord_x, cord_y) ⇒ Object
Moves the cursor
174 175 176 |
# File 'lib/cli_helper.rb', line 174 def self.scr_move(cord_x, cord_y) print "\33[#{cord_x};#{cord_y}H" end |
.scr_restore ⇒ Object
Restore normal font
161 162 163 |
# File 'lib/cli_helper.rb', line 161 def self.scr_restore print "\33[0m" end |
.scr_underline ⇒ Object
Sets underline
156 157 158 |
# File 'lib/cli_helper.rb', line 156 def self.scr_underline print "\33[4m" end |