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.



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

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

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



7
8
9
# File 'lib/multish.rb', line 7

def command
  @command
end

Instance Method Details

#color_codeObject



38
39
40
41
42
43
44
45
46
# File 'lib/multish.rb', line 38

def color_code
  if !@wait_thr
    4
  elsif finished?
    errored? ? 3 : 2
  else
    4
  end
end

#create_window!Object



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

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)


48
49
50
# File 'lib/multish.rb', line 48

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

#finished?Boolean

Returns:

  • (Boolean)


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

def finished?
  return false unless @wait_thr
  ret = !@wait_thr.alive?
  if ret && !@exit_code
    @exit_code = @wait_thr.value
  end
  ret
end

#heightObject



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

def height
  Curses.lines
end

#leftObject



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

def left
  width * @index
end

#open_process!Object



60
61
62
63
# File 'lib/multish.rb', line 60

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


75
76
77
78
# File 'lib/multish.rb', line 75

def print(text)
  @window.addstr(text)
  @window.refresh
end

#streamsObject



65
66
67
# File 'lib/multish.rb', line 65

def streams
  [@stdout, @stderr]
end

#topObject



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

def top
  0
end

#try_update(fd) ⇒ Object



69
70
71
72
73
# File 'lib/multish.rb', line 69

def try_update(fd)
  return unless fd == @stdout || fd == @stderr
  line = fd.gets
  print(line) if line
end

#update_title!Object



52
53
54
55
56
57
58
# File 'lib/multish.rb', line 52

def update_title!
  @nav_window.setpos(0, 0)
  @nav_window.attron(Curses.color_pair(color_code) | Curses::A_REVERSE | Curses::A_BOLD)
  text = !finished? ? "$ #{command}" : "[ #{command} ] -> #{@exit_code}"
  @nav_window.addstr(text.ljust(width - 1))
  @nav_window.refresh
end

#widthObject



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

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