]> git.cworth.org Git - sup/blob - bin/sup
2038fbdf1d656aee4e68314ad060b0dd795de5ba
[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 :alternate_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_BLUE
75     c.add :missing_message_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_RED
76     c.add :mime_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
77     c.add :quote_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
78     c.add :sig_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
79     c.add :quote_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
80     c.add :sig_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
81     c.add :to_me_color, Ncurses::COLOR_GREEN, Ncurses::COLOR_BLACK
82     c.add :starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
83           Ncurses::A_BOLD
84     c.add :starred_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_GREEN,
85           Ncurses::A_BOLD
86     c.add :snippet_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
87     c.add :option_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
88     c.add :tagged_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
89           Ncurses::A_BOLD
90     c.add :draft_notification_color, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK,
91           Ncurses::A_BOLD
92   end
93
94   log "initializing buffer manager"
95   bm = BufferManager.new
96
97   log "initializing mail index buffer"
98   imode = InboxMode.new
99   ibuf = bm.spawn "inbox", imode
100
101   log "ready for (inter)action!"
102   Logger.make_buf
103
104   bm.draw_screen
105   Index.usual_sources.each do |s|
106     reporting_thread do
107       begin
108         s.connect
109       rescue SourceError => e
110         Redwood::log "Fatal error loading from #{s}: #{e.message}"
111       end
112     end if s.respond_to? :connect
113   end
114
115   imode.load_threads :num => ibuf.content_height, :when_done => lambda { reporting_thread { sleep 1; PollManager.poll } }
116
117   PollManager.start_thread
118
119   until $exception
120     bm.draw_screen
121     c = Ncurses.nonblocking_getch
122     bm.erase_flash
123
124     if c == Ncurses::KEY_RESIZE
125       bm.handle_resize
126     elsif c
127       unless bm.handle_input(c)
128         x = global_keymap.action_for c
129         case x
130         when :quit
131           break
132         when :help
133           curmode = bm.focus_buf.mode
134           bm.spawn_unless_exists("<help for #{curmode.name}>") { HelpMode.new curmode, global_keymap }
135         when :roll_buffers
136           bm.roll_buffers
137         when :roll_buffers_backwards
138           bm.roll_buffers_backwards
139         when :kill_buffer
140           bm.kill_buffer bm.focus_buf if bm.focus_buf.mode.killable?
141         when :list_buffers
142           bm.spawn_unless_exists("Buffer List") { BufferListMode.new }
143         when :list_contacts
144           b = bm.spawn_unless_exists("Contact List") { ContactListMode.new }
145           b.mode.load_in_background
146         when :search
147           text = bm.ask :search, "query: "
148           next unless text && text !~ /^\s*$/
149
150           begin
151             qobj = Index.parse_user_query_string text
152             short_text = text.length < 20 ? text : text[0 ... 20] + "..."
153             log "built query from #{text.inspect}: #{qobj}"
154             mode = SearchResultsMode.new qobj
155             bm.spawn "search: \"#{short_text}\"", mode
156             mode.load_threads :num => mode.buffer.content_height
157           rescue Ferret::QueryParser::QueryParseException => e
158             bm.flash "Couldn't parse query."
159           end
160         when :list_labels
161           b = bm.spawn_unless_exists("Label List") { LabelListMode.new }
162           b.mode.load_in_background
163         when :compose
164           mode = ComposeMode.new
165           bm.spawn "New Message", mode
166           mode.edit
167         when :poll
168           bm.raise_to_front PollManager.buffer
169           reporting_thread { PollManager.poll }
170         when :recall_draft
171           case Index.num_results_for :label => :draft
172           when 0
173             bm.flash "No draft messages."
174           when 1
175             m = nil
176             Index.each_id_by_date(:label => :draft) { |mid, builder| m = builder.call }
177             r = ResumeMode.new(m)
178             BufferManager.spawn "Edit message", r
179             r.edit
180           else
181             b = BufferManager.spawn_unless_exists(:draft) do
182               mode = LabelSearchResultsMode.new [:draft]
183             end
184             b.mode.load_threads :num => b.content_height
185           end
186         when :nothing
187         when :redraw
188           bm.completely_redraw_screen
189         else
190           bm.flash "Unknown key press '#{c.to_character}' for #{bm.focus_buf.mode.name}."
191         end
192       end
193     end
194   end
195   bm.kill_all_buffers
196 rescue Exception => e
197   $exception ||= e
198 ensure
199   Redwood::finish
200   stop_cursing
201   
202   # don't ask me why, but sometimes it's necessary to print something
203   # to stderr at this point or the exception doesn't get printed.
204   # doesn't get printed. WHY?
205
206   $stderr.puts " " 
207 end
208
209 Index.save unless $exception # TODO: think about this
210
211 if $exception 
212   $stderr.puts <<EOS
213 ----------------------------------------------------------------
214 I'm very sorry, but it seems that an error occurred in Sup. 
215 Please accept my sincere apologies. If you don't mind, please
216 send the backtrace below and a brief report of the circumstances
217 to wmorgan-sup at masanjin dot nets so that I might address this
218 problem. Thank you!
219
220 Sincerely,
221 William
222 ----------------------------------------------------------------
223
224 The problem was: #{$exception.message} (error type #{$exception.class.name})
225 A backtrace follows:
226 EOS
227   raise $exception
228 end
229
230 end