Class: RubyText::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/output.rb,
lib/window.rb,
lib/rubytext.rb,
lib/navigation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(high = nil, wide = nil, r0 = 1, c0 = 1, border = false, fg = nil, bg = nil) ⇒ Window

Returns a new instance of Window.



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

def initialize(high=nil, wide=nil, r0=1, c0=1, border=false, fg=nil, bg=nil)
  debug "RT::Win.init: #{[high, wide, r0, c0, border]}"
  @wide, @high, @r0, @c0 = wide, high, r0, c0
  @border, @fg, @bg      = border, fg, bg
  @win = X::Window.new(high, wide, r0, c0)
  debug "outer = #{@win.inspect}"
  debug "@border = #@border"
  debug "Calling 'colors': #{[@win, fg, bg]}"
  RubyText::Window.colors!(@win, fg, bg)
  if @border
    @win.box(Vert, Horiz)
    @outer = @win
    @outer.refresh
    debug "About to call again: params = #{[high-2, wide-2, r0+1, c0+1]}"
    @win = X::Window.new(high-2, wide-2, r0+1, c0+1) # , false, fg, bg)  # relative now??
    RubyText::Window.colors!(@win, fg, bg)
  else
    @outer = @win
  end
  @rows, @cols = @win.maxy, @win.maxx  # unnecessary really...
  @width, @height = @cols + 2, @rows + 2 if @border
  @win.refresh
end

Instance Attribute Details

#bg=(value) ⇒ Object (writeonly)

Sets the attribute bg

Parameters:

  • value

    the value to set the attribute bg to.



5
6
7
# File 'lib/window.rb', line 5

def bg=(value)
  @bg = value
end

#colsObject (readonly)

Returns the value of attribute cols.



4
5
6
# File 'lib/window.rb', line 4

def cols
  @cols
end

#fg=(sym) ⇒ Object (writeonly)

Sets the attribute fg

Parameters:

  • value

    the value to set the attribute fg to.



5
6
7
# File 'lib/window.rb', line 5

def fg=(value)
  @fg = value
end

#heightObject (readonly)

Returns the value of attribute height.



4
5
6
# File 'lib/window.rb', line 4

def height
  @height
end

#rowsObject (readonly)

Returns the value of attribute rows.



4
5
6
# File 'lib/window.rb', line 4

def rows
  @rows
end

#widthObject (readonly)

Returns the value of attribute width.



4
5
6
# File 'lib/window.rb', line 4

def width
  @width
end

#winObject (readonly)

Returns the value of attribute win.



4
5
6
# File 'lib/window.rb', line 4

def win
  @win
end

Class Method Details

.clear(win) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/output.rb', line 109

def self.clear(win)
  num = win.maxx * win.maxy
  win.setpos(0, 0)
  win.addstr(' '*num)
  win.setpos(0, 0)
  win.refresh
end

.colors(win, fg, bg) ⇒ Object



30
31
32
33
34
# File 'lib/window.rb', line 30

def self.colors(win, fg, bg)
  cfg, cbg, cp = fb2cp(fg, bg)
  X.init_pair(cp, cfg, cbg)
  win.color_set(cp|X::A_NORMAL)
end

.colors!(win, fg, bg) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/window.rb', line 36

def self.colors!(win, fg, bg)
  colors(win, fg, bg)
  num = win.maxx * win.maxy
  win.setpos(0, 0)
  win.addstr(' '*num)
  win.setpos(0, 0)
  win.refresh
end

.main(fg: nil, bg: nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/window.rb', line 45

def self.main(fg: nil, bg: nil)
  @main_win = X.init_screen
  X.start_color
  colors!(@main_win, fg, bg)
  rows, cols = @main_win.maxy, @main_win.maxx
  @screen = self.make(@main_win, rows, cols, 0, 0, false,
                      fg: fg, bg: bg)
# FIXME Why is this hard to inline?
#     @win = @main_win
#     obj = self.allocate
#     obj.instance_eval do 
#       @outer = @win = @main_win
#       @wide, @high, @r0, @c0 = cols, rows, 0, 0
#       @fg, @bg = fg, bg
#       @border = false
#       @rows, @cols = @high, @wide
#       @width, @height = @cols + 2, @rows + 2 if @border
#     end
#     @win = @main_win  # FIXME?
#     obj
  @screen
end

.make(cwin, high, wide, r0, c0, border, fg: :white, bg: :black) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/window.rb', line 68

def self.make(cwin, high, wide, r0, c0, border, fg: :white, bg: :black)
  obj = self.allocate
  obj.instance_eval do 
    debug "  Inside instance_eval..."
    @outer = @win = cwin
    @wide, @high, @r0, @c0 = wide, high, r0, c0
    @fg, @bg = fg, bg
    @border = border
    @rows, @cols = high, wide
    @width, @height = @cols + 2, @rows + 2 if @border
  end
  obj
end

.wockaObject



86
87
88
89
# File 'lib/window.rb', line 86

def self.wocka
  STDSCR.puts "HELLO!!!!!!"
  sleep 5
end

Instance Method Details

#[](r, c) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/output.rb', line 132

def [](r, c)
  save = self.rc
  @win.setpos r, c
  ch = @win.inch
  @win.setpos *save
  ch.chr
#   go(r, c) { ch = @win.inch }
end

#[]=(r, c, char) ⇒ Object



141
142
143
144
145
# File 'lib/output.rb', line 141

def []=(r, c, char)
  @win.setpos(r, c)
  @win.addch(char[0])
  @win.refresh
end

#bottomObject



36
37
38
39
40
# File 'lib/navigation.rb', line 36

def bottom 
  r, c = rc
  rmax = self.rows - 1
  go rmax, c
end

#boxmeObject



147
148
149
150
# File 'lib/output.rb', line 147

def boxme
  @outer.box(Vert, Horiz)
  @outer.refresh
end

#center(str) ⇒ Object



36
37
38
39
40
41
# File 'lib/output.rb', line 36

def center(str)
  r, c = self.rc
  n = @win.maxx - str.length
  go r, n/2
  puts str
end

#clearObject



117
118
119
120
121
122
123
124
# File 'lib/output.rb', line 117

def clear
  win = @win
  num = win.maxx * win.maxy
  win.setpos(0, 0)
  win.addstr(' '*num)
  win.setpos(0, 0)
  win.refresh
end

#crlfObject



103
104
105
106
107
# File 'lib/output.rb', line 103

def crlf
  # Technically not output...
  r, c = rc
  go r+1, 0
end

#delegate_output(sym, *args) ⇒ Object



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
# File 'lib/output.rb', line 53

def delegate_output(sym, *args)
  args = [""] if args.empty?
  RubyText::Window.colors(@win, @fg, @bg)  # FIXME?
  if sym == :p
    args.map!(&:inspect) 
  else
    args.map! do |x|
      if RubyText::Colors.include? x
        x
      else
        x.to_s
      end
    end
  end
# STDOUT.puts "again: #{args.inspect}"
  flag = need_crlf?(sym, args)
  # Limitation: Can't print color symbols!
  args.each do |arg|  
    if arg.is_a? Symbol # must be a color
      RubyText::Window.colors(@win, arg, @bg)  # FIXME?
    else
      arg.each_char {|ch| ch == "\n" ? crlf : @win.addch(ch) }
    end
  end
  crlf if flag
  RubyText::Window.colors(@win, @fg, @bg)  # FIXME?
  @win.refresh
end

#down(n = 1) ⇒ Object



16
17
18
19
# File 'lib/navigation.rb', line 16

def down(n=1)
  r, c = rc
  go r+n, c
end

#down!Object



46
47
48
# File 'lib/navigation.rb', line 46

def down!
  bottom
end

#go(r, c) ⇒ Object



2
3
4
5
6
7
8
9
# File 'lib/navigation.rb', line 2

def go(r, c)
  save = self.rc
  @win.setpos(r, c)
  if block_given?
    yield
    go(*save)   # No block here!
  end
end

#homeObject



61
62
63
# File 'lib/navigation.rb', line 61

def home
  go 0, 0
end

#left(n = 1) ⇒ Object



21
22
23
24
# File 'lib/navigation.rb', line 21

def left(n=1)
  r, c = rc
  go r, c-n
end

#left!Object



50
51
52
53
# File 'lib/navigation.rb', line 50

def left!
  r, c = rc
  go r, 0
end

#need_crlf?(sym, args) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/output.rb', line 48

def need_crlf?(sym, args)
  sym != :print &&      # print doesn't default to crlf
  args[-1][-1] != "\n"  # last char is a literal linefeed
end

#output(&block) ⇒ Object



126
127
128
129
130
# File 'lib/output.rb', line 126

def output(&block)
  $stdscr = self
  block.call
  $stdscr = STDSCR
end

#p(*args) ⇒ Object



90
91
92
# File 'lib/output.rb', line 90

def p(*args)
  delegate_output(:p, *args)
end


86
87
88
# File 'lib/output.rb', line 86

def print(*args)
  delegate_output(:print, *args)
end

#putch(ch) ⇒ Object



43
44
45
46
# File 'lib/output.rb', line 43

def putch(ch)
  r, c = self.rc
  self[r, c] = ch[0]
end

#puts(*args) ⇒ Object



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

def puts(*args)
  delegate_output(:puts, *args)
end

#rcObject



70
71
72
# File 'lib/navigation.rb', line 70

def rc
  [@win.cury, @win.curx]
end

#rcprint(r, c, *args) ⇒ Object



94
95
96
# File 'lib/output.rb', line 94

def rcprint(r, c, *args)
  self.go(r, c) { self.print *args }
end

#rcprint!(r, c, *args) ⇒ Object



98
99
100
101
# File 'lib/output.rb', line 98

def rcprint!(r, c, *args)
  @win.setpos(r, c)  # Cursor isn't restored
  self.print *args
end

#refreshObject



152
153
154
# File 'lib/output.rb', line 152

def refresh
  @win.refresh
end

#right(n = 1) ⇒ Object



26
27
28
29
# File 'lib/navigation.rb', line 26

def right(n=1)
  r, c = rc
  go r, c+n
end

#right!Object



55
56
57
58
59
# File 'lib/navigation.rb', line 55

def right!
  r, c = rc
  cmax = self.cols - 1
  go r, cmax
end

#topObject



31
32
33
34
# File 'lib/navigation.rb', line 31

def top
  r, c = rc
  go 0, c
end

#up(n = 1) ⇒ Object



11
12
13
14
# File 'lib/navigation.rb', line 11

def up(n=1)
  r, c = rc
  go r-n, c
end

#up!Object



42
43
44
# File 'lib/navigation.rb', line 42

def up!
  top
end