]> git.cworth.org Git - sup/blob - bin/sup
8c785e477584ad1f62202d7dfdb7c8e09e0f7559
[sup] / bin / sup
1 #!/usr/bin/env ruby
2
3 require 'rubygems'
4 require 'ncurses'
5 require "sup"
6
7 Thread.abort_on_exception = true # make debugging possible
8
9 module Redwood
10
11 global_keymap = Keymap.new do |k|
12   k.add :quit, "Quit Redwood", 'q'
13   k.add :help, "Show help", 'H', '?'
14   k.add :roll_buffers, "Switch to next buffer", 'b'
15   k.add :roll_buffers_backwards, "Switch to previous buffer", 'B'
16   k.add :kill_buffer, "Kill the current buffer", 'x'
17   k.add :list_buffers, "List all buffers", 'A'
18   k.add :list_contacts, "List contacts", 'C'
19   k.add :redraw, "Redraw screen", :ctrl_l
20   k.add :search, "Search messages", '/'
21   k.add :list_labels, "List labels", 'L'
22   k.add :poll, "Poll for new messages", 'P'
23   k.add :compose, "Compose new message", 'm'
24   k.add :recall_draft, "Edit most recent draft message", 'R'
25 end
26
27 def start_cursing
28   Ncurses.initscr
29   Ncurses.noecho
30   Ncurses.cbreak
31   Ncurses.stdscr.keypad 1
32   Ncurses.curs_set 0
33   Ncurses.start_color
34 end
35
36 def stop_cursing
37   Ncurses.curs_set 1
38   Ncurses.echo
39   Ncurses.endwin
40 end
41 module_function :start_cursing, :stop_cursing
42
43 Redwood::start
44 Index.new.load
45
46 if(s = Index.source_for DraftManager.source_name)
47   DraftManager.source = s
48 else
49   Index.add_source DraftManager.new_source
50 end
51
52 if(s = Index.source_for SentManager.source_name)
53   SentManager.source = s
54 else
55   Index.add_source SentManager.new_source
56 end
57
58 begin
59   log "starting curses"
60   start_cursing
61
62   log "initializing colormap"
63   Colormap.new do |c|
64     c.add :status_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLUE, Ncurses::A_BOLD
65     c.add :index_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
66     c.add :index_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK, 
67            Ncurses::A_BOLD
68     c.add :labellist_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
69     c.add :labellist_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK, 
70            Ncurses::A_BOLD
71     c.add :twiddle_color, Ncurses::COLOR_BLUE, Ncurses::COLOR_BLACK
72     c.add :label_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
73     c.add :message_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_GREEN
74     c.add :mime_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
75     c.add :quote_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
76     c.add :sig_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
77     c.add :quote_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
78     c.add :sig_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
79     c.add :to_me_color, Ncurses::COLOR_GREEN, Ncurses::COLOR_BLACK
80     c.add :starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
81           Ncurses::A_BOLD
82     c.add :starred_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_GREEN,
83           Ncurses::A_BOLD
84     c.add :snippet_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
85     c.add :option_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
86     c.add :tagged_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
87           Ncurses::A_BOLD
88     c.add :draft_notification_color, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK,
89           Ncurses::A_BOLD
90   end
91
92   log "initializing buffer manager"
93   bm = BufferManager.new
94
95   log "initializing mail index buffer"
96   imode = InboxMode.new
97   ibuf = bm.spawn "inbox", imode
98
99   log "ready for (inter)action!"
100   Logger.make_buf
101
102   bm.draw_screen
103   imode.load_threads :num => ibuf.content_height, :when_done => lambda {   reporting_thread { sleep 1; PollManager.poll } }
104
105   PollManager.start_thread
106
107   until $exception
108     bm.draw_screen
109     c = Ncurses.nonblocking_getch
110     bm.erase_flash
111
112     if c == Ncurses::KEY_RESIZE
113       bm.handle_resize
114     elsif c
115       unless bm.handle_input(c)
116         x = global_keymap.action_for c
117         case x
118         when :quit
119           break
120         when :help
121           curmode = bm.focus_buf.mode
122           bm.spawn_unless_exists("<help for #{curmode.name}>") { HelpMode.new curmode, global_keymap }
123         when :roll_buffers
124           bm.roll_buffers
125         when :roll_buffers_backwards
126           bm.roll_buffers_backwards
127         when :kill_buffer
128           bm.kill_buffer bm.focus_buf if bm.focus_buf.mode.killable?
129         when :list_buffers
130           bm.spawn_unless_exists("Buffer List") { BufferListMode.new }
131         when :list_contacts
132           b = bm.spawn_unless_exists("Contact List") { ContactListMode.new }
133           b.mode.load_more b.content_height
134         when :search
135           text = bm.ask :search, "query: "
136           next unless text && text !~ /^\s*$/
137
138           begin
139             qobj = Index.parse_user_query_string text
140             short_text = text.length < 20 ? text : text[0 ... 20] + "..."
141             log "built query from #{text.inspect}: #{qobj}"
142             mode = SearchResultsMode.new qobj
143             bm.spawn "search: \"#{short_text}\"", mode
144             mode.load_threads :num => mode.buffer.content_height
145           rescue Ferret::QueryParser::QueryParseException => e
146             bm.flash "Couldn't parse query."
147           end
148
149         when :list_labels
150           b = bm.spawn_unless_exists("Label List") { LabelListMode.new }
151           b.mode.load_in_background
152         when :compose
153           mode = ComposeMode.new
154           bm.spawn "New Message", mode
155           mode.edit
156         when :poll
157           bm.raise_to_front PollManager.buffer
158           reporting_thread { PollManager.poll }
159         when :recall_draft
160           case Index.num_results_for :label => :draft
161           when 0
162             bm.flash "No draft messages."
163           when 1
164             m = nil
165             Index.each_id_by_date(:label => :draft) { |mid, builder| m = builder.call }
166             r = ResumeMode.new(m)
167             BufferManager.spawn "Edit message", r
168             r.edit
169           else
170             b = BufferManager.spawn_unless_exists(:draft) do
171               mode = LabelSearchResultsMode.new [:draft]
172             end
173             b.mode.load_threads :num => b.content_height
174           end
175         when :nothing
176         when :redraw
177           bm.completely_redraw_screen
178         else
179           bm.flash "Unknown key press '#{c.to_character}' for #{bm.focus_buf.mode.name}."
180         end
181       end
182     end
183   end
184   bm.kill_all_buffers
185 rescue Exception => e
186   $exception ||= e
187 ensure
188   Redwood::finish
189   stop_cursing
190   
191   # don't ask me why, but sometimes it's necessary to print something
192   # to stderr at this point or the exception doesn't get printed.
193   # doesn't get printed. WHY?
194
195   $stderr.puts " " 
196 end
197
198 Index.save unless $exception # TODO: think about this
199
200 if $exception 
201   $stderr.puts <<EOS
202 ----------------------------------------------------------------
203 I'm very sorry, but it seems that an error occurred in Sup. 
204 Please accept my sincere apologies. If you don't mind, please
205 send the backtrace below and a brief report of the circumstances
206 to wmorgan-sup at masanjin dot nets so that I might address this
207 problem. Thank you!
208
209 Sincerely,
210 William
211 ----------------------------------------------------------------
212
213 The problem was: #{$exception.message} (error type #{$exception.class.name})
214 A backtrace follows:
215 EOS
216   raise $exception
217 end
218
219 end