Class: Umbra::Pad
- Inherits:
-
Object
- Object
- Umbra::Pad
- Defined in:
- lib/umbra/pad.rb
Instance Method Summary collapse
- #create_centered_window(height, width, color_pair = 14, attr = FFI::NCurses::A_BOLD) ⇒ Object
-
#create_window(h, w, t, l) ⇒ Object
minimum window creator method, not using a class.
- #destroy_pad ⇒ Object
- #destroy_window(pointer, panel) ⇒ Object
-
#filename=(filename) ⇒ Object
source of data is a filename.
-
#initialize(config = {}, &block) ⇒ Pad
constructor
You may pass height, width, row and col for creating a window otherwise a fullscreen window will be created.
-
#list=(content) ⇒ Object
receive array as content source.
-
#render(content, pad, color_pair, attr) ⇒ Object
renders the content in a loop.
- #run ⇒ Object
-
#title(stitle) ⇒ Object
print a title over the box on zeroth row.
Constructor Details
#initialize(config = {}, &block) ⇒ Pad
You may pass height, width, row and col for creating a window otherwise a fullscreen window will be created. If you pass a window from caller then that window will be used. Some keys are trapped, jkhl space, pgup, pgdown, end, home, t b NOTE: this is very minimal, and uses no widgets, so I am unable to yield an object
for further configuration. If we used a textbox, I could have yielded that.
TODO handle passed block
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/umbra/pad.rb', line 33 def initialize config={}, &block $log.debug " inside pad contructor" if $log @config = config @rows = FFI::NCurses.LINES-1 @cols = FFI::NCurses.COLS-1 @prow = @pcol = 0 # how many cols we are panning @startrow = 0 @startcol = 0 h = config.fetch(:height, 0) w = config.fetch(:width, 0) t = config.fetch(:row, 0) l = config.fetch(:col, 0) @color_pair = config.fetch(:color_pair, 14) @attr = config.fetch(:attr, FFI::NCurses::A_BOLD) @rows = h unless h == 0 @cols = w unless w == 0 @startrow = t unless t == 0 @startcol = l unless l == 0 @suppress_border = config[:suppress_border] top = t left = l @height = h @width = w #@pointer, @panel = create_window(h, w, t, l) @pointer, @panel = create_centered_window(h, w, @color_pair, @attr) @startrow, @startcol = FFI::NCurses.getbegyx(@pointer) unless @suppress_border @startrow += 1 @startcol += 1 @rows -=3 # 3 is since print_border_only reduces one from width, to check whether this is correct @cols -=3 end $log.debug "top and left are: #{top} #{left} " if $log #@window.box # 2018-03-28 - FFI::NCurses.box @pointer, 0, 0 title(config[:title]) FFI::NCurses.wbkgd(@pointer, FFI::NCurses.COLOR_PAIR(@color_pair) | @attr); FFI::NCurses.curs_set 0 # cursor invisible if config[:filename] self.filename=(config[:filename]) elsif config[:list] self.list=(config[:list]) end end |
Instance Method Details
#create_centered_window(height, width, color_pair = 14, attr = FFI::NCurses::A_BOLD) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/umbra/pad.rb', line 90 def create_centered_window height, width, color_pair=14, attr=FFI::NCurses::A_BOLD row = ((FFI::NCurses.LINES-height)/2).floor col = ((FFI::NCurses.COLS-width)/2).floor pointer = FFI::NCurses.newwin(height, width, row, col) FFI::NCurses.wbkgd(pointer, FFI::NCurses.COLOR_PAIR(color_pair) | attr); panel = FFI::NCurses.new_panel(pointer) FFI::NCurses.keypad(pointer, true) return pointer, panel end |
#create_window(h, w, t, l) ⇒ Object
minimum window creator method, not using a class. However, some methods do require windows width and ht etc
84 85 86 87 88 89 |
# File 'lib/umbra/pad.rb', line 84 def create_window h, w, t, l pointer = FFI::NCurses.newwin(h, w, t, l) panel = FFI::NCurses.new_panel(pointer) FFI::NCurses.keypad(pointer, true) return pointer, panel end |
#destroy_pad ⇒ Object
104 105 106 107 108 109 |
# File 'lib/umbra/pad.rb', line 104 def destroy_pad if @pad FFI::NCurses.delwin(@pad) @pad = nil end end |
#destroy_window(pointer, panel) ⇒ Object
99 100 101 102 103 |
# File 'lib/umbra/pad.rb', line 99 def destroy_window pointer, panel FFI::NCurses.del_panel(panel) if panel FFI::NCurses.delwin(pointer) if pointer panel = pointer = nil # prevent call twice end |
#filename=(filename) ⇒ Object
source of data is a filename
155 156 157 158 |
# File 'lib/umbra/pad.rb', line 155 def filename=(filename) content = File.open(filename,"r").read.split("\n") display_content content end |
#list=(content) ⇒ Object
receive array as content source
151 152 153 |
# File 'lib/umbra/pad.rb', line 151 def list=(content) display_content content end |
#render(content, pad, color_pair, attr) ⇒ Object
renders the content in a loop.
NOTE: separated in the hope that caller can override.
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/umbra/pad.rb', line 136 def render content, pad, color_pair, attr cp = color_pair FFI::NCurses.wbkgd(pad, FFI::NCurses.COLOR_PAIR(color_pair) | attr); FFI::NCurses.wattron(pad, FFI::NCurses.COLOR_PAIR(cp) | attr) # WRITE #filler = " "*@content_cols content.each_index { |ix| #FFI::NCurses.mvwaddstr(pad,ix, 0, filler) FFI::NCurses.mvwaddstr(pad,ix, 0, content[ix]) } FFI::NCurses.wattroff(pad, FFI::NCurses.COLOR_PAIR(cp) | attr) end |
#run ⇒ Object
176 177 178 |
# File 'lib/umbra/pad.rb', line 176 def run return handle_keys end |
#title(stitle) ⇒ Object
print a title over the box on zeroth row
111 112 113 114 115 116 |
# File 'lib/umbra/pad.rb', line 111 def title stitle return unless stitle stitle = "| #{stitle} |" col = (@width-stitle.size)/2 FFI::NCurses.mvwaddstr(@pointer, 0, col, stitle) end |