Module: CliTools
- Extended by:
- CliTools
- Included in:
- CliTools
- Defined in:
- lib/cli_tools.rb,
lib/cli_tools/system.rb,
lib/cli_tools/console.rb,
lib/cli_tools/version.rb
Defined Under Namespace
Classes: ShellExecutionError
Constant Summary collapse
- SH_STORED_OUTPUT_LIMIT =
Execs cmd in a shell and returns true if command executed successfully, or throws an exception if command failed.
2048
- SH_STORED_OUTPUT_LINES =
8
- VERSION =
"0.0.1"
Instance Method Summary collapse
-
#_sh_capture_concat(output_buffer, text) ⇒ Object
Concatenates output buffer and text.
-
#_sh_captured_output ⇒ Object
Returns list of capture channels.
-
#copy_if_newer(from, to, echo = true) ⇒ Object
Copy file if newer.
-
#esc_blue(text) ⇒ Object
Returns ANSI escaped string for the blue colored text.
-
#esc_bold(text) ⇒ Object
Returns ANSI escaped string for the bold text.
-
#esc_green(text) ⇒ Object
Returns ANSI escaped string for the green colored text.
-
#esc_red(text) ⇒ Object
Returns ANSI escaped string for the red colored text.
-
#esc_string(esc, text) ⇒ Object
Returns ANSI escaped string.
-
#esc_yellow(text) ⇒ Object
Returns ANSI escaped string for the yellow colored text.
-
#fs_disk_free(path) ⇒ Object
Returns the amount in bytes of free disk space in the specified folder.
-
#fs_disk_used(path) ⇒ Object
Returns the amount in bytes of used disk space in the specified folder.
-
#kb_getkey ⇒ Object
Returns pressed key or
nil
if there is no keyboard input. -
#kb_raw_no_echo_mode ⇒ Object
Switches the input mode to raw and disables echo.
-
#kb_restore_mode ⇒ Object
Restores a previously saved input mode.
-
#os_name ⇒ Object
Returns a host OS name in lowercase.
-
#put_beep ⇒ Object
Puts a ‘beep’ ANSI code.
-
#put_cls ⇒ Object
Clears console screen and puts cursor in 0,0.
-
#putr(str = '') ⇒ Object
Replaces current line in the output with a new one.
- #sh(cmd, echo = true, capture_output = nil, &block) ⇒ Object
-
#sh_capture_echo(message) ⇒ Object
Prints message and captures it if capture output is enabled.
-
#sh_capture_output(output, &block) ⇒ Object
Captures all outputs of all the #sh executed inside the block into
output
.
Instance Method Details
#_sh_capture_concat(output_buffer, text) ⇒ Object
Concatenates output buffer and text. If output buffer object is a String, the content is #replace-d, otherwise object must provide #text property-accessor.
82 83 84 85 86 87 88 |
# File 'lib/cli_tools/system.rb', line 82 def _sh_capture_concat( output_buffer, text ) if output_buffer.is_a? String output_buffer.replace( output_buffer + text ) else output_buffer.text += text end end |
#_sh_captured_output ⇒ Object
Returns list of capture channels. This list is local to the current thread.
93 94 95 96 |
# File 'lib/cli_tools/system.rb', line 93 def _sh_captured_output Thread.current[:sh_captured_output] ||= [] Thread.current[:sh_captured_output] end |
#copy_if_newer(from, to, echo = true) ⇒ Object
Copy file if newer.
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/cli_tools/system.rb', line 112 def copy_if_newer( from, to, echo = true ) # if Mac OS X: os = os_name() if os == 'macosx' sh "cp -nf #{from} #{to}", echo else # update, force, preserve attrs sh "cp -ufp #{from} #{to}", echo end end |
#esc_blue(text) ⇒ Object
Returns ANSI escaped string for the blue colored text.
60 61 62 |
# File 'lib/cli_tools/console.rb', line 60 def esc_blue( text ) esc_string "\e[34m", text end |
#esc_bold(text) ⇒ Object
Returns ANSI escaped string for the bold text.
66 67 68 |
# File 'lib/cli_tools/console.rb', line 66 def esc_bold( text ) esc_string "\e[01;37m", text end |
#esc_green(text) ⇒ Object
Returns ANSI escaped string for the green colored text.
48 49 50 |
# File 'lib/cli_tools/console.rb', line 48 def esc_green( text ) esc_string "\e[32m", text end |
#esc_red(text) ⇒ Object
Returns ANSI escaped string for the red colored text.
42 43 44 |
# File 'lib/cli_tools/console.rb', line 42 def esc_red( text ) esc_string "\e[31m", text end |
#esc_string(esc, text) ⇒ Object
Returns ANSI escaped string.
36 37 38 |
# File 'lib/cli_tools/console.rb', line 36 def esc_string( esc, text ) esc+text.to_s+"\e[0m" end |
#esc_yellow(text) ⇒ Object
Returns ANSI escaped string for the yellow colored text.
54 55 56 |
# File 'lib/cli_tools/console.rb', line 54 def esc_yellow( text ) esc_string "\e[33m", text end |
#fs_disk_free(path) ⇒ Object
Returns the amount in bytes of free disk space in the specified folder.
125 126 127 128 129 130 |
# File 'lib/cli_tools/system.rb', line 125 def fs_disk_free( path ) result = `df -P -k #{path}`.split("\n")[1] device, d_size, d_usage, d_free = result.split(" ")[0..3] d_free.to_i*1024 # in bytes end |
#fs_disk_used(path) ⇒ Object
Returns the amount in bytes of used disk space in the specified folder.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/cli_tools/system.rb', line 134 def fs_disk_used( path ) # First, dereference path if File.symlink? path p = File.readlink( path ) else p = path end result = `du -ks #{p} 2>/dev/null` size = 0 result.split("\n").each do |line| if m = /(\d+)\s+.*/.match(line) size += m[1].to_i*1024 end end size end |
#kb_getkey ⇒ Object
Returns pressed key or nil
if there is no keyboard input.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/cli_tools/console.rb', line 73 def kb_getkey kb_raw_no_echo_mode begin return $stdin.read_nonblock(1) rescue return nil ensure kb_restore_mode end end |
#kb_raw_no_echo_mode ⇒ Object
Switches the input mode to raw and disables echo.
WARNING: This method requires the external “stty” program!
Pasted from HighLine
gem.
91 92 93 94 |
# File 'lib/cli_tools/console.rb', line 91 def kb_raw_no_echo_mode @tty_state = `stty -g` system "stty raw -echo cbreak isig" end |
#kb_restore_mode ⇒ Object
Restores a previously saved input mode.
WARNING: This method requires the external “stty” program!
Pasted from HighLine
gem.
102 103 104 |
# File 'lib/cli_tools/console.rb', line 102 def kb_restore_mode system "stty #{@tty_state}" end |
#os_name ⇒ Object
Returns a host OS name in lowercase.
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/cli_tools/system.rb', line 153 def os_name name = `uname` rescue '' # Windows? name.chomp! case name when 'Darwin' then 'macosx' when 'Linux' then 'linux' else "unknown(#{name})" end end |
#put_beep ⇒ Object
Puts a ‘beep’ ANSI code.
22 23 24 25 |
# File 'lib/cli_tools/console.rb', line 22 def put_beep print "\a" $stdout.flush end |
#put_cls ⇒ Object
Clears console screen and puts cursor in 0,0.
29 30 31 32 |
# File 'lib/cli_tools/console.rb', line 29 def put_cls cls = `clear` putr cls end |
#putr(str = '') ⇒ Object
Replaces current line in the output with a new one.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/cli_tools/console.rb', line 5 def putr( str = '' ) # move cursor to beginning of line cr = "\r" # ANSI escape code to clear line from cursor to end of line # cf. http://en.wikipedia.org/wiki/ANSI_escape_code clear = "\e[0K" # reset lines reset = cr + clear print "#{reset}#{str}" $stdout.flush end |
#sh(cmd, echo = true, capture_output = nil, &block) ⇒ Object
10 11 12 13 14 15 16 17 18 19 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 58 |
# File 'lib/cli_tools/system.rb', line 10 def sh( cmd, echo = true, capture_output = nil, &block ) if _sh_captured_output.size == 0 && capture_output.nil? && block.nil? # simple form puts cmd if echo system cmd or raise ShellExecutionError.new("Failed to execute: '#{cmd}' (#{$?})", $?, '') return true end # ok, it's a bit more complex exit_status = nil current_output = '' if echo _sh_captured_output.each do |output_channel| _sh_capture_concat output_channel, cmd+"\n" end if capture_output _sh_capture_concat capture_output, cmd+"\n" else puts cmd end end i = STDIN Open3.popen2e( cmd ) do |i, oe, t| i = STDIN oe.sync = true while oe_char = oe.getc do # puts "sh: block tick" _sh_captured_output.each do |output_channel| _sh_capture_concat output_channel, oe_char end if capture_output _sh_capture_concat capture_output, oe_char else putc oe_char end current_output += oe_char if current_output.size > SH_STORED_OUTPUT_LIMIT # squeeze cached output current_output = current_output.split("\n").last(SH_STORED_OUTPUT_LINES).join("\n") end yield(oe_char) if block end exit_status = t.value end if exit_status != 0 raise ShellExecutionError.new("Failed to execute: '#{cmd}' (#{exit_status})", exit_status, current_output) end true end |
#sh_capture_echo(message) ⇒ Object
Prints message and captures it if capture output is enabled.
72 73 74 75 76 |
# File 'lib/cli_tools/system.rb', line 72 def sh_capture_echo( ) _sh_captured_output.each do |output_channel| _sh_capture_concat output_channel, +"\n" end end |
#sh_capture_output(output, &block) ⇒ Object
Captures all outputs of all the #sh executed inside the block into output
.
63 64 65 66 67 68 |
# File 'lib/cli_tools/system.rb', line 63 def sh_capture_output( output, &block ) _sh_captured_output << output yield ensure _sh_captured_output.delete output end |