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 = White, bg = Blue, 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=White, bg=Blue, scroll=false)
  @wide, @high, @r0, @c0 = wide, high, r0, c0
  @border, @fg, @bg      = border, fg, bg
  @cwin = X::Window.new(high, wide, r0, c0)
  colorize!(fg, bg)
  if @border
    @cwin.box(Vert, Horiz)
    @outer = @cwin
    @outer.refresh
    @cwin = X::Window.new(high-2, wide-2, r0+1, c0+1)
    colorize!(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

#bgObject

Returns the value of attribute bg.



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

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

#fgObject

Returns the value of attribute fg.



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

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



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

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



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

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

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/color.rb', line 35

def self.colorize!(win, fg, bg)
  File.open("/tmp/cize.out", "w") do |f|
    f.puts "colorize: fg, bg = #{[fg, bg].inspect}"
  end
  cp = RubyText::Color.pair(fg, bg)
  win.color_set(cp)
  num = win.maxx * win.maxy
  win.setpos 0,0
  win.addstr(' '*num)
  win.setpos 0,0
  win.refresh
rescue => err
  File.open("/tmp/#{__method__}.out", "w") do |f|
    f.puts err
    f.puts err.backtrace
  end
end

.main(fg: White, bg: Blue, scroll: false) ⇒ Object



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

def self.main(fg: White, bg: Blue, scroll: false)
  main_win = X.init_screen
  X.start_color
  self.colorize!(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)
rescue => err
  File.open("/tmp/main.out", "w") {|f| f.puts err.inspect; f.puts err.backtrace } 
end

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



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

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



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

def [](r, c)
  ch = nil
  go(r, c) { ch = @cwin.inch }
  debug "ch = #{ch}  ch.chr = #{ch.chr}"
  ch.chr
end

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



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

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



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

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

#center(str) ⇒ Object



4
5
6
7
8
9
# File 'lib/output.rb', line 4

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

#clearObject



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

def clear
  num = @cwin.maxx * @cwin.maxy
  home
  @cwin.addstr(' '*num)
  home
  @cwin.refresh
end

#colorize!(fg, bg) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/color.rb', line 58

def colorize!(fg, bg)
  set_colors(fg, bg)
  num = @cwin.maxx * @cwin.maxy
  self.home
  self.go(0, 0) { @cwin.addstr(' '*num) }
  @cwin.refresh
end

#crlfObject

Technically not output…



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

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/output.rb', line 17

def delegate_output(sym, *args)
  self.cwin.attrset(0)
  args = [""] if args.empty?
  args += ["\n"] if sym == :puts
  set_colors(@fg, @bg)
  debug "  set colors: #{[@fg, @bg].inspect}"
  if sym == :p
    args.map! {|x| effect?(x) ? x : x.inspect }
  else
    args.map! {|x| effect?(x) ? x : x.to_s }
  end
  args.each do |arg|  
    if arg.is_a?(RubyText::Effects)
      arg.set(self)
    elsif arg.respond_to? :effect
      arg.effect.set(self)
      arg.each_char {|ch| ch == "\n" ? crlf : @cwin.addch(ch) }
      @cwin.refresh
    else
      arg.each_char {|ch| ch == "\n" ? crlf : @cwin.addch(ch) }
      @cwin.refresh
    end
  end
  crlf if sym == :p
  set_colors(@fg, @bg)
  @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

#effect?(arg) ⇒ Boolean

FIXME Please refactor the Hal out of this.

Returns:

  • (Boolean)


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

def effect?(arg)
  arg.is_a?(RubyText::Effects)
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

#noscrollObject



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

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

#output(&block) ⇒ Object



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

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

#p(*args) ⇒ Object



53
54
55
# File 'lib/output.rb', line 53

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


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

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

#putch(ch, r: nil, c: nil, fx: nil) ⇒ Object



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

def putch(ch, r: nil, c: nil, fx: nil)
  r0, c0 = self.rc
  r ||= r0
  c ||= c0
  go(r, c) do
    fx.set(self) if fx
    val = fx.value rescue 0
    @cwin.addch(ch.ord|val)
    # @win.refresh
  end
  fx.reset(self) if fx
  # self[r, c] = ch[0]
end

#puts(*args) ⇒ Object



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

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



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

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

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



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

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

#refreshObject



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

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



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

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



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

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

#set_colors(fg, bg) ⇒ Object



53
54
55
56
# File 'lib/color.rb', line 53

def set_colors(fg, bg)
  cp = RubyText::Color.pair(fg, bg)
  @cwin.color_set(cp)
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