Class: TerminalNotes::FileList
- Defined in:
- lib/terminal-notes/file_list.rb
Constant Summary collapse
- WIDTH =
1
- EXTENSIONS =
['.md', '.txt']
- DEFAULT_EDITOR =
"nano"
- CONTENT_PADDING =
2
- FILE_INDICATOR =
"❯ "
- INTER_COL_GAP =
" "
- MATCHERS =
[:regex, :fuzzy]
Constants inherited from Widget
Instance Attribute Summary
Attributes inherited from Widget
Instance Method Summary collapse
- #draw ⇒ Object
- #filter(pattern) ⇒ Object
-
#focus ⇒ Object
TODO.
-
#initialize(parent:, height:, y:, directory:, extensions: EXTENSIONS) ⇒ FileList
constructor
A new instance of FileList.
- #notify_file_opened(file) ⇒ Object
- #on_file_opened(&block) ⇒ Object
- #on_key(key) ⇒ Object
- #toggle_matcher ⇒ Object
- #unfocus ⇒ Object
Methods inherited from Widget
Constructor Details
#initialize(parent:, height:, y:, directory:, extensions: EXTENSIONS) ⇒ FileList
Returns a new instance of FileList.
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 |
# File 'lib/terminal-notes/file_list.rb', line 16 def initialize(parent:, height:, y:, directory:, extensions: EXTENSIONS) super(parent: parent, title: "Files", width: WIDTH, height: height, y: y) @content_start = Cursor.new(x: 0, y: CONTENT_PADDING) # Find all the files that match specified extensions @all_files = Dir.glob("#{directory}/*") @all_files = @all_files.find_all do |f| f.match /(#{EXTENSIONS.join("|")})$/ end @all_files = @all_files.map do |f| { name: f.gsub("#{directory}/", ''), path: f, date: "Jun 24, 2012", time: "05:32pm" } end @filtered_files = @all_files @fuzzy_matcher = FuzzyMatch.new(@all_files, read: :name) @curr_matcher = 0 @current_line = 0 @has_focus = false @on_file_opened_delegates = [] draw end |
Instance Method Details
#draw ⇒ Object
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/terminal-notes/file_list.rb', line 60 def draw super do # Calculate where to start drawing the table # table_width = 0 # Calculate NAME col width longest_file = @all_files.max { |a, b| a[:name].size <=> b[:name].size } if longest_file.nil? name_col_width = 10 else name_col_width = 2 + longest_file[:name].size end # Calculate DATE col width, e.g. Jun 24, 2012 date_col_width = 12 # Calculate TIME col width, e.g. 05:24pm time_col_width = 7 table_width += name_col_width + INTER_COL_GAP.size table_width += date_col_width + INTER_COL_GAP.size table_width += time_col_width # Move cursor to center the table cursor = @content_start.dup cursor.moveTo(x: (@parent.size[:columns] - table_width) / 2) @window.move_cursor(cursor) # Start actually drawing # # Draw header line = "NAME".ljust(name_col_width) + INTER_COL_GAP line += "DATE".ljust(date_col_width) + INTER_COL_GAP line += "TIME".ljust(time_col_width) @window.draw_string(line) # Move cursor back to accomodate the file indicator cursor.moveBy(-FILE_INDICATOR.size, 1) @window.move_cursor(cursor) no_indicator = " " * FILE_INDICATOR.size index = 0 @filtered_files.each do |file| line = no_indicator line += file[:name].ljust(name_col_width) + INTER_COL_GAP line += file[:date].rjust(date_col_width) + INTER_COL_GAP line += file[:time].rjust(time_col_width) @window.draw_string(line) # Draw the arrow if @has_focus && index == @current_line @window.move_cursor(cursor) @window.draw_string(FILE_INDICATOR) end cursor.moveBy(0, 1) @window.move_cursor(cursor) index += 1 end clear_line = no_indicator + " " * table_width while index < @all_files.size @window.draw_string(clear_line) cursor.moveBy(0, 1) @window.move_cursor(cursor) index += 1 end end end |
#filter(pattern) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/terminal-notes/file_list.rb', line 153 def filter pattern old_file_list = @filtered_files if pattern.empty? @filtered_files = @all_files else matcher_type = MATCHERS[@curr_matcher] case matcher_type when :regex @filtered_files = @all_files.find_all do |f| f[:name].match /#{pattern}/ end when :fuzzy @filtered_files = @fuzzy_matcher.find_all(pattern) else raise "Unknown Matcher! '#{matcher_type}'" end end # If the file list has changed, reset current line marker @current_line = 0 unless old_file_list == @filtered_files draw end |
#focus ⇒ Object
TODO
50 51 52 53 |
# File 'lib/terminal-notes/file_list.rb', line 50 def focus @has_focus = true draw end |
#notify_file_opened(file) ⇒ Object
187 188 189 190 191 |
# File 'lib/terminal-notes/file_list.rb', line 187 def notify_file_opened(file) @on_file_opened_delegates.each do |delegate| delegate.call(file) end end |
#on_file_opened(&block) ⇒ Object
183 184 185 |
# File 'lib/terminal-notes/file_list.rb', line 183 def on_file_opened &block @on_file_opened_delegates << block end |
#on_key(key) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/terminal-notes/file_list.rb', line 137 def on_key key return unless @has_focus case key when "j" scroll_down when "k" scroll_up when :ENTER open_current else end draw end |
#toggle_matcher ⇒ Object
178 179 180 181 |
# File 'lib/terminal-notes/file_list.rb', line 178 def toggle_matcher @curr_matcher += 1 @curr_matcher = 0 if @curr_matcher >= MATCHERS.size end |
#unfocus ⇒ Object
55 56 57 58 |
# File 'lib/terminal-notes/file_list.rb', line 55 def unfocus @has_focus = false draw end |