Class: Noteman::Finder

Inherits:
Object
  • Object
show all
Includes:
Curses
Defined in:
lib/noteman/finder.rb

Instance Method Summary collapse

Constructor Details

#initializeFinder



7
8
9
# File 'lib/noteman/finder.rb', line 7

def initialize
  @opend = false
end

Instance Method Details

#closeObject



33
34
35
36
# File 'lib/noteman/finder.rb', line 33

def close
  @opened = false
  close_screen
end

#draw_message_box(text = '') ⇒ Object



102
103
104
105
106
107
# File 'lib/noteman/finder.rb', line 102

def draw_message_box(text='')
  @message_box.clear
  @message_box.setpos(1, 1)
  @message_box.addstr "#{text}"
  @message_box.refresh
end

#draw_result_box(results = [], selected_index = 0) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/noteman/finder.rb', line 92

def draw_result_box(results=[], selected_index=0)
  @result_box.clear
  results.each_with_index do |n, i|
    @result_box.setpos(i+1, 1)
    @result_box.attrset(i == selected_index ? A_STANDOUT : A_NORMAL)
    @result_box.addstr "#{n.file}"
  end
  @result_box.refresh
end

#draw_search_box(text = '') ⇒ Object



84
85
86
87
88
89
90
# File 'lib/noteman/finder.rb', line 84

def draw_search_box(text='')
  @search_box.clear
  @search_box.box('|', '-')
  @search_box.setpos(1, 1)
  @search_box.addstr text
  @search_box.refresh
end

#find_noteObject



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
80
81
82
# File 'lib/noteman/finder.rb', line 38

def find_note
  begin
    open
    text = ''
    selected = 0
    notes = []
    while true
      ch = @search_box.getch
      case ch
      when KEY_RESIZE
        next
      when 10 # enter
        break
      when 127 # backspace
        if text.length > 0
          text = text.slice(0, text.length - 1)
        end
      when KEY_UP #arrow up
        #selected -= 1
        selected == 0 ? selected = notes.length - 1 : selected -= 1
      when KEY_DOWN #arrow down
        selected == notes.length - 1 ? selected = 0 : selected += 1
      else
        if ch.is_a? String
          text += ch
        end
      end
      draw_search_box text
      tags = []
      keywords = []
      text.split.each do |t|
        if t.start_with? '@' and t.length > 1
          tags << t.tr('@', '')
        else
          keywords << t
        end
      end
      notes = NoteManager.search.by_tags(tags).by_keywords(keywords).result
      draw_result_box notes, selected
    end
    notes[selected]
  ensure
    close
  end
end

#openObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/noteman/finder.rb', line 11

def open
  if @opened
    return self
  end
  init_screen
  cbreak
  noecho
  raw
  @search_box = Window.new(3, cols, 0 ,0)
  @search_box.keypad = true
  draw_search_box

  @result_box = Window.new(30, cols, 3 ,0)
  draw_result_box

  @message_box = Window.new(3, cols, 40 ,0)
  draw_message_box

  @opened = true
  self
end