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) ⇒ 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
30
# 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)
  @win.scrollok(true)
  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=(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(@win, 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

#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



84
85
86
87
88
89
90
# File 'lib/output.rb', line 84

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) ⇒ Object



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

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



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/window.rb', line 55

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

Instance Method Details

#[](r, c) ⇒ Object



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

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



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

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



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

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

#center(str) ⇒ Object



9
10
11
12
13
14
# File 'lib/output.rb', line 9

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

#clearObject



92
93
94
95
96
97
98
99
# File 'lib/output.rb', line 92

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

#crlfObject



78
79
80
81
82
# File 'lib/output.rb', line 78

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

#delegate_output(sym, *args) ⇒ Object



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

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.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(@win, arg, @bg)  # FIXME?
    elsif arg.is_a? RubyText::Effects
      X.attrset(arg.value)
    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)


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

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



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

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

#p(*args) ⇒ Object



65
66
67
# File 'lib/output.rb', line 65

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


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

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

#putch(ch) ⇒ Object



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

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

#puts(*args) ⇒ Object



57
58
59
# File 'lib/output.rb', line 57

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



69
70
71
# File 'lib/output.rb', line 69

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

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



73
74
75
76
# File 'lib/output.rb', line 73

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

#refreshObject



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

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

#screen_text(file = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/window.rb', line 74

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



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

def scroll(n=1)
  n.times { @win.scroll }
  @win.refresh
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