Class: RubyText::Window

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

Overview

require ‘global’ # FIXME later

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

def initialize(high=nil, wide=nil, r0=1, c0=1, border=false, fg=nil, bg=nil, scroll=false)
  @wide, @high, @r0, @c0 = wide, high, r0, c0
  @border, @fg, @bg      = border, fg, bg
  @cwin = X::Window.new(high, wide, r0, c0)
  RubyText::Window.colors!(@cwin, fg, bg)
  if @border
    @cwin.box(Vert, Horiz)
    @outer = @cwin
    @outer.refresh
    @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:

  • 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:

  • 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

#scrolling(flag = true) ⇒ Object (readonly)

FIXME refactor bad code



55
56
57
# File 'lib/window.rb', line 55

def scrolling
  @scrolling
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



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

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



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

def self.main(fg: nil, bg: nil, scroll: false)
  main_win = X.init_screen
  X.start_color
  colors!(main_win, fg, bg)
  rows, cols = main_win.maxy, main_win.maxx
  self.make(main_win, rows, cols, 0, 0, border: false,
            fg: fg, bg: bg, scroll: scroll)
end

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



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/window.rb', line 39

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



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

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



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

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



137
138
139
140
# File 'lib/output.rb', line 137

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

#center(str) ⇒ Object



6
7
8
9
10
11
# File 'lib/output.rb', line 6

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

#clearObject



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

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

#crlfObject

Technically not output…



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/output.rb', line 81

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

#delegate_output(sym, *args) ⇒ Object

FIXME Please refactor the Hal out of this.



25
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
56
57
58
# File 'lib/output.rb', line 25

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
  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 do |ch| 
        if ch == "\n" 
          crlf 
        else
          @cwin.addch(ch)
        end
      end
    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:



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

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



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

def noscroll
  @scrolling = false
  @cwin.scrollok(false)
end

#output(&block) ⇒ Object



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

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

#p(*args) ⇒ Object



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

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


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

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

#putch(ch) ⇒ Object



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

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

#puts(*args) ⇒ Object



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

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

#rcObject

def crlf

r, c = rc
go r+1, 0

end



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

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

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



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

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

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



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

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

#refreshObject



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

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



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/window.rb', line 80

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



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

def scroll(n=1)
  if n < 0
    @cwin.scrl(n)
    (-n).times {|i| rcprint i, 0, (' '*@cols) }
  else
    n.times do |i|
      @cwin.scroll
      scrolling(false)
      rcprint @rows-1, 0, (' '*@cols)
      scrolling
    end
  end
  @cwin.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