class MarsBase10::Pane
Attributes
draw_col[RW]
draw_row[RW]
height_pct[R]
index[RW]
latch[RW]
left_edge_col[R]
subject[RW]
top_row[R]
viewport[R]
width_pct[R]
Public Class Methods
new(viewport:, at_row:, at_col:, height_pct: 1, width_pct: 1)
click to toggle source
# File lib/mars_base_10/pane.rb, line 9 def initialize(viewport:, at_row:, at_col:, height_pct: 1, width_pct: 1) @top_row = at_row @left_edge_col = at_col @height_pct = height_pct @index = 0 @latch = -1 @subject = nil @win = nil @viewport = viewport @width_pct = width_pct end
Public Instance Methods
active?()
click to toggle source
# File lib/mars_base_10/pane.rb, line 21 def active? self == self.viewport.active_pane end
clear()
click to toggle source
# File lib/mars_base_10/pane.rb, line 25 def clear self.prepare_for_writing_contents (0..(self.last_row - 1)).each do |item| self.window.setpos(self.draw_row, self.draw_col) self.window.addstr("") self.window.clrtoeol self.draw_row += 1 end end
current_subject()
click to toggle source
# File lib/mars_base_10/pane.rb, line 35 def current_subject self.subject.at index: self.index end
draw()
click to toggle source
# File lib/mars_base_10/pane.rb, line 39 def draw self.prepare_for_writing_contents (0..(self.max_contents_rows - 1)).each do |item| self.window.setpos(self.draw_row, self.draw_col) # The string here is the gutter followed by the window contents. improving the gutter is tbd. self.window.attron(Curses::A_REVERSE) if item == self.index self.window.addstr("#{"%02d" % item} #{self.subject.at index: item}") self.window.attroff(Curses::A_REVERSE) # if item == self.index self.window.clrtoeol self.draw_row += 1 end self.draw_border end
draw_border()
click to toggle source
# File lib/mars_base_10/pane.rb, line 55 def draw_border self.window.attron(Curses.color_pair(1) | Curses::A_BOLD) if self.active? self.window.box self.draw_title self.window.attroff(Curses.color_pair(1) | Curses::A_BOLD) if self.active? end
draw_title()
click to toggle source
# File lib/mars_base_10/pane.rb, line 62 def draw_title self.window.setpos(0, 2) self.window.addstr(" #{self.subject.title} (#{self.subject.rows} total) ") end
first_col()
click to toggle source
# File lib/mars_base_10/pane.rb, line 67 def first_col 1 end
first_row()
click to toggle source
# File lib/mars_base_10/pane.rb, line 71 def first_row 1 end
gutter_width()
click to toggle source
# File lib/mars_base_10/pane.rb, line 75 def gutter_width 4 end
last_col()
click to toggle source
This is the relative last column, e.g. the width of the pane in columns.
# File lib/mars_base_10/pane.rb, line 82 def last_col [(self.viewport.max_cols * self.width_pct).floor, self.min_column_width].max end
last_row()
click to toggle source
This is the relative last row, e.g. the height of the pane in columns.
# File lib/mars_base_10/pane.rb, line 89 def last_row (self.viewport.max_rows * self.height_pct).floor end
latched?()
click to toggle source
The pane is latched if it has consumed 1 key 0-9 and is awaiting the next key.
# File lib/mars_base_10/pane.rb, line 96 def latched? self.latch != -1 end
max_contents_rows()
click to toggle source
# File lib/mars_base_10/pane.rb, line 100 def max_contents_rows self.subject.rows end
min_column_width()
click to toggle source
# File lib/mars_base_10/pane.rb, line 104 def min_column_width self.gutter_width + self.subject.cols + self.right_pad end
prepare_for_writing_contents()
click to toggle source
# File lib/mars_base_10/pane.rb, line 108 def prepare_for_writing_contents self.draw_row = self.first_row self.draw_col = self.first_col end
process()
click to toggle source
process blocks and waits for a keypress.
this method handles only the “default” keypresses which all controllers/subjects
must support. Any unrecognized key is bubbled to the controller for more specific handling.
# File lib/mars_base_10/pane.rb, line 120 def process key = self.window.getch.to_s case key when 'j' self.set_row(self.index + 1) when 'k' self.set_row(self.index - 1) when 'q' exit 0 when ('0'..'9') if self.latched? self.set_row((self.latch * 10) + key.to_i) self.latch = -1 else self.latch = key.to_i end end # Always send the key to the controller for additional processing... self.viewport.controller.send key: key end
right_pad()
click to toggle source
# File lib/mars_base_10/pane.rb, line 142 def right_pad 2 end
set_row(i)
click to toggle source
this is a no-op if the index is out of range
# File lib/mars_base_10/pane.rb, line 149 def set_row(i) self.subject.scroll_limit = [self.last_row - 1, self.max_contents_rows].min if (i < 0) self.subject.scroll_up i = 0 end # If we've reached the end of the content, it's a no-op. if (i >= self.max_contents_rows) i -= 1 end if (i >= self.last_row - 2) self.subject.scroll_down i -= 1 end self.index = i # if (i <= self.max_contents_rows) && (i >= 0) end
viewing(subject:)
click to toggle source
# File lib/mars_base_10/pane.rb, line 170 def viewing(subject:) @subject = subject end
window()
click to toggle source
# File lib/mars_base_10/pane.rb, line 174 def window return @win if @win @win = Curses::Window.new(self.last_row, self.last_col, self.top_row, self.left_edge_col) end