Class: Rfd::BookmarkWindow

Inherits:
SubWindow show all
Defined in:
lib/rfd/bookmark_window.rb

Instance Attribute Summary

Attributes inherited from SubWindow

#controller, #window

Instance Method Summary collapse

Methods inherited from SubWindow

#close, #create_window, #draw_border, #height, #left, #max_height, #max_width, #refresh, #reposition_if_needed, #top, #width

Constructor Details

#initialize(controller) ⇒ BookmarkWindow

Returns a new instance of BookmarkWindow.



5
6
7
8
9
10
11
# File 'lib/rfd/bookmark_window.rb', line 5

def initialize(controller)
  super(controller)
  @cursor = 0
  @scroll = 0
  @filter = FilterInput.new { apply_filter }
  @filtered_items = nil
end

Instance Method Details

#all_itemsObject



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

def all_items
  Bookmark.bookmarks
end

#current_bookmarked?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/rfd/bookmark_window.rb', line 21

def current_bookmarked?
  Bookmark.include?(controller.current_dir.path)
end

#current_itemObject



29
30
31
# File 'lib/rfd/bookmark_window.rb', line 29

def current_item
  display_items[@cursor]
end

#display_itemsObject



17
18
19
# File 'lib/rfd/bookmark_window.rb', line 17

def display_items
  @filtered_items || all_items
end

#handle_input(c) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rfd/bookmark_window.rb', line 58

def handle_input(c)
  case c
  when 27  # ESC - close window
    controller.close_sub_window
    true
  when 64, ?@  # @ - switch to tree view
    controller.close_sub_window
    controller.instance_variable_set(:@sub_window, NavigationWindow.new(controller))
    controller.instance_variable_get(:@sub_window).render
    true
  when 2  # Ctrl-B - toggle bookmark for current directory
    toggle_bookmark
    true
  when 10, 13  # Enter - cd to selected bookmark
    select_item
    true
  when 14  # Ctrl-N
    move_cursor_down
    true
  when 16  # Ctrl-P
    move_cursor_up
    true
  else
    if @filter.handle_input(c)
      render
      true
    else
      false
    end
  end
end

#renderObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rfd/bookmark_window.rb', line 33

def render
  reposition_if_needed
  @window.clear

  draw_border('Bookmarks (@:tree ^B:add/remove ESC:close)')

  # Filter input line (row 1)
  @filter.render(@window, 1, max_width)

  # Bookmarks list starts at row 2
  visible_items.each_with_index do |path, i|
    actual_index = @scroll + i
    @window.setpos(2 + i, 1)

    display_path = path.sub(File.expand_path('~'), '~')
    if actual_index == @cursor
      @window.attron(Curses::A_REVERSE) { @window.addstr(display_path[0, max_width].ljust(max_width)) }
    else
      @window.addstr(display_path[0, max_width].ljust(max_width))
    end
  end

  @window.refresh
end

#visible_itemsObject



25
26
27
# File 'lib/rfd/bookmark_window.rb', line 25

def visible_items
  display_items[@scroll, max_height - 1] || []
end