class MultishItem

Constants

COLORS

Attributes

command[R]

Public Class Methods

new(command, index, count) click to toggle source
# File lib/multish.rb, line 116
def initialize(command, index, count)
  @command = command
  @index = index
  @count = count
  @output = ''
end

Public Instance Methods

color_code() click to toggle source
# File lib/multish.rb, line 146
def color_code
  if !@wait_thr
    :yellow
  elsif finished?
    errored? ? :red : :green
  else # rubocop:disable Lint/DuplicateBranch
    :yellow
  end
end
color_print(window, input) click to toggle source
# File lib/multish.rb, line 195
def color_print(window, input)
  parse_commands(input) do |op, arg|
    case op
    when :string
      window << arg
    when :reset
      window.reset!
    when :bold
      window.bold = true
    when :color
      window.fgcolor = arg
    when :error
      raise "ERROR: #{arg}"
    end
  end
end
create_window!() click to toggle source
# File lib/multish.rb, line 139
def create_window!
  @nav_window = Window.new(1, width - 1, top, left)
  @window = Window.new(height - 1, width - 1, top + 1, left)
  @window.scrollok(true)
  update_title!
end
errored?() click to toggle source
# File lib/multish.rb, line 156
def errored?
  @exit_code && @exit_code != 0
end
finished?() click to toggle source
# File lib/multish.rb, line 280
def finished?
  return false unless @wait_thr

  ret = !@wait_thr.alive?
  if ret && !@exit_code
    @exit_code = @wait_thr.value
  end
  ret
end
height() click to toggle source
# File lib/multish.rb, line 127
def height
  Window.screen_height
end
left() click to toggle source
# File lib/multish.rb, line 131
def left
  width * @index
end
open_process!() click to toggle source
# File lib/multish.rb, line 173
def open_process!
  @stdin, @stdout, @stderr, @wait_thr = Open3.popen3(@command)
  @stdin.close
end
parse_commands(string) { |:string, arg| ... } click to toggle source
# File lib/multish.rb, line 214
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 = COLORS[subarg - 30]
            yield [:color, color]
          end
        end
      end
    when :error
      yield [:error, arg]
    end
  end
end
parse_string(string) { |:string, chars| ... } click to toggle source
# File lib/multish.rb, line 241
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
print(text) click to toggle source
print_output!() click to toggle source
streams() click to toggle source
# File lib/multish.rb, line 178
def streams
  [@stdout, @stderr]
end
top() click to toggle source
# File lib/multish.rb, line 135
def top
  0
end
try_update(fd) click to toggle source
# File lib/multish.rb, line 182
def try_update(fd)
  return unless [@stdout, @stderr].include?(fd)

  line = fd.gets
  print(line) if line
end
update_title!() click to toggle source
# File lib/multish.rb, line 160
def update_title!
  @nav_window.setpos(0, 0)
  @nav_window.fgcolor = color_code
  @nav_window.bgcolor = :black
  @nav_window.bold = true
  @nav_window << window_title.ljust(width - 1)
  @nav_window.refresh!
end
width() click to toggle source
# File lib/multish.rb, line 123
def width
  (Window.screen_width / @count).floor
end
window_title() click to toggle source
# File lib/multish.rb, line 169
def window_title
  finished? ? "[ #{command} ] -> #{@exit_code}" : "$ #{command}"
end