class Zashoku::View

Attributes

attributes[RW]
format[RW]
items_formatted[RW]

Public Class Methods

new() click to toggle source
# File lib/core/view.rb, line 10
def initialize
  @attributes = {}

  @children = true
  @expanded = -1
  @selected = 0

  @matches = []
  @match_i = -1

  @items_formatted  = {}

  @old_rows = Term.rows

  # @win = Curses::Window.new(0,0,0,0)
  @view = [0, Term.rows - 3]
  refresh
end

Public Instance Methods

adjust_view() click to toggle source
# File lib/core/view.rb, line 211
def adjust_view
  buffer = 2
  bottom = Term.rows - 3

  ov = @view.clone
  diff = 0
  if @selected < @view[0] + buffer # and @selected > buffer
    while @selected < @view[0] + buffer
      @view[0] -= 1
      @view[1] -= 1
    end
  elsif (@selected > @view[1] - (buffer + 1)) && (@selected < get_len - buffer)
    while (@selected > @view[1] - (buffer + 1)) && (@selected < get_len - buffer)
      @view[0] += 1
      @view[1] += 1
    end
  end

  diff = 0      - @view[0] if @view[0].negative?
  diff = bottom - @view[1] if @view[1] < bottom


  @view[0] += diff
  @view[1] += diff

  dirty_all_lines unless ov == @view
end
change_screen_size(redraw: true) click to toggle source
# File lib/core/view.rb, line 84
def change_screen_size(redraw: true)
  @items_formatted = {}
  refresh_formats

  diff = Term.rows - @old_rows
  # @view[0] += diff
  @view[1] += diff

  @old_rows = Term.rows
  adjust_view
  if redraw
    dirty_all_lines
    draw
  end
end
changed!() click to toggle source
# File lib/core/view.rb, line 78
def changed!
  fix_cursor
  changed
  notify_observers
end
cleanup() click to toggle source
# File lib/core/view.rb, line 29
def cleanup; end
color(index) click to toggle source
# File lib/core/view.rb, line 203
def color(index)
  if index == @selected
    Zashoku.conf.get(%w[color selected])
  else
    Zashoku.conf.get(%w[color secondary])
  end
end
dirty_all_lines() click to toggle source
# File lib/core/view.rb, line 31
def dirty_all_lines
  @dirty_lines = 0.step(get_len).to_a
end
draw() click to toggle source
# File lib/core/view.rb, line 284
def draw
  # set the view
  line_on = @view[0]
  lines_to_draw = @view[1]

  print "\e[1;1H#{@cl['m']}#{@title_formatted}\e[K"

  while line_on < lines_to_draw
    draw_line(line_on) if @dirty_lines.include?(line_on)
    line_on += 1
  end

  @dirty_lines.clear
end
draw_line(line_on) click to toggle source
# File lib/core/view.rb, line 256
def draw_line(line_on)
  in_outer_region = line_on < get_len
  rs              = resolve_selected(line_on)
  line            = line_on - @view[0] + 2

  lc =
    if line_on == @selected
      @cl['s']
    elsif rs['in_expanded']
      @cl['2']
    else
      @cl['1']
    end

  lo = "\e[#{line};1H#{lc}"

  item =
    if rs['in_expanded'] && @children
      "#{@items_formatted.values[@expanded][rs['in']]}"
    elsif in_outer_region
      "#{@items_formatted.keys[rs['out']]}"
    else
      ''
    end

  print "#{lo}#{item}\e[K" #.tr(' ', '#')
end
expand(i) click to toggle source
# File lib/core/view.rb, line 110
def expand(i)
  return if i > @items.length
  oexpanded = @expanded

  if @expanded == i
    @selected = @expanded
    @expanded = -1
  else
    @selected = i
    @expanded = i
  end

  arr = [@expanded, oexpanded] - [-1]
  @dirty_lines = arr.min.step(@view[1]).to_a
  changed!
end
fix_cursor() click to toggle source
# File lib/core/view.rb, line 192
def fix_cursor
  @selected = 0 if @selected < 0
  @selected = get_len - 1 if @selected >= get_len
end
get_format() click to toggle source
# File lib/core/view.rb, line 303
def get_format; '' end
get_len() click to toggle source
# File lib/core/view.rb, line 197
def get_len
  len = @items.length
  len += @items.values[@expanded].length if @expanded >= 0
  len
end
move_cursor(dir) click to toggle source
# File lib/core/view.rb, line 161
def move_cursor(dir)
  os = @selected
  @selected -= 1 if dir == 'up'
  @selected += 1 if dir == 'down'
  @dirty_lines += [os, @selected]

  adjust_view
  changed!
end
next_match() click to toggle source
# File lib/core/view.rb, line 35
def next_match
  return if @matches.empty?
  @match_i += 1

  if @match_i >= @matches.length
    @match_i = 0
  end

  select(*@matches[@match_i])
end
pause() click to toggle source
# File lib/core/view.rb, line 76
def pause; end
previous_match() click to toggle source
# File lib/core/view.rb, line 46
def previous_match
  return if @matches.empty?

  @match_i -= 1

  if @match_i < 0
    @match_i = @matches.length - 1
  end
  select(*@matches[@match_i])
end
refresh() click to toggle source
# File lib/core/view.rb, line 100
def refresh
  @items = refresh_items
  @children = @items.class == Hash
  refresh_attributes
  refresh_formats
  dirty_all_lines
  @expanded = -1 if @children && @items.values[@expanded].nil?
  changed!
end
refresh_attributes() click to toggle source
# File lib/core/view.rb, line 299
def refresh_attributes; end
refresh_formats() click to toggle source
# File lib/core/view.rb, line 239
def refresh_formats
  @cl = {
    's' => Zashoku.conf.get(%w[color selected]),
    '1' => Zashoku.conf.get(%w[color primary]),
    '2' => Zashoku.conf.get(%w[color secondary]),
    'm' => Zashoku.conf.get(%w[color main])
  }

  @title_formatted = Zashoku::Formatter.format_line(
    get_format['title'],
    @attributes
  )

  return if @items.empty?
  @items_formatted = Zashoku::Formatter.items(get_format, @items)
end
refresh_items() click to toggle source
# File lib/core/view.rb, line 301
def refresh_items; [] end
resolve_selected(resolve_me = @selected) click to toggle source
# File lib/core/view.rb, line 127
def resolve_selected(resolve_me = @selected)
  in_expanded = false
  inside = 0
  outside = resolve_me
  if @expanded > -1
    if (resolve_me > @expanded) && (resolve_me <= (@items.values[@expanded].length + @expanded))
      outside = @expanded
      inside = resolve_me - @expanded - 1
      in_expanded = true
    elsif resolve_me > @expanded
      outside = resolve_me - @items.values[@expanded].length
    end
  end

  {
    'out' => outside,
    'in' => inside,
    'in_expanded' => in_expanded
  }
end
select(out, inr) click to toggle source
# File lib/core/view.rb, line 171
def select(out, inr)
  out = out.to_i
  inr = inr.to_i

  #return if out >= @items.length
  #return if inr >= @items[@expanded].length
  unless inr == 0 ||  @expanded == out
    oexpanded = @expanded
    @expanded = out
    arr = [@expanded, oexpanded] - [-1]
    @dirty_lines += arr.min.step(@view[1]).to_a
  end
  os = @selected
  @selected = out + inr

  @dirty_lines += [os, @selected]

  adjust_view
  changed!
end
selected_group() click to toggle source
# File lib/core/view.rb, line 148
def selected_group
  resolve_selected['out']
end
selected_item() click to toggle source
# File lib/core/view.rb, line 152
def selected_item
  rs = resolve_selected
  if rs['in_expanded']
    @items.values[rs["out"]][rs["in"]]
  else
    @items.keys[rs['out']]
  end
end
unpause() click to toggle source
# File lib/core/view.rb, line 74
def unpause; end