Class: Macdiff::Command

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/macdiff/command.rb

Overview

Base command class

Instance Attribute Summary collapse

Instance Method Summary collapse

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)
  @options = options
  @pastel = Pastel.new
  config
end

Instance Attribute Details

#pastelObject (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_screenObject



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(**options)
  require 'tty-command'
  TTY::Command.new(options)
end

#configObject

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

#cursorObject

The cursor movement



154
155
156
157
# File 'lib/macdiff/command.rb', line 154

def cursor
  require 'tty-cursor'
  TTY::Cursor
end

#editorObject

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

Returns:

  • (Boolean)

See Also:



238
239
240
241
# File 'lib/macdiff/command.rb', line 238

def exec_exist?(*args)
  require 'tty-which'
  TTY::Which.exist?(*args)
end

#executeObject

Execute this command

Raises:

  • (NotImplementedError)


132
133
134
135
136
137
# File 'lib/macdiff/command.rb', line 132

def execute(*)
  raise(
    NotImplementedError,
    "#{self.class}##{__method__} must be implemented"
  )
end

#generatorObject

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(**options)
  require 'tty-pager'
  TTY::Pager.new(options)
end

#platformObject

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


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(**options)
  require 'tty-prompt'
  TTY::Prompt.new(options)
end

#screenObject

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