Class: MultishItem
- Inherits:
-
Object
- Object
- MultishItem
- Defined in:
- lib/multish.rb
Constant Summary collapse
- COLORS =
%i[black red green yellow blue magenta cyan white].freeze
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
Instance Method Summary collapse
- #color_code ⇒ Object
- #color_print(window, input) ⇒ Object
- #create_window! ⇒ Object
- #errored? ⇒ Boolean
- #finished? ⇒ Boolean
- #height ⇒ Object
-
#initialize(command, index, count) ⇒ MultishItem
constructor
A new instance of MultishItem.
- #left ⇒ Object
- #open_process! ⇒ Object
- #parse_commands(string) ⇒ Object
- #parse_string(string) {|[:string, chars]| ... } ⇒ Object
- #print(text) ⇒ Object
- #print_output! ⇒ Object
- #streams ⇒ Object
- #top ⇒ Object
- #try_update(fd) ⇒ Object
- #update_title! ⇒ Object
- #width ⇒ Object
- #window_title ⇒ Object
Constructor Details
#initialize(command, index, count) ⇒ MultishItem
Returns a new instance of MultishItem.
116 117 118 119 120 121 |
# File 'lib/multish.rb', line 116 def initialize(command, index, count) @command = command @index = index @count = count @output = '' end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
114 115 116 |
# File 'lib/multish.rb', line 114 def command @command end |
Instance Method Details
#color_code ⇒ Object
146 147 148 149 150 151 152 153 154 |
# 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) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# 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! ⇒ Object
139 140 141 142 143 144 |
# 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? ⇒ Boolean
156 157 158 |
# File 'lib/multish.rb', line 156 def errored? @exit_code && @exit_code != 0 end |
#finished? ⇒ Boolean
280 281 282 283 284 285 286 287 288 |
# 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 ⇒ Object
127 128 129 |
# File 'lib/multish.rb', line 127 def height Window.screen_height end |
#left ⇒ Object
131 132 133 |
# File 'lib/multish.rb', line 131 def left width * @index end |
#open_process! ⇒ Object
173 174 175 176 |
# File 'lib/multish.rb', line 173 def open_process! @stdin, @stdout, @stderr, @wait_thr = Open3.popen3(@command) @stdin.close end |
#parse_commands(string) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# 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 || = .to_i case when 1 yield [:bold] when 30..37 color = COLORS[ - 30] yield [:color, color] end end end when :error yield [:error, arg] end end end |
#parse_string(string) {|[:string, chars]| ... } ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# 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) ⇒ Object
189 190 191 192 193 |
# File 'lib/multish.rb', line 189 def print(text) @output << text color_print(@window, text) @window.refresh! end |
#print_output! ⇒ Object
290 291 292 293 |
# File 'lib/multish.rb', line 290 def print_output! warn window_title.red warn @output end |
#streams ⇒ Object
178 179 180 |
# File 'lib/multish.rb', line 178 def streams [@stdout, @stderr] end |
#top ⇒ Object
135 136 137 |
# File 'lib/multish.rb', line 135 def top 0 end |
#try_update(fd) ⇒ Object
182 183 184 185 186 187 |
# 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! ⇒ Object
160 161 162 163 164 165 166 167 |
# 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 ⇒ Object
123 124 125 |
# File 'lib/multish.rb', line 123 def width (Window.screen_width / @count).floor end |
#window_title ⇒ Object
169 170 171 |
# File 'lib/multish.rb', line 169 def window_title finished? ? "[ #{command} ] -> #{@exit_code}" : "$ #{command}" end |