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
29
30
31
32
33
34
# File 'lib/howzit/task.rb', line 21

def initialize(attributes, optional: false, default: true)
  @prefix = "{bw}\u{25B7}\u{25B7} {x}"
  # arrow = "{bw}\u{279F}{x}"
  @arguments = attributes[:arguments] || []

  @type = attributes[:type] || :run
  @title = attributes[:title] || nil
  @parent = attributes[:parent] || nil

  @action = attributes[:action].render_arguments || 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

#argumentsObject (readonly)

Returns the value of attribute arguments.



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

def arguments
  @arguments
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:



41
42
43
# File 'lib/howzit/task.rb', line 41

def inspect
  %(<#Howzit::Task @type=:#{@type} @title="#{@title}" @action="#{@action}" @arguments=#{@arguments} @block?=#{@action.split(/\n/).count > 1}>)
end

#runObject

Execute the task



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/howzit/task.rb', line 115

def run
  output = []
  tasks = 1
  res = if @type == :block
          run_block
        else
          case @type
          when :include
            output, tasks = run_include
          when :run
            run_run
          when :copy
            run_copy
          when :open
            Util.os_open(@action)
          end
        end

  [output, tasks, res]
end

#run_blockObject

Execute a block type



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/howzit/task.rb', line 57

def run_block
  Howzit.console.info "#{@prefix}{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)
    res = system(%(/bin/sh -c "#{script.path}"))
  ensure
    script.close
    script.unlink
  end

  res
end

#run_copyObject

Execute a copy task



105
106
107
108
109
110
# File 'lib/howzit/task.rb', line 105

def run_copy
  title = Howzit.options[:show_all_code] ? @action : @title
  Howzit.console.info("#{@prefix}{bg}Copied {bw}#{title}{bg} to clipboard{x}".c)
  Util.os_copy(@action)
  true
end

#run_includeArray

Execute an include task

Returns:

  • (Array)

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



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/howzit/task.rb', line 79

def run_include
  output = []
  action = @action

  matches = Howzit.buildnote.find_topic(action)
  raise "Topic not found: #{action}" if matches.empty?

  Howzit.console.info("#{@prefix}{by}Running tasks from {bw}#{matches[0].title}{x}".c)
  output.concat(matches[0].run(nested: true))
  Howzit.console.info("{by}End include: #{matches[0].tasks.count} tasks{x}".c)
  [output, matches[0].tasks.count]
end

#run_runObject

Execute a run task



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

def run_run
  title = Howzit.options[:show_all_code] ? @action : @title
  Howzit.console.info("#{@prefix}{bg}Running {bw}#{title}{x}".c)
  ENV['HOWZIT_SCRIPTS'] = File.expand_path('~/.config/howzit/scripts')
  system(@action)
end

#to_listString

Output terminal-formatted list item

Returns:

  • (String)

    List representation of the object.



141
142
143
# File 'lib/howzit/task.rb', line 141

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

#to_sString

Output string representation

Returns:

  • (String)

    string representation of the object.



50
51
52
# File 'lib/howzit/task.rb', line 50

def to_s
  @title
end