Class: Screen

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

Overview

A segment of the terminal screen.

BUG WARNING HACK FUCK Whenever I use @win.attrset/@win.setpos/@win.addch it doesn’t work at all. Apparently, when I do this, Curses::getch clears up the entire screen.

DO NOT DO THIS

Direct Known Subclasses

Popup

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, w, h) ⇒ Screen

Creates a Screen at ‘x` `y` `w` `h`.



18
19
20
21
22
# File 'lib/tabscroll/screen.rb', line 18

def initialize(x, y, w, h)
  @win = Curses::Window.new(h, w, y, x)
  @width  = w
  @height = h
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



15
16
17
# File 'lib/tabscroll/screen.rb', line 15

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



15
16
17
# File 'lib/tabscroll/screen.rb', line 15

def width
  @width
end

Instance Method Details

#background(char) ⇒ Object



108
109
110
111
# File 'lib/tabscroll/screen.rb', line 108

def background char
  @win.bkgd char
  @win.refresh
end

#box(horizontal = 0, vertical = 0) ⇒ Object

Sets the Screen border.

  • If all arguments are set, that’s ok.

  • If only the first 2 arguments are set, they are the vertical and horizontal chars.



119
120
121
122
# File 'lib/tabscroll/screen.rb', line 119

def box(horizontal=0, vertical=0)
  @win.box(horizontal, vertical)
  @win.refresh
end

#clearObject

Erases all of the Screen’s contents



77
78
79
# File 'lib/tabscroll/screen.rb', line 77

def clear
  @win.clear
end

#move(x, y) ⇒ Object

Moves window so that the upper-left corner is at ‘x` `y`.



87
88
89
# File 'lib/tabscroll/screen.rb', line 87

def move(x, y)
  @win.move(y, x)
end

#mvaddch(x, y, c, color = nil) ⇒ Object

Puts a character c on (x, y) with optional color.



38
39
40
41
42
43
44
45
46
# File 'lib/tabscroll/screen.rb', line 38

def mvaddch(x, y, c, color=nil)
  return if x < 0 or x >= @width
  return if y < 0 or y >= @height

  self.with_color color do
    Curses::setpos(@win.begy + y, @win.begx + x)
    Curses::addch c
  end
end

#mvaddstr(x, y, str, color = nil) ⇒ Object

Puts a string str on (x, y) with optional color.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tabscroll/screen.rb', line 49

def mvaddstr(x, y, str, color=nil)
  return if x < 0 or x >= @width
  return if y < 0 or y >= @height

  self.with_color color do
#      @win.setpos(@win.begy + y, @win.begx + x)
#      @win.addstr str
    Curses::setpos(@win.begy + y, @win.begx + x)
    Curses::addstr str
  end
end

#mvaddstr_center(y, str, color = nil) ⇒ Object

Puts a string str centered on y with optional color.



62
63
64
65
# File 'lib/tabscroll/screen.rb', line 62

def mvaddstr_center(y, str, color=nil)
  x = (@width/2) - (str.length/2)
  self.mvaddstr(x, y, str, color)
end

#mvaddstr_left(y, str, color = nil) ⇒ Object



67
68
69
# File 'lib/tabscroll/screen.rb', line 67

def mvaddstr_left(y, str, color=nil)
  self.mvaddstr(0, y, str, color)
end

#mvaddstr_right(y, str, color = nil) ⇒ Object



71
72
73
74
# File 'lib/tabscroll/screen.rb', line 71

def mvaddstr_right(y, str, color=nil)
  x = @width - str.length
  self.mvaddstr(x, y, str, color)
end

#refreshObject

Commits the changes on the Screen.



82
83
84
# File 'lib/tabscroll/screen.rb', line 82

def refresh
  @win.refresh
end

#resize(w, h) ⇒ Object

Resizes window to width and height.



92
93
94
95
96
# File 'lib/tabscroll/screen.rb', line 92

def resize(w, h)
  @win.resize(h, w)
  @width  = w
  @height = h
end

#set_color(color) ⇒ Object

Sets the current color of the Screen.



25
26
27
# File 'lib/tabscroll/screen.rb', line 25

def set_color color
  Curses::attrset(Curses::color_pair color)
end

#timeout(delay = -1)) ⇒ Object

Set block/nonblocking reads for window.

  • If ‘delay` is negative, blocking read is used.

  • If ‘delay` is zero, nonblocking read is used.

  • If ‘delay` is positive, waits for `delay` milliseconds and returns ERR of no input.



104
105
106
# File 'lib/tabscroll/screen.rb', line 104

def timeout(delay=-1)
  @win.timeout = delay
end

#with_color(color = nil) ⇒ Object

Executes a block of code encapsulated within a color on/off. Note that the color can be overrided.



31
32
33
34
35
# File 'lib/tabscroll/screen.rb', line 31

def with_color(color=nil)
  Curses::attron(Curses::color_pair color) if color
  yield
  Curses::attroff(Curses::color_pair color) if color
end