Class: TerminalNotes::App

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal-notes.rb

Instance Method Summary collapse

Instance Method Details

#editorObject



144
145
146
# File 'lib/terminal-notes.rb', line 144

def editor
    ENV['EDITOR'] || DEFAULT_EDITOR
end

#get_contextObject



130
131
132
# File 'lib/terminal-notes.rb', line 130

def get_context
    { mode: @context }
end

#open_file(file) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/terminal-notes.rb', line 134

def open_file(file)
    system(editor, file)
    redraw

    # Hack to redraw focus correctly
    # TODO: Figure out why this works
    set_context :search
    set_context :browse
end

#redraw(resize: false) ⇒ Object

This method can be invoked independent of the wait for keyboard input loop, so explicitly call for a screen update after widgets have been drawn



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/terminal-notes.rb', line 151

def redraw(resize: false)
    return unless @has_initialized

    @widgets.each(&:redraw)
    # @widgets.each do |widget|
        # widget.window.clear
        # @panels.remove(widget.window)
        # widget.draw(resize: resize)
        # @panels.add(widget.window)
    # end

    set_context @context

    @panels.refresh_in_memory
    Rurses.update_screen
end

#set_context(context) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/terminal-notes.rb', line 112

def set_context context
    @context = context

    if @context == :browse
        Rurses.curses.curs_set(0)
        @search_field.unfocus
        @file_list.focus

    elsif @context == :search
        Rurses.curses.curs_set(1)
        @file_list.unfocus
        @search_field.focus

    end

    @info_bar.draw
end

#start(config) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/terminal-notes.rb', line 14

def start(config)
    directory = config[:directory]

    Rurses.program(modes: %i[c_break no_echo keypad]) do |screen|
        @screen = screen

        @widgets = []

        @search_field = SearchField.new(parent: @screen)
        @widgets << @search_field

        @info_bar = InfoBar.new(parent: @screen) { get_context }
        @widgets << @info_bar

        padding = 2

        # file_list_height = screen.size[:lines]
        #                     - @search_field.height
        #                     - @info_bar.height
        #                     - padding
        file_list_height = 40
        @file_list = FileList.new(parent: @screen,
                                  height: file_list_height,
                                  y: @search_field.height + padding,
                                  directory: directory)
        @widgets << @file_list

        # Register the text_field filter as a delegate
        # of the search field
        @search_field.on_text_changed do |pattern|
            @file_list.filter(pattern)
        end

        # Register file open handler
        @file_list.on_file_opened do |file|
            open_file(file)
        end

        # A PanelStack handles how windows are drawn
        # over each other - i.e. overlapping
        #
        # I'm not really using this ability, however, in
        # Rurses, the PanelStack seems to be the only way
        # to delete/deallocate a window completely
        #
        @panels = Rurses::PanelStack.new
        @widgets.each { |widget| @panels.add(widget.window) }
        @panels.refresh_in_memory

        # Events
        # watch_for_resize

        # Set initial state
        set_context :search

        # Actually draw the screen
        # @panels.refresh_in_memory
        Rurses.update_screen

        @has_initialized = true

        loop do
            key = Rurses.get_key

            if @context == :browse
                case key
                when "q", :CTRL_X
                    break # quit
                when "m"
                    @file_list.toggle_matcher
                    @file_list.filter(@search_field.text)

                when "\t", "/"
                    set_context :search
                else
                    @file_list.on_key(key)
                end
            else
                case key
                when :CTRL_X
                    break # quit
                when "\t", :ENTER
                    set_context :browse
                else
                    @search_field.on_key(key)
                end
            end

            # When all the downstream widgets are done updating
            # in-memory, actually redraw the screen
            # @panels.refresh_in_memory
            Rurses.update_screen
        end

        @screen.move_cursor(x: 0, y: 0)
    end
end

#watch_for_resizeObject



168
169
170
171
172
173
# File 'lib/terminal-notes.rb', line 168

def watch_for_resize
    # SIGWINCH
    #   The SIGWINCH signal is sent to a process when its
    #   controlling terminal changes its size (a window change).
    Signal.trap(:SIGWINCH) { redraw(resize: true) }
end