Class: Howzit::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/howzit/task.rb

Overview

Task object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, optional: false, default: true) ⇒ Task

Initialize a Task object

Parameters:

  • attributes (Hash)

    the task attributes

  • optional (Boolean) (defaults to: false)

    Task requires confirmation

  • default (Boolean) (defaults to: true)

    Default response for confirmation dialog

Options Hash (attributes):

  • :type (Symbol)

    task type (:block, :run, :include, :copy)

  • :title (String)

    task title

  • :action (String)

    task action

  • :parent (String)

    title of nested (included) topic origin



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

#actionObject (readonly)

Returns the value of attribute action.



6
7
8
# File 'lib/howzit/task.rb', line 6

def action
  @action
end

#defaultObject (readonly)

Returns the value of attribute default.



6
7
8
# File 'lib/howzit/task.rb', line 6

def default
  @default
end

#optionalObject (readonly)

Returns the value of attribute optional.



6
7
8
# File 'lib/howzit/task.rb', line 6

def optional
  @optional
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/howzit/task.rb', line 6

def parent
  @parent
end

#titleObject (readonly)

Returns the value of attribute title.



6
7
8
# File 'lib/howzit/task.rb', line 6

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/howzit/task.rb', line 6

def type
  @type
end

Instance Method Details

#inspectString

Inspect

Returns:



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

Parameters:

  • string (String)

    The string to copy



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.options[:log_level].zero?
    `echo #{Shellwords.escape(string)}'\\c'|pbcopy`
  when /mingw|mswin/i
    $stderr.puts "#{out} (Windows){x}".c if Howzit.options[:log_level].zero?
    `echo #{Shellwords.escape(string)} | clip`
  else
    if 'xsel'.available?
      $stderr.puts "#{out} (Linux, xsel){x}".c if Howzit.options[:log_level].zero?
      `echo #{Shellwords.escape(string)}'\\c'|xsel -i`
    elsif 'xclip'.available?
      $stderr.puts "#{out} (Linux, xclip){x}".c if Howzit.options[:log_level].zero?
      `echo #{Shellwords.escape(string)}'\\c'|xclip -i`
    else
      $stderr.puts out if Howzit.options[:log_level].zero?
      $stderr.puts 'Unable to determine executable for clipboard.'
    end
  end
end

#os_open(command) ⇒ Object

Platform-agnostic open command

Parameters:

  • command (String)

    The 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.options[:log_level] < 2
    `open #{Shellwords.escape(command)}`
  when /mingw|mswin/i
    Howzit.console.debug "#{out} (Windows){x}".c if Howzit.options[:log_level] < 2
    `start #{Shellwords.escape(command)}`
  else
    if 'xdg-open'.available?
      Howzit.console.debug "#{out} (Linux){x}".c if Howzit.options[:log_level] < 2
      `xdg-open #{Shellwords.escape(command)}`
    else
      Howzit.console.debug out if Howzit.options[:log_level] < 2
      Howzit.console.debug 'Unable to determine executable for `open`.'
    end
  end
end

#runObject

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_blockObject

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.options[: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_copyObject

Execute a copy task



94
95
96
97
98
# File 'lib/howzit/task.rb', line 94

def run_copy
  title = Howzit.options[:show_all_code] ? @action : @title
  $stderr.puts "{bg}Copied {bw}#{title}{bg} to clipboard{x}".c if Howzit.options[:log_level] < 2
  os_copy(@action)
end

#run_includeArray

Execute an include task

Returns:

  • (Array)

    [[Array] output, [Integer] number of tasks executed]



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.options[:log_level] < 2
  output.concat(matches[0].run(nested: true))
  $stderr.puts "{by}End include: #{matches[0].tasks.count} tasks{x}".c if Howzit.options[:log_level] < 2
  [output, matches[0].tasks.count]
end

#run_runObject

Execute a run task



85
86
87
88
89
# File 'lib/howzit/task.rb', line 85

def run_run
  title = Howzit.options[:show_all_code] ? @action : @title
  $stderr.puts "{bg}Running {bw}#{title}{x}".c if Howzit.options[:log_level] < 2
  system(@action)
end

#to_listString

Output terminal-formatted list item

Returns:

  • (String)

    List representation of the object.



184
185
186
# File 'lib/howzit/task.rb', line 184

def to_list
  "    * #{@type}: #{@title.preserve_escapes}"
end

#to_sString

Output string representation

Returns:

  • (String)

    string representation of the object.



44
45
46
# File 'lib/howzit/task.rb', line 44

def to_s
  @title
end