Class: RubyText::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/color.rb,
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, scroll = false) ⇒ Window

Better to use Window.window IRL



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

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

#cwinObject (readonly)

Returns the value of attribute cwin.



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

def cwin
  @cwin
end

#fg=(value) ⇒ Object (writeonly)

Sets the attribute fg

Parameters:

  • value

    the value to set the attribute fg to.



41
42
43
# File 'lib/color.rb', line 41

def fg=(sym)
  self.colors(@cwin, fg, @bg)
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

Class Method Details

.clear(win) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/output.rb', line 90

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



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

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

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



32
33
34
35
36
37
38
39
# File 'lib/color.rb', line 32

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, scroll: false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/window.rb', line 35

def self.main(fg: nil, bg: nil, scroll: false)
  @main_win = X.init_screen
  X.start_color
  colors!(@main_win, fg, bg)
  @cwin = @main_win  # FIXME?
  rows, cols = @main_win.maxy, @main_win.maxx
  @screen = self.make(@main_win, rows, cols, 0, 0, border: false,
                      fg: fg, bg: bg, scroll: scroll)
# FIXME Why is this hard to inline?
##  Must be params...?
##  def self.make(cwin, high, wide, r0, c0, border, fg: White, bg: Black, scroll: false)
#     obj = self.allocate
#     obj.instance_eval do 
#       @outer = @cwin = @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
#     obj.scrolling(scroll)
#     obj
end

.make(cwin, high, wide, r0, c0, border: true, fg: White, bg: Black, scroll: false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/window.rb', line 59

def self.make(cwin, high, wide, r0, c0, border: true, fg: White, bg: Black, scroll: false)
  obj = self.allocate
  obj.instance_eval do 
    #  debug "  Inside instance_eval..."
    @outer = @cwin = 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.scrolling(scroll)
  obj
end

Instance Method Details

#[](r, c) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/output.rb', line 113

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

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



122
123
124
125
126
# File 'lib/output.rb', line 122

def []=(r, c, char)
  @cwin.setpos(r, c)
  @cwin.addch(char[0])
  @cwin.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



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

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

#center(str) ⇒ Object



11
12
13
14
15
16
# File 'lib/output.rb', line 11

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

#clearObject



98
99
100
101
102
103
104
105
# File 'lib/output.rb', line 98

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

#crlfObject



80
81
82
83
84
85
86
87
88
# File 'lib/output.rb', line 80

def crlf
  # Technically not output...
  r, c = rc
  if r < @rows - 1 && !@scrolling
    go r+1, 0
  else
    scroll
  end
end

#delegate_output(sym, *args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/output.rb', line 28

def delegate_output(sym, *args)
  args = [""] if args.empty?
  RubyText::Window.colors(@cwin, @fg, @bg)  # FIXME?
  if sym == :p
    args.map!(&:inspect) 
  else
    args.map! do |x|
      if RubyText::Colors.include?(x) || x.is_a?(RubyText::Effects)
        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(@cwin, arg, @bg)  # FIXME?
    elsif arg.is_a? RubyText::Effects
      X.attrset(arg.value)
    else
      arg.each_char {|ch| ch == "\n" ? crlf : @cwin.addch(ch) }
    end
  end
  crlf if flag
  RubyText::Window.colors(@cwin, @fg, @bg)  # FIXME?
  @cwin.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
  @cwin.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)


23
24
25
26
# File 'lib/output.rb', line 23

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

#noscrollObject



80
81
82
# File 'lib/window.rb', line 80

def noscroll
  @cwin.scrollok(false)
end

#output(&block) ⇒ Object



107
108
109
110
111
# File 'lib/output.rb', line 107

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

#p(*args) ⇒ Object



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

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


63
64
65
# File 'lib/output.rb', line 63

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

#putch(ch) ⇒ Object



18
19
20
21
# File 'lib/output.rb', line 18

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

#puts(*args) ⇒ Object



59
60
61
# File 'lib/output.rb', line 59

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

#rcObject



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

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

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



71
72
73
# File 'lib/output.rb', line 71

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

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



75
76
77
78
# File 'lib/output.rb', line 75

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

#refreshObject



133
134
135
# File 'lib/output.rb', line 133

def refresh
  @cwin.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

#screen_text(file = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/window.rb', line 96

def screen_text(file = nil)
  lines = []
  0.upto(self.rows-1) do |r|
    line = ""
    0.upto(self.cols-1) do |c|
      line << self[r, c]
    end
    lines << line
  end
  File.open(file, "w") {|f| f.puts lines }  if file
  lines
end

#scroll(n = 1) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/window.rb', line 84

def scroll(n=1)
  n.times do |i|
    @cwin.scroll
    go(@rows-1, 0)
    noscroll
    print(' '*@cols)
    scrolling
    left!
  end
  @cwin.refresh
end

#scrolling(flag = true) ⇒ Object

FIXME refactor bad code



76
77
78
# File 'lib/window.rb', line 76

def scrolling(flag=true)
  @cwin.scrollok(flag)
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