Class: MultishItem

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, index, count) ⇒ MultishItem

Returns a new instance of MultishItem.



12
13
14
15
16
17
# File 'lib/multish.rb', line 12

def initialize(command, index, count)
  @command = command
  @index = index
  @count = count
  @output = ''
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



10
11
12
# File 'lib/multish.rb', line 10

def command
  @command
end

Instance Method Details

#color_codeObject



42
43
44
45
46
47
48
49
50
# File 'lib/multish.rb', line 42

def color_code
  if !@wait_thr
    4
  elsif finished?
    errored? ? 3 : 2
  else # rubocop:disable Lint/DuplicateBranch
    4
  end
end

#color_print(window, input) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/multish.rb', line 89

def color_print(window, input)
  parse_commands(input) do |op, arg|
    case op
    when :string
      window.addstr(arg)
    when :reset
      window.attroff(Curses.color_pair(10) | Curses::A_BOLD)
    when :bold
      window.attron(Curses::A_BOLD)
    when :color
      Curses.init_pair(10, arg, BRIGHT_WHITE)
      window.attron(Curses.color_pair(10))
    when :error
      raise "ERROR: #{arg}"
    end
  end
end

#create_window!Object



35
36
37
38
39
40
# File 'lib/multish.rb', line 35

def create_window!
  @nav_window = Curses::Window.new(1, width - 1, top, left)
  @window = Curses::Window.new(height - 1, width - 1, top + 1, left)
  @window.scrollok(true)
  update_title!
end

#errored?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/multish.rb', line 52

def errored?
  @exit_code && @exit_code != 0
end

#finished?Boolean

Returns:

  • (Boolean)


173
174
175
176
177
178
179
180
181
# File 'lib/multish.rb', line 173

def finished?
  return false unless @wait_thr

  ret = !@wait_thr.alive?
  if ret && !@exit_code
    @exit_code = @wait_thr.value
  end
  ret
end

#heightObject



23
24
25
# File 'lib/multish.rb', line 23

def height
  Curses.lines
end

#leftObject



27
28
29
# File 'lib/multish.rb', line 27

def left
  width * @index
end

#open_process!Object



67
68
69
70
# File 'lib/multish.rb', line 67

def open_process!
  @stdin, @stdout, @stderr, @wait_thr = Open3.popen3(@command)
  @stdin.close
end

#parse_commands(string) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/multish.rb', line 107

def parse_commands(string)
  parse_string(string) do |op, arg|
    case op
    when :string
      yield [:string, arg]
    when :escape
      if arg == '[m'
        yield [:reset]
      elsif arg[/^\[(\d+;)+(\d+)m$/]
        args = ($1 + $2).split(';')
        args.each do |subarg|
          subarg = subarg.to_i
          case subarg
          when 1
            yield [:bold]
          when 30..37
            color = Curses::COLOR_BLACK + subarg - 30
            yield [:color, color]
          end
        end
      end
    when :error
      yield [:error, arg]
    end
  end
end

#parse_string(string) {|[:string, chars]| ... } ⇒ Object

Yields:

  • ([:string, chars])


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/multish.rb', line 134

def parse_string(string)
  len = string.length
  i = 0
  chars = ''
  while i < len
    char = string[i]
    if char == "\e"
      yield [:string, chars] if !chars.empty? && block_given?
      chars = ''
      escape = ''
      i += 1
      if string[i] == '['
        escape << string[i]
        i += 1
      else
        return yield [:error, string]
      end
      while string[i] =~ /[\x30-\x3f]/
        escape << string[i]
        i += 1
      end
      while string[i] =~ /[\x20–\x2f]/
        escape << string[i]
        i += 1
      end
      if string[i] =~ /[\x40-\x7e]/
        escape << string[i]
      else
        return yield [:error, string]
      end
      yield [:escape, escape] if block_given?
    else
      chars << char
    end
    i += 1
  end
  yield [:string, chars] if !chars.empty? && block_given?
end


83
84
85
86
87
# File 'lib/multish.rb', line 83

def print(text)
  @output << text
  color_print(@window, text)
  @window.refresh
end


183
184
185
186
# File 'lib/multish.rb', line 183

def print_output!
  warn window_title.red
  warn @output
end

#streamsObject



72
73
74
# File 'lib/multish.rb', line 72

def streams
  [@stdout, @stderr]
end

#topObject



31
32
33
# File 'lib/multish.rb', line 31

def top
  0
end

#try_update(fd) ⇒ Object

rubocop:disable Naming/MethodParameterName



76
77
78
79
80
81
# File 'lib/multish.rb', line 76

def try_update(fd) # rubocop:disable Naming/MethodParameterName
  return unless [@stdout, @stderr].include?(fd)

  line = fd.gets
  print(line) if line
end

#update_title!Object



56
57
58
59
60
61
# File 'lib/multish.rb', line 56

def update_title!
  @nav_window.setpos(0, 0)
  @nav_window.attron(Curses.color_pair(color_code) | Curses::A_REVERSE | Curses::A_BOLD)
  @nav_window.addstr(window_title.ljust(width - 1))
  @nav_window.refresh
end

#widthObject



19
20
21
# File 'lib/multish.rb', line 19

def width
  (Curses.cols / @count).floor
end

#window_titleObject



63
64
65
# File 'lib/multish.rb', line 63

def window_title
  finished? ? "[ #{command} ] -> #{@exit_code}" : "$ #{command}"
end