Class: Teletype::Screen

Inherits:
Object
  • Object
show all
Defined in:
lib/teletype/screen.rb

Overview

Screen abstracts console printing, moving to specific row and column.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(height, width, verbose: false) ⇒ Screen

Returns a new instance of Screen.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/teletype/screen.rb', line 8

def initialize(height, width, verbose: false)
  @verbose = verbose
  @maxh, @maxw = $stdout.winsize

  if @maxh > height
    @height = height
    @top = (@maxh - height) / 2
  else
    @height = @maxh
    @top = 0
  end

  if @maxw > width
    @width = width
    @left = (@maxw - width) / 2
  else
    @width = @maxw
    @left = 0
  end

  at_exit { $stdout.clear_screen }
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



6
7
8
# File 'lib/teletype/screen.rb', line 6

def height
  @height
end

#leftObject

Returns the value of attribute left.



6
7
8
# File 'lib/teletype/screen.rb', line 6

def left
  @left
end

#topObject

Returns the value of attribute top.



6
7
8
# File 'lib/teletype/screen.rb', line 6

def top
  @top
end

#widthObject

Returns the value of attribute width.



6
7
8
# File 'lib/teletype/screen.rb', line 6

def width
  @width
end

Instance Method Details

#fill(lines) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/teletype/screen.rb', line 35

def fill(lines)
  invisible do
    $stdout.clear_screen

    lines.each_with_index do |line, index|
      $stdout.goto(@top + index, @left)
      $stdout.puts line
    end
  end
end

#invisibleObject



60
61
62
63
64
65
# File 'lib/teletype/screen.rb', line 60

def invisible
  $stdout.print "\e[?25l"
  yield
ensure
  $stdout.print "\e[?25h"
end

#log(*lines) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/teletype/screen.rb', line 46

def log(*lines)
  return unless @verbose

  original = $stdin.cursor
  invisible do
    lines.each_with_index do |line, index|
      $stdout.goto(@top + @height + 5 + index, @left)
      $stdout.print "\e[90;104m#{line}\e[0m"
      $stdout.erase_line(0)
    end
  end
  $stdout.cursor = original if original
end

#to(x, y) ⇒ Object



31
32
33
# File 'lib/teletype/screen.rb', line 31

def to(x, y)
  $stdout.goto(@top + y, @left + x)
end