class Listpager::List

Constants

BLANK_SPACE

U+2003 “EM SPACE”. Ncurses' range-combining “optimizations” fuck up normal and non-breaking spaces. For display, this is fine. For copying, you'd have a scrollbar in the way anyway. I fear newer releases of ncurses will get smarter and also consider this “blank” for optimizations.

INDICATOR
NO_INDICATOR

Attributes

offset[R]
scrollbar[R]
selected[R]
title[R]
values[RW]
window[R]

Public Class Methods

new(window) click to toggle source
# File lib/listpager/list.rb, line 29
def initialize(window)
  @window = window
  @title = nil
  @values = []
  @selected = 0
  @offset = 0
  @dirty = true
  @scrollbar = Scrollbar.new(self)
end

Public Instance Methods

dirty!(value = true) click to toggle source
# File lib/listpager/list.rb, line 39
def dirty!(value = true)
  @dirty = value
end
dirty?() click to toggle source
# File lib/listpager/list.rb, line 43
def dirty?
  @dirty
end
getmaxxy() click to toggle source
# File lib/listpager/list.rb, line 116
def getmaxxy
  maxx, maxy = [], []
  window.getmaxyx(maxy, maxx)
  [maxx[0], maxy[0]]
end
getminxy() click to toggle source
# File lib/listpager/list.rb, line 110
def getminxy
  x, y = 0, 0
  y += 1 if title
  [x, y]
end
key_input(value) click to toggle source
# File lib/listpager/list.rb, line 85
def key_input(value)
  maxx, maxy = getmaxxy

  case value
    when Ncurses::KEY_UP
      self.selected -= 1
    when Ncurses::KEY_DOWN
      self.selected += 1
    when Ncurses::KEY_PPAGE
      self.selected -= maxy - 1
    when Ncurses::KEY_NPAGE
      self.selected += maxy - 1
    else
      on_key_press(value)
  end
end
normalize(s) click to toggle source
# File lib/listpager/list.rb, line 106
def normalize(s)
  s.gsub(/[^[:print:]]/, '')
end
offset=(v) click to toggle source
# File lib/listpager/list.rb, line 55
def offset=(v)
  dirty! if v != @offset
  v = 0 if v < 0
  @offset = v
end
on_key_press(k) click to toggle source
# File lib/listpager/list.rb, line 19
def on_key_press(k)
end
on_select_change() click to toggle source
# File lib/listpager/list.rb, line 16
def on_select_change
end
render() click to toggle source
# File lib/listpager/list.rb, line 137
def render
  return false if ! dirty?

  render_title

  maxx, maxy = getmaxxy
  minx, miny = getminxy

  (miny...maxy).each do |i|
    window.color_set(Color[:list_default], nil)

    list_index = (offset + i) - miny
    window.move(i, 0)
    indicator = nil

    fixed_len = maxx - scrollbar.width

    if list_index == selected
      window.color_set(Color[:list_selected], nil)
      indicator = INDICATOR
    else
      indicator = NO_INDICATOR
    end

    string = values[list_index] || ''
    string = normalize(string)
    string = indicator + string

    if string.size < fixed_len
      string += (BLANK_SPACE * (fixed_len - string.size))
    elsif string.size > fixed_len
      string = string[0...fixed_len]
    end
    window.addstr(string)
  end

  scrollbar.render
  dirty!(false)
  return true
end
render_title() click to toggle source
# File lib/listpager/list.rb, line 128
def render_title
  maxx, maxy = getmaxxy
  if title
    window.color_set(Color[:title], nil)
    window.move(0, 0)
    window.addstr(space_pad(' ' + title, maxx))
  end
end
selected=(v) click to toggle source
# File lib/listpager/list.rb, line 62
def selected=(v)
  minx, miny = getminxy
  maxx, maxy = getmaxxy

  v = [0, v, values.size - 1].sort[1]
  screenh = maxy - miny

  self.offset = [v + miny - 1, offset, (v + miny + 1) - screenh].sort[1]

  if v != @selected
    dirty!
    @selected = v
    on_select_change
  end

  return @selected
end
selected_value() click to toggle source
# File lib/listpager/list.rb, line 80
def selected_value
  values[selected]
end
space_pad(s, w) click to toggle source
# File lib/listpager/list.rb, line 122
def space_pad(s, w)
  nspaces = (w - s.size)
  nspaces = 0 if nspaces < 0
  s + (BLANK_SPACE * nspaces)
end
title=(v) click to toggle source
# File lib/listpager/list.rb, line 47
def title=(v)
  if v != @title
    @title = v
    dirty!
  end
  @title
end