]> git.cworth.org Git - sup/blob - lib/sup/modes/log-mode.rb
yet another sig pattern :(
[sup] / lib / sup / modes / log-mode.rb
1 module Redwood
2
3 class LogMode < TextMode
4   register_keymap do |k|
5     k.add :toggle_follow, "Toggle follow mode", 'f'
6     k.add :save_to_disk, "Save log to disk", 's'
7   end
8
9   def initialize
10     @follow = true
11     super
12   end
13
14   def toggle_follow
15     @follow = !@follow
16     if buffer
17       if @follow
18         jump_to_line lines - buffer.content_height + 1 # leave an empty line at bottom
19       end
20       buffer.mark_dirty
21     end
22   end
23
24   def text= t
25     super
26     if buffer && @follow
27       follow_top = lines - buffer.content_height + 1
28       jump_to_line follow_top if topline < follow_top
29     end
30   end
31
32   def << line
33     super
34     if buffer && @follow
35       follow_top = lines - buffer.content_height + 1
36       jump_to_line follow_top if topline < follow_top
37     end
38   end
39
40   def save_to_disk
41     fn = BufferManager.ask_for_filename :filename, "Save log to file: "
42     save_to_file(fn) { |f| f.puts text } if fn
43   end
44
45   def status
46     super + " (follow: #@follow)"
47   end
48 end
49
50 end