Class: Howzit::Task
- Inherits:
-
Object
- Object
- Howzit::Task
- Defined in:
- lib/howzit/task.rb
Overview
Task object
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#optional ⇒ Object
readonly
Returns the value of attribute optional.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(attributes, optional: false, default: true) ⇒ Task
constructor
Initialize a Task object.
-
#inspect ⇒ String
Inspect.
-
#os_copy(string) ⇒ Object
Platform-agnostic copy-to-clipboard.
-
#os_open(command) ⇒ Object
Platform-agnostic open command.
-
#run ⇒ Object
Execute the task.
-
#run_block ⇒ Object
Execute a block type.
-
#run_copy ⇒ Object
Execute a copy task.
-
#run_include ⇒ Array
Execute an include task.
-
#run_run ⇒ Object
Execute a run task.
-
#to_list ⇒ String
Output terminal-formatted list item.
-
#to_s ⇒ String
Output string representation.
Constructor Details
#initialize(attributes, optional: false, default: true) ⇒ Task
Initialize a Task object
21 22 23 24 25 26 27 28 |
# File 'lib/howzit/task.rb', line 21 def initialize(attributes, optional: false, default: true) @type = attributes[:type] || :run @title = attributes[:title] || nil @action = attributes[:action].render_arguments || nil @parent = attributes[:parent] || nil @optional = optional @default = default end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
6 7 8 |
# File 'lib/howzit/task.rb', line 6 def action @action end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
6 7 8 |
# File 'lib/howzit/task.rb', line 6 def default @default end |
#optional ⇒ Object (readonly)
Returns the value of attribute optional.
6 7 8 |
# File 'lib/howzit/task.rb', line 6 def optional @optional end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
6 7 8 |
# File 'lib/howzit/task.rb', line 6 def parent @parent end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
6 7 8 |
# File 'lib/howzit/task.rb', line 6 def title @title end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/howzit/task.rb', line 6 def type @type end |
Instance Method Details
#inspect ⇒ String
Inspect
35 36 37 |
# File 'lib/howzit/task.rb', line 35 def inspect %(<#Howzit::Task @type=:#{@type} @title="#{@title}" @block?=#{@action.split(/\n/).count > 1}>) end |
#os_copy(string) ⇒ Object
Platform-agnostic copy-to-clipboard
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/howzit/task.rb', line 105 def os_copy(string) os = RbConfig::CONFIG['target_os'] out = "{bg}Copying {bw}#{string}".c case os when /darwin.*/i $stderr.puts "#{out} (macOS){x}".c if Howzit.[:log_level].zero? `echo #{Shellwords.escape(string)}'\\c'|pbcopy` when /mingw|mswin/i $stderr.puts "#{out} (Windows){x}".c if Howzit.[:log_level].zero? `echo #{Shellwords.escape(string)} | clip` else if 'xsel'.available? $stderr.puts "#{out} (Linux, xsel){x}".c if Howzit.[:log_level].zero? `echo #{Shellwords.escape(string)}'\\c'|xsel -i` elsif 'xclip'.available? $stderr.puts "#{out} (Linux, xclip){x}".c if Howzit.[:log_level].zero? `echo #{Shellwords.escape(string)}'\\c'|xclip -i` else $stderr.puts out if Howzit.[:log_level].zero? $stderr.puts 'Unable to determine executable for clipboard.' end end end |
#os_open(command) ⇒ Object
Platform-agnostic open command
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/howzit/task.rb', line 134 def os_open(command) os = RbConfig::CONFIG['target_os'] out = "{bg}Opening {bw}#{command}".c case os when /darwin.*/i Howzit.console.debug "#{out} (macOS){x}".c if Howzit.[:log_level] < 2 `open #{Shellwords.escape(command)}` when /mingw|mswin/i Howzit.console.debug "#{out} (Windows){x}".c if Howzit.[:log_level] < 2 `start #{Shellwords.escape(command)}` else if 'xdg-open'.available? Howzit.console.debug "#{out} (Linux){x}".c if Howzit.[:log_level] < 2 `xdg-open #{Shellwords.escape(command)}` else Howzit.console.debug out if Howzit.[:log_level] < 2 Howzit.console.debug 'Unable to determine executable for `open`.' end end end |
#run ⇒ Object
Execute the task
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/howzit/task.rb', line 158 def run output = [] tasks = 1 if @type == :block run_block else case @type when :include output, tasks = run_include when :run run_run when :copy run_copy when :open os_open(@action) end end [output, tasks] end |
#run_block ⇒ Object
Execute a block type
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/howzit/task.rb', line 51 def run_block Howzit.console.info "{bg}Running block {bw}#{@title}{x}".c if Howzit.[:log_level] < 2 block = @action script = Tempfile.new('howzit_script') begin script.write(block) script.close File.chmod(0o777, script.path) system(%(/bin/sh -c "#{script.path}")) ensure script.close script.unlink end end |
#run_copy ⇒ Object
Execute a copy task
94 95 96 97 98 |
# File 'lib/howzit/task.rb', line 94 def run_copy title = Howzit.[:show_all_code] ? @action : @title $stderr.puts "{bg}Copied {bw}#{title}{bg} to clipboard{x}".c if Howzit.[:log_level] < 2 os_copy(@action) end |
#run_include ⇒ Array
Execute an include task
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/howzit/task.rb', line 71 def run_include output = [] matches = Howzit.buildnote.find_topic(@action) raise "Topic not found: #{@action}" if matches.empty? $stderr.puts "{by}Running tasks from {bw}#{matches[0].title}{x}".c if Howzit.[:log_level] < 2 output.concat(matches[0].run(nested: true)) $stderr.puts "{by}End include: #{matches[0].tasks.count} tasks{x}".c if Howzit.[:log_level] < 2 [output, matches[0].tasks.count] end |
#run_run ⇒ Object
Execute a run task
85 86 87 88 89 |
# File 'lib/howzit/task.rb', line 85 def run_run title = Howzit.[:show_all_code] ? @action : @title $stderr.puts "{bg}Running {bw}#{title}{x}".c if Howzit.[:log_level] < 2 system(@action) end |
#to_list ⇒ String
Output terminal-formatted list item
184 185 186 |
# File 'lib/howzit/task.rb', line 184 def to_list " * #{@type}: #{@title.preserve_escapes}" end |
#to_s ⇒ String
Output string representation
44 45 46 |
# File 'lib/howzit/task.rb', line 44 def to_s @title end |