Class: Macdiff::Command
- Inherits:
-
Object
- Object
- Macdiff::Command
- Extended by:
- Forwardable
- Defined in:
- lib/macdiff/command.rb
Overview
Base command class
Instance Attribute Summary collapse
-
#pastel ⇒ Object
readonly
Returns the value of attribute pastel.
Instance Method Summary collapse
- #clear_screen ⇒ Object
-
#command(**options) ⇒ Object
The external commands runner.
-
#config ⇒ Object
Commandlet Configuration.
-
#config_fetch(key) ⇒ Object
(also: #get)
Configuration fetch key/value.
-
#config_set(key, value) ⇒ Object
(also: #set)
Configuration set key/value.
-
#cursor ⇒ Object
The cursor movement.
-
#editor ⇒ Object
Open a file or text in the user’s preferred editor.
-
#exec_exist?(*args) ⇒ Boolean
Check if executable exists.
-
#execute ⇒ Object
Execute this command.
-
#generator ⇒ Object
File manipulation utility methods.
- #heading(heading, size = 70) ⇒ Object
-
#initialize(options) ⇒ Command
constructor
A new instance of Command.
-
#kv(key, value, key_width = 30) ⇒ Object
kv - Print Key/Value.
- #line(size = 70, character = '=') ⇒ Object (also: #l)
-
#pager(**options) ⇒ Object
Terminal output paging.
-
#platform ⇒ Object
Terminal platform and OS properties.
- #pretty_print(obj) ⇒ Object
-
#pretty_table(heading, column_headings, column_values) ⇒ Object
Print a pretty table.
- #print_all(data) ⇒ Object
-
#prompt(**options) ⇒ Object
The interactive prompt.
-
#screen ⇒ Object
Get terminal screen properties.
-
#section_heading(heading, size = 70) ⇒ Object
A section heading.
- #subheading(heading, size = 70) ⇒ Object
-
#which(*args) ⇒ Object
The unix which utility.
Constructor Details
#initialize(options) ⇒ Command
Returns a new instance of Command.
17 18 19 20 21 |
# File 'lib/macdiff/command.rb', line 17 def initialize() @options = @pastel = Pastel.new config end |
Instance Attribute Details
#pastel ⇒ Object (readonly)
Returns the value of attribute pastel.
12 13 14 |
# File 'lib/macdiff/command.rb', line 12 def pastel @pastel end |
Instance Method Details
#clear_screen ⇒ Object
159 160 161 |
# File 'lib/macdiff/command.rb', line 159 def clear_screen puts cursor.clear_screen end |
#command(**options) ⇒ Object
The external commands runner
144 145 146 147 |
# File 'lib/macdiff/command.rb', line 144 def command(**) require 'tty-command' TTY::Command.new() end |
#config ⇒ Object
Commandlet Configuration
28 29 30 31 32 33 34 |
# File 'lib/macdiff/command.rb', line 28 def config config = Macdiff::App.config # @my_config_setting = config.fetch(:my_config_setting) config end |
#config_fetch(key) ⇒ Object Also known as: get
Configuration fetch key/value
example:
get(:name) ==> 'David'
52 53 54 55 |
# File 'lib/macdiff/command.rb', line 52 def config_fetch(key) config.read config.fetch(key) end |
#config_set(key, value) ⇒ Object Also known as: set
Configuration set key/value
example:
set(:name, 'David')
40 41 42 43 44 45 |
# File 'lib/macdiff/command.rb', line 40 def config_set(key, value) config.read config.set(key, value: value) config.write force: true value end |
#cursor ⇒ Object
The cursor movement
154 155 156 157 |
# File 'lib/macdiff/command.rb', line 154 def cursor require 'tty-cursor' TTY::Cursor end |
#editor ⇒ Object
Open a file or text in the user’s preferred editor
168 169 170 171 |
# File 'lib/macdiff/command.rb', line 168 def editor require 'tty-editor' TTY::Editor end |
#exec_exist?(*args) ⇒ Boolean
Check if executable exists
238 239 240 241 |
# File 'lib/macdiff/command.rb', line 238 def exec_exist?(*args) require 'tty-which' TTY::Which.exist?(*args) end |
#execute ⇒ Object
Execute this command
132 133 134 135 136 137 |
# File 'lib/macdiff/command.rb', line 132 def execute(*) raise( NotImplementedError, "#{self.class}##{__method__} must be implemented" ) end |
#generator ⇒ Object
File manipulation utility methods
178 179 180 181 |
# File 'lib/macdiff/command.rb', line 178 def generator require 'tty-file' TTY::File end |
#heading(heading, size = 70) ⇒ Object
73 74 75 76 77 |
# File 'lib/macdiff/command.rb', line 73 def heading(heading, size = 70) line(size) puts heading line(size) end |
#kv(key, value, key_width = 30) ⇒ Object
kv - Print Key/Value
63 64 65 |
# File 'lib/macdiff/command.rb', line 63 def kv(key, value, key_width = 30) puts "#{pastel.green(key.ljust(key_width))}: #{value}" end |
#line(size = 70, character = '=') ⇒ Object Also known as: l
67 68 69 70 |
# File 'lib/macdiff/command.rb', line 67 def line(size = 70, character = '=') result = character * size puts pastel.green(result) end |
#pager(**options) ⇒ Object
Terminal output paging
188 189 190 191 |
# File 'lib/macdiff/command.rb', line 188 def pager(**) require 'tty-pager' TTY::Pager.new() end |
#platform ⇒ Object
Terminal platform and OS properties
198 199 200 201 |
# File 'lib/macdiff/command.rb', line 198 def platform require 'tty-platform' TTY::Platform.new end |
#pretty_print(obj) ⇒ Object
95 96 97 |
# File 'lib/macdiff/command.rb', line 95 def pretty_print(obj) puts JSON.pretty_generate obj end |
#pretty_table(heading, column_headings, column_values) ⇒ Object
Print a pretty table
Example:
values = [
['less', which('less')],
['ruby', which('ruby')]
]
pretty_table(‘Which paths’, %w[key path], values)
117 118 119 120 121 122 123 |
# File 'lib/macdiff/command.rb', line 117 def pretty_table(heading, column_headings, column_values) heading heading require 'tty-table' table = TTY::Table.new column_headings, column_values puts table.render(:unicode, multiline: true, resize: true) end |
#print_all(data) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/macdiff/command.rb', line 99 def print_all(data) keys = data.first.keys data.each do |row| keys.each do |key| puts "#{key.to_s.rjust(20)}: #{row[key].to_s.delete("\n")[1..100]}" end puts '-' * 120 end end |
#prompt(**options) ⇒ Object
The interactive prompt
208 209 210 211 |
# File 'lib/macdiff/command.rb', line 208 def prompt(**) require 'tty-prompt' TTY::Prompt.new() end |
#screen ⇒ Object
Get terminal screen properties
218 219 220 221 |
# File 'lib/macdiff/command.rb', line 218 def screen require 'tty-screen' TTY::Screen end |
#section_heading(heading, size = 70) ⇒ Object
A section heading
example: [ I am a heading ]—————————————————-
89 90 91 92 93 |
# File 'lib/macdiff/command.rb', line 89 def section_heading(heading, size = 70) section_heading = "[ #{heading} ]#{'-' * (size - heading.length)}" puts pastel.green(section_heading) end |
#subheading(heading, size = 70) ⇒ Object
79 80 81 82 83 |
# File 'lib/macdiff/command.rb', line 79 def subheading(heading, size = 70) line(size, '-') puts heading line(size, '-') end |
#which(*args) ⇒ Object
The unix which utility
228 229 230 231 |
# File 'lib/macdiff/command.rb', line 228 def which(*args) require 'tty-which' TTY::Which.which(*args) end |