Class: Screen
Defined Under Namespace
Classes: NotATTY
Instance Attribute Summary collapse
-
#tty ⇒ Object
readonly
Returns the value of attribute tty.
Class Method Summary collapse
Instance Method Summary collapse
- #configure_tty ⇒ Object
- #cursor_up(lines) ⇒ Object
- #expand_tabs(string) ⇒ Object
- #height ⇒ Object
-
#initialize(tty) ⇒ Screen
constructor
A new instance of Screen.
- #newline ⇒ Object
- #restore_tty ⇒ Object
- #suspend ⇒ Object
- #width ⇒ Object
- #with_cursor_hidden(&block) ⇒ Object
- #write(text) ⇒ Object
- #write_bytes(bytes) ⇒ Object
Constructor Details
#initialize(tty) ⇒ Screen
Returns a new instance of Screen.
568 569 570 571 |
# File 'lib/selecta.rb', line 568 def initialize(tty) @tty = tty @original_stty_state = tty.stty("-g") end |
Instance Attribute Details
#tty ⇒ Object (readonly)
Returns the value of attribute tty.
566 567 568 |
# File 'lib/selecta.rb', line 566 def tty @tty end |
Class Method Details
.with_screen ⇒ Object
550 551 552 553 554 555 556 557 558 559 560 561 562 |
# File 'lib/selecta.rb', line 550 def self.with_screen SelectaTTY.with_tty do |tty| screen = self.new(tty) screen.configure_tty begin raise NotATTY if screen.height == 0 yield screen, tty ensure screen.restore_tty tty.puts end end end |
Instance Method Details
#configure_tty ⇒ Object
573 574 575 576 577 |
# File 'lib/selecta.rb', line 573 def configure_tty # -echo: terminal doesn't echo typed characters back to the terminal # -icanon: terminal doesn't interpret special characters (like backspace) tty.stty("raw -echo -icanon") end |
#cursor_up(lines) ⇒ Object
610 611 612 |
# File 'lib/selecta.rb', line 610 def cursor_up(lines) write_bytes(ANSI.cursor_up(lines)) end |
#expand_tabs(string) ⇒ Object
640 641 642 643 644 645 646 |
# File 'lib/selecta.rb', line 640 def (string) # Modified from http://markmail.org/message/avdjw34ahxi447qk tab_width = 8 string.gsub(/([^\t\n]*)\t/) do $1 + " " * (tab_width - ($1.size % tab_width)) end end |
#height ⇒ Object
602 603 604 |
# File 'lib/selecta.rb', line 602 def height tty.winsize[0] end |
#newline ⇒ Object
614 615 616 |
# File 'lib/selecta.rb', line 614 def newline write_bytes("\n") end |
#restore_tty ⇒ Object
579 580 581 |
# File 'lib/selecta.rb', line 579 def restore_tty tty.stty("#{@original_stty_state}") end |
#suspend ⇒ Object
583 584 585 586 587 588 589 590 591 |
# File 'lib/selecta.rb', line 583 def suspend restore_tty begin yield configure_tty rescue restore_tty end end |
#width ⇒ Object
606 607 608 |
# File 'lib/selecta.rb', line 606 def width tty.winsize[1] end |
#with_cursor_hidden(&block) ⇒ Object
593 594 595 596 597 598 599 600 |
# File 'lib/selecta.rb', line 593 def with_cursor_hidden(&block) write_bytes(ANSI.hide_cursor) begin block.call ensure write_bytes(ANSI.show_cursor) end end |
#write(text) ⇒ Object
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
# File 'lib/selecta.rb', line 618 def write(text) write_bytes(ANSI.clear_line) write_bytes("\r") text.components.each do |component| if component.is_a? String write_bytes((component)) elsif component == :inverse write_bytes(ANSI.inverse) elsif component == :reset write_bytes(ANSI.reset) else if component =~ /_/ fg, bg = component.to_s.split(/_/).map(&:to_sym) else fg, bg = component, :default end write_bytes(ANSI.color(fg, bg)) end end end |
#write_bytes(bytes) ⇒ Object
648 649 650 |
# File 'lib/selecta.rb', line 648 def write_bytes(bytes) tty.console_file.write(bytes) end |