Class: Screen

Inherits:
Object show all
Defined in:
lib/selecta.rb

Defined Under Namespace

Classes: NotATTY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#ttyObject (readonly)

Returns the value of attribute tty.



566
567
568
# File 'lib/selecta.rb', line 566

def tty
  @tty
end

Class Method Details

.with_screenObject



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_ttyObject



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 expand_tabs(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

#heightObject



602
603
604
# File 'lib/selecta.rb', line 602

def height
  tty.winsize[0]
end

#newlineObject



614
615
616
# File 'lib/selecta.rb', line 614

def newline
  write_bytes("\n")
end

#restore_ttyObject



579
580
581
# File 'lib/selecta.rb', line 579

def restore_tty
  tty.stty("#{@original_stty_state}")
end

#suspendObject



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

#widthObject



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(expand_tabs(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