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

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/howzit/task.rb', line 23

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? ? nil : attributes[:title].to_s
  @parent = attributes[:parent] || nil

  @action = attributes[:action].render_arguments || nil

  @optional = optional
  @default = default
  @last_status = nil
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



8
9
10
# File 'lib/howzit/task.rb', line 8

def action
  @action
end

#argumentsObject (readonly)

Returns the value of attribute arguments.



8
9
10
# File 'lib/howzit/task.rb', line 8

def arguments
  @arguments
end

#defaultObject (readonly)

Returns the value of attribute default.



8
9
10
# File 'lib/howzit/task.rb', line 8

def default
  @default
end

#last_statusObject (readonly)

Returns the value of attribute last_status.



8
9
10
# File 'lib/howzit/task.rb', line 8

def last_status
  @last_status
end

#optionalObject (readonly)

Returns the value of attribute optional.



8
9
10
# File 'lib/howzit/task.rb', line 8

def optional
  @optional
end

#parentObject (readonly)

Returns the value of attribute parent.



8
9
10
# File 'lib/howzit/task.rb', line 8

def parent
  @parent
end

#titleObject (readonly)

Returns the value of attribute title.



8
9
10
# File 'lib/howzit/task.rb', line 8

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/howzit/task.rb', line 8

def type
  @type
end

Instance Method Details

#inspectString

Inspect



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

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

#runObject

Execute the task



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/howzit/task.rb', line 145

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)
            @last_status = 0
            true
          end
        end

  [output, tasks, res]
end

#run_blockObject

Execute a block type



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/howzit/task.rb', line 60

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

  update_last_status(res ? 0 : 1)
  res
end

#run_copyObject

Execute a copy task



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/howzit/task.rb', line 123

def run_copy
  # If a title was explicitly provided (different from action), always use it
  # Otherwise, use action (or respect show_all_code if no title)
  display_title = if @title && !@title.empty? && @title != @action
                    # Title was explicitly provided, use it
                    @title
                  elsif Howzit.options[:show_all_code]
                    # No explicit title, show code if requested
                    @action
                  else
                    # No explicit title, use title if available (might be same as action), otherwise action
                    @title && !@title.empty? ? @title : @action
                  end
  Howzit.console.info("#{@prefix}{bg}Copied {bw}#{display_title}{bg} to clipboard{x}".c)
  Util.os_copy(@action)
  @last_status = 0
  true
end

#run_includeArray

Execute an include task



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/howzit/task.rb', line 83

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)
  @last_status = nil
  [output, matches[0].tasks.count]
end

#run_runObject

Execute a run task



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/howzit/task.rb', line 100

def run_run
  # If a title was explicitly provided (different from action), always use it
  # Otherwise, use action (or respect show_all_code if no title)
  display_title = if @title && !@title.empty? && @title != @action
                    # Title was explicitly provided, use it
                    @title
                  elsif Howzit.options[:show_all_code]
                    # No explicit title, show code if requested
                    @action
                  else
                    # No explicit title, use title if available (might be same as action), otherwise action
                    @title && !@title.empty? ? @title : @action
                  end
  Howzit.console.info("#{@prefix}{bg}Running {bw}#{display_title}{x}".c)
  ENV['HOWZIT_SCRIPTS'] = File.expand_path('~/.config/howzit/scripts')
  res = system(@action)
  update_last_status(res ? 0 : 1)
  res
end

#to_listString

Output terminal-formatted list item



182
183
184
# File 'lib/howzit/task.rb', line 182

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

#to_sString

Output string representation



53
54
55
# File 'lib/howzit/task.rb', line 53

def to_s
  @title
end

#update_last_status(default = nil) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/howzit/task.rb', line 168

def update_last_status(default = nil)
  status = if defined?($CHILD_STATUS) && $CHILD_STATUS
             $CHILD_STATUS.exitstatus
           else
             default
           end
  @last_status = status
end