Class: KV::Screen
- Inherits:
-
Object
- Object
- KV::Screen
- Defined in:
- lib/kv.rb
Defined Under Namespace
Classes: RenderStatus
Constant Summary collapse
- LINE_ATTR =
Curses::A_DIM
- ANIMATION =
['[O ]', '[o. ]', '[... ]', '[ ... ]', '[ ... ]', '[ ...]', '[ .o]', '[ O]', '[ .o]', '[ ...]', '[ ... ]', '[ ... ]', '[ ... ]', '[... ]', '[o. ]', ]
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
Instance Method Summary collapse
- #cattr(attr) ⇒ Object
- #check_update ⇒ Object
- #control ⇒ Object
- #control_screen ⇒ Object
- #control_terminal ⇒ Object
- #ctimeout(ms) ⇒ Object
- #init_screen ⇒ Object
-
#initialize(input, lines: [], name: nil, search: nil, first_line: 0, following_mode: false, line_mode: false, separation_mode: false, time_stamp: nil, ext_input: nil, fifo_file: nil, watch: false, filter_command: nil) ⇒ Screen
constructor
A new instance of Screen.
- #input_str(pattern, str = ''.dup, other_actions: {}) ⇒ Object
- #key_name(ev) ⇒ Object
- #open_file ⇒ Object
- #pause ⇒ Object
- #read_async(input) ⇒ Object
- #redraw! ⇒ Object
- #render_data ⇒ Object
- #render_screen ⇒ Object
- #render_status ⇒ Object
- #screen_status(status, post = nil) ⇒ Object
- #search_next(start) ⇒ Object
- #search_next_move ⇒ Object
- #search_prev(start) ⇒ Object
- #search_str ⇒ Object
- #set_load_unlimited(b) ⇒ Object
- #setup_line(line) ⇒ Object
- #standout ⇒ Object
- #y ⇒ Object
- #y=(y) ⇒ Object
- #y_max ⇒ Object
Constructor Details
#initialize(input, lines: [], name: nil, search: nil, first_line: 0, following_mode: false, line_mode: false, separation_mode: false, time_stamp: nil, ext_input: nil, fifo_file: nil, watch: false, filter_command: nil) ⇒ Screen
Returns a new instance of Screen.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/kv.rb', line 32 def initialize input, lines: [], name: nil, search: nil, first_line: 0, following_mode: false, line_mode: false, separation_mode: false, time_stamp: nil, ext_input: nil, fifo_file: nil, watch: false, filter_command: nil @rs = RenderStatus.new @last_rs = nil @rs.y = first_line @rs.goto = first_line if first_line > 0 @rs.x = 0 @rs.last_lineno = 0 @rs.line_mode = line_mode @rs.search = search @rs.wrapping = false @rs.ts_mode = false @rs.separation_mode = separation_mode @name = name @filename = @name if @name && File.exist?(@name) @filter_command = @filename && filter_command @time_stamp = time_stamp @ext_input = ext_input @fifo_file = fifo_file @lines = lines @mode = :screen @watch_mode = watch @following = following_mode @apos = 0 @mouse = false @search_ignore_case = false @search_regexp = true @loading = false @buffer_lines = 10_000 @yq = Queue.new if @filename @load_unlimited = true else @load_unlimited = false end @prev_render = {} input = open_file if @filename @meta = input.respond_to?(:meta) ? input. : nil read_async input if input end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
176 177 178 |
# File 'lib/kv.rb', line 176 def x @x end |
Instance Method Details
#cattr(attr) ⇒ Object
217 218 219 220 221 222 223 224 |
# File 'lib/kv.rb', line 217 def cattr attr Curses.attron attr begin yield ensure Curses.attroff attr end end |
#check_update ⇒ Object
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/kv.rb', line 390 def check_update if @loading == false if @filename && File.mtime(@filename) > @file_mtime screen_status "#{@filename} is updated." if @watch_mode @lines = [] input = open_file read_async input else input = open(@filename) if input.size < @file_lastpos screen_status "#{@filename} is truncated. Rewinded." pause @lineno = 0 else input.seek @file_lastpos end read_async input end end end end |
#control ⇒ Object
756 757 758 759 760 761 762 763 764 765 |
# File 'lib/kv.rb', line 756 def control case @mode when :screen control_screen when :terminal control_terminal else raise end end |
#control_screen ⇒ Object
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 |
# File 'lib/kv.rb', line 536 def control_screen ev = render_screen case ev when 'q' raise PopScreen when Curses::KEY_UP, 'k' self.y -= 1 when Curses::KEY_DOWN, 'j' self.y += 1 when Curses::KEY_LEFT, 'h' self.x -= 1 when Curses::KEY_RIGHT, 'l' self.x += 1 when 'g' self.y = 0 self.x = 0 when 'G' self.y = self.y_max self.x = 0 when ' ', Curses::KEY_NPAGE, Curses::KEY_CTRL_D self.y += Curses.lines-1 when Curses::KEY_PPAGE, Curses::KEY_CTRL_U self.y -= Curses.lines-1 when /[0-9]/ screen_status "Goto:", ev ystr = input_str(/\d/, ev) if ystr && !ystr.empty? @rs.goto = ystr.to_i - 1 self.y = @rs.goto end when 'F' @following = true set_load_unlimited true when 'L' set_load_unlimited !@load_unlimited when '/' search_str = ''.dup update_search_status = -> do regexp = @search_regexp ? 'regexp' : 'string' ignore = @search_ignore_case ? '/ignore' : '' screen_status "Search[#{regexp}#{ignore}]:", search_str end update_search_status[] input_str(/./, search_str, other_actions: { Curses::KEY_CTRL_I => -> ev do @search_ignore_case = !@search_ignore_case update_search_status[] end, Curses::KEY_CTRL_R => -> ev do @search_regexp = !@search_regexp update_search_status[] end, }) if search_str && !search_str.empty? ic = @search_ignore_case ? [Regexp::IGNORECASE] : [] if @search_regexp begin @rs.search = Regexp.compile(search_str, *ic) rescue RegexpError => e @rs.search = nil screen_status "regexp compile error: #{e.}" pause end else @rs.search = Regexp.compile(Regexp.escape(search_str), *ic) end else @rs.search = nil end if @rs.search @rs.search.instance_variable_set(:@search_str, search_str) search_next self.y end when 'n' search_next self.y+1 if @rs.search when 'p' search_prev self.y-1 if @rs.search when 'f' if @rs.search filter_mode_title = "*filter mode [#{self.search_str}]*" if @name != filter_mode_title lines = @lines.grep(@rs.search) fscr = Screen.new nil, lines: lines, search: @rs.search, name: filter_mode_title raise PushScreen.new(fscr) end end when 's' screen_status "Save file:" file = input_str /./ begin if file && !file.empty? if File.exist? file screen_status "#{file.dump} exists. Override? [y/n] " yn = input_str(/[yn]/) if yn == 'y' File.write(file, @lines.join("\n")) else # do nothing end else File.write(file, @lines.join("\n")) end end rescue SystemCallError # TODO: status line end when 'v' if @filename syste m("vi #{@filename} +#{self.y + 1}") @last_rs = nil end when 'P' begin if v = `gist -v` and /^gist v\d/ =~ v screen_status "gist-ing..." url = IO.popen('gist -p', 'a+'){|rw| @lines.each{|line| rw.puts line} rw.close_write rw.read } msg = "gist URL: #{url}" at_exit{ puts msg } screen_status msg pause else raise v.inspect end rescue Errno::ENOENT screen_status 'gist command is not found' pause end when 'm' @mouse = !@mouse Curses.close_screen init_screen when Curses::KEY_MOUSE m = Curses.getmouse log m, "mouse ->" # log [m.bstate, m.x, m.y, m.z, m.eid] log @lines[self.y + m.y] when 'N' @rs.line_mode = !@rs.line_mode when 'T' @rs.ts_mode = !@rs.ts_mode if @time_stamp when 'S' @rs.separation_mode = !@rs.separation_mode @max_cols = [] when 't' Curses.close_screen @mode = :terminal when 'x' while @ext_input && !@ext_input.closed? update_ext_status = -> str do screen_status "input for ext:", str end update_ext_status[''] actions = { update: -> str do self.y = self.y_max render_data update_ext_status[str] end } str = input_str(/./, other_actions: actions) if str && !str.empty? @ext_input.puts str unless @ext_input.closed? else break end end when 'H' if @meta lines = @meta.map{|k, v| "#{k}: #{v}"} raise PushScreen.new(Screen.new nil, lines: lines, name: "Response header [#{@name}]") end when Curses::KEY_CTRL_G # do nothing when '?' raise PushScreen.new(Screen.new help_io) when nil # ignore when Curses::KEY_RESIZE # ignore else screen_status "unknown: #{key_name(ev)}" pause end end |
#control_terminal ⇒ Object
748 749 750 751 752 753 754 |
# File 'lib/kv.rb', line 748 def control_terminal @rs.instance_eval('binding').irb @mode = :screen init_screen @last_rs = nil end |
#ctimeout(ms) ⇒ Object
226 227 228 229 230 231 232 233 |
# File 'lib/kv.rb', line 226 def ctimeout ms Curses.timeout = ms begin yield ensure Curses.timeout = -1 end end |
#init_screen ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/kv.rb', line 192 def init_screen Curses.init_screen Curses.stdscr.keypad(true) if @mouse Curses.mousemask(Curses::BUTTON1_CLICKED | Curses::BUTTON2_CLICKED | Curses::BUTTON3_CLICKED | Curses::BUTTON4_CLICKED) else Curses.mousemask(0) end if @loading && self.y_max < @rs.y log [:going, self.y_max, @rs.y] @following = :going end self.y = @rs.y end |
#input_str(pattern, str = ''.dup, other_actions: {}) ⇒ Object
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/kv.rb', line 503 def input_str pattern, str = ''.dup, other_actions: {} update_action = other_actions[:update] ctimeout update_action ? 200 : -1 do loop do ev = Curses.getch case ev when 10 return str when Curses::KEY_BACKSPACE str.chop! when pattern str << ev when nil # timeout update_action[str] else if action = other_actions[ev] action.call(ev) else log "failure: #{key_name ev}" return nil end end end end end |
#key_name(ev) ⇒ Object
496 497 498 499 500 501 |
# File 'lib/kv.rb', line 496 def key_name ev Curses.constants.grep(/KEY/){|c| return c if Curses.const_get(c) == ev } ev end |
#open_file ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/kv.rb', line 91 def open_file input = open(@filename) @rs.last_lineno = 0 if @filter_command io = IO.popen("#{@filter_command} #{@filename}", err: '/dev/null') # io.close_write input = io end input end |
#pause ⇒ Object
531 532 533 534 |
# File 'lib/kv.rb', line 531 def pause ev = Curses.getch Curses.ungetch ev if ev end |
#read_async(input) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/kv.rb', line 110 def read_async input @loading = true begin data = input.read_nonblock(800_000) rescue IO::EAGAINWaitReadable, EOFError data = '' end last_line = nil data.each_line{|line| if line[-1] != "\n" last_line = line break end @lines << setup_line(line) } Thread.abort_on_exception = true @reader_thread = Thread.new do while line = input.gets if last_line line = last_line + line last_line = nil end @lines << setup_line(line) while !@load_unlimited && @lines.size > self.y + @buffer_lines @yq.pop; @yq.clear end @yq.clear end ensure if @filename @file_mtime = File.mtime(@filename) @file_lastpos = input.tell unless @filter_command elsif @fifo_file input = open(@fifo_file) log(input) redo end input.close @loading = false end end |
#redraw! ⇒ Object
767 768 769 |
# File 'lib/kv.rb', line 767 def redraw! @last_rs = nil end |
#render_data ⇒ Object
237 238 239 240 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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/kv.rb', line 237 def render_data # check update c_lines = @rs.c_lines = Curses.lines c_cols = @rs.c_cols = Curses.cols if @rs != @last_rs @last_rs = @rs.dup else return end Curses.clear if @rs.separation_mode && (lines = @lines[self.y ... (self.y + c_lines - 1)]) @max_cols = [] unless defined? @max_cols lines.each.with_index{|line, ln| line.split("\t").each_with_index{|w, i| @max_cols[i] = @max_cols[i] ? [@max_cols[i], w.size].max : w.size } } end (c_lines-1).times{|i| lno = i + self.y line = @lines[lno] cols = c_cols unless line if lno == @lines.size Curses.setpos i, 0 cattr LINE_ATTR do Curses.addstr '(END)' end end break end Curses.setpos i, 0 if @rs.line_mode cattr LINE_ATTR do lineno = line.instance_variable_get(:@lineno) ln_str = '%5d |' % lineno if @rs.goto == lineno - 1 || (@rs.search && (@rs.search === line)) standout do Curses.addstr(ln_str) end else Curses.addstr(ln_str) end cols -= ln_str.size end end if @rs.ts_mode && ts = line.instance_variable_get(:@time_stamp) cattr LINE_ATTR do ts = line.instance_variable_get(:@time_stamp) Curses.addstr("#{ts} |") end end line = line[self.x, cols] || '' if @rs.separation_mode line = line.split(/\t/).tap{|e| if (max = @max_cols.size) > 0 # fill empty columns e[max - 1] ||= nil end }.map.with_index{|w, i| "%-#{@max_cols[i]}s" % w }.join(' | ') end if !@rs.search || !(Regexp === @rs.search) || (parts = search_partition(line, @rs.search)).is_a?(String) Curses.addstr line else cattr Curses::A_UNDERLINE do parts.each{|(matched, str)| if matched == :match standout{ Curses.addstr str } else Curses.addstr str end } end end } end |
#render_screen ⇒ Object
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
# File 'lib/kv.rb', line 415 def render_screen ev = nil ms = @following ? 100 : 500 ctimeout ms do while ev == nil render_data render_status ev = Curses.getch check_update y_max = self.y_max if @following case @following when :searching break if search_next_move when :going if @rs.goto <= y_max self.y = @rs.goto break end when true # ok else raise "unknown following mode: #{@following}" end self.y = y_max end end @following = false set_load_unlimited false return ev end end |
#render_status ⇒ Object
343 344 345 346 347 348 349 350 |
# File 'lib/kv.rb', line 343 def render_status name = @name ? "<#{@name}>" : '' mouse = @mouse ? ' [MOUSE]' : '' search = @rs.search ? " search[#{search_str}]" : '' loading = @loading ? " (loading...#{@load_unlimited ? '!' : nil}#{@following ? ' following' : ''}) " : '' x = self.x > 0 ? " x:#{self.x}" : '' screen_status "#{name} lines:#{self.y+1}/#{@lines.size}#{x}#{loading}#{search}#{mouse}" end |
#screen_status(status, post = nil) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/kv.rb', line 369 def screen_status status, post = nil cols = Curses.cols line = Curses.lines-1 Curses.setpos line, 0 Curses.addstr ' '.ljust(cols) len = status.size len += post.size if post standout{ Curses.setpos Curses.lines-1, 0 Curses.addstr status } Curses.addstr post if post if !post && len < cols - ANIMATION.first.size Curses.setpos line, cols - ANIMATION.first.size - 1 @apos = (@apos + 1) % ANIMATION.size Curses.addstr ANIMATION[@apos] end end |
#search_next(start) ⇒ Object
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/kv.rb', line 469 def search_next start @searching = start if search_next_move # OK. self.y is updated. else if @loading set_load_unlimited true @following = :searching else screen_status "not found: [#{self.search_str}]" pause @searching = false end end end |
#search_next_move ⇒ Object
454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/kv.rb', line 454 def search_next_move last_line = @lines.size # log (@searching..last_line) (@searching...last_line).each{|i| if @rs.search === @lines[i] self.y = i @searching = false return true end } @searching = last_line return false end |
#search_prev(start) ⇒ Object
485 486 487 488 489 490 491 492 493 494 |
# File 'lib/kv.rb', line 485 def search_prev start start.downto(0){|i| if @rs.search === @lines[i] self.y = i return true end } screen_status "not found: [#{self.search_str}]" pause end |
#search_str ⇒ Object
331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/kv.rb', line 331 def search_str if @rs.search if str = @rs.search.instance_variable_get(:@search_str) str else @rs.search.inspect end else nil end end |
#set_load_unlimited(b) ⇒ Object
187 188 189 190 |
# File 'lib/kv.rb', line 187 def set_load_unlimited b @load_unlimited = b @yq << true end |
#setup_line(line) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/kv.rb', line 103 def setup_line line line = line.chomp line.instance_variable_set(:@time_stamp, Time.now.strftime('%H:%M:%S')) if @time_stamp line.instance_variable_set(:@lineno, @rs.last_lineno += 1) line end |
#standout ⇒ Object
211 212 213 214 215 |
# File 'lib/kv.rb', line 211 def standout cattr Curses::A_STANDOUT do yield end end |
#y ⇒ Object
161 162 163 |
# File 'lib/kv.rb', line 161 def y @rs.y end |
#y=(y) ⇒ Object
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/kv.rb', line 165 def y=(y) if y > (ym = self.y_max) @rs.y = ym else @rs.y = y end @rs.y = 0 if @rs.y < 0 @yq << nil if @loading end |
#y_max ⇒ Object
156 157 158 159 |
# File 'lib/kv.rb', line 156 def y_max max = @lines.size - Curses.lines + 2 max < 0 ? 0 : max end |