]> git.cworth.org Git - sup/blobdiff - bin/sup
multiple concurrent process detection and resolution
[sup] / bin / sup
diff --git a/bin/sup b/bin/sup
index fac2f3bb65b860e95a00f6d5784129c15888ac6d..ab835e7ba4edcdec548cf43cef117ddafb6fe493 100644 (file)
--- a/bin/sup
+++ b/bin/sup
@@ -2,8 +2,23 @@
 
 require 'rubygems'
 require 'ncurses'
+require 'fileutils'
+require 'trollop'
 require "sup"
 
+$opts = Trollop::options do
+  version "sup v#{Redwood::VERSION}"
+  banner <<EOS
+Sup is a curses-based email client.
+
+Usage:
+  sup [options]
+
+Options are:
+EOS
+  opt :no_threads, "Turn of threading. Helps with debugging. (Necessarily disables background polling for new messages.)"
+end
+
 Thread.abort_on_exception = true # make debugging possible
 
 module Redwood
@@ -40,6 +55,30 @@ def stop_cursing
 end
 module_function :start_cursing, :stop_cursing
 
+begin
+  Redwood::lock
+rescue LockError => e
+  require 'highline'
+  h = HighLine.new
+  h.say <<EOS
+Error: sup is already running! User #{e.user} on host #{e.host} was running sup
+with pid #{e.pid} as of #{e.time}.
+EOS
+
+  case h.ask("Should I try and kill that process? ")
+  when /^\s*y\s*$/i
+    h.say "Ok, suggesting sepuku..."
+    FileUtils.touch Redwood::SUICIDE_FN
+    sleep SuicideManager::DELAY * 2
+    FileUtils.rm_f Redwood::SUICIDE_FN
+    h.say "Let's try that again."
+    retry
+  else
+    h.say "Ok, see you later."
+    exit
+  end
+end
+
 Redwood::start
 Index.new.load
 
@@ -65,6 +104,8 @@ begin
     c.add :index_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
     c.add :index_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK, 
            Ncurses::A_BOLD
+    c.add :index_starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK, 
+           Ncurses::A_BOLD
     c.add :labellist_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
     c.add :labellist_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK, 
            Ncurses::A_BOLD
@@ -83,6 +124,8 @@ begin
           Ncurses::A_BOLD
     c.add :starred_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_GREEN,
           Ncurses::A_BOLD
+    c.add :alternate_starred_patina_color, Ncurses::COLOR_YELLOW,
+          Ncurses::COLOR_BLUE, Ncurses::A_BOLD
     c.add :snippet_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
     c.add :option_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
     c.add :tagged_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
@@ -96,25 +139,36 @@ begin
 
   log "initializing mail index buffer"
   imode = InboxMode.new
-  ibuf = bm.spawn "inbox", imode
+  ibuf = bm.spawn "Inbox", imode
 
-  log "ready for (inter)action!"
+  log "ready for interaction!"
   Logger.make_buf
 
   bm.draw_screen
+
+  begin
+    Index.usual_sources.each { |s| s.check }
+  rescue SourceError
+    # do nothing! we'll report it at the next step
+  end
+  Redwood::report_broken_sources
+  
   Index.usual_sources.each do |s|
     reporting_thread do
       begin
         s.connect
       rescue SourceError => e
-        Redwood::log "Fatal error loading from #{s}: #{e.message}"
+        Redwood::log "fatal error loading from #{s}: #{e.message}"
       end
     end if s.respond_to? :connect
   end
 
   imode.load_threads :num => ibuf.content_height, :when_done => lambda { reporting_thread { sleep 1; PollManager.poll } }
 
-  PollManager.start_thread
+  unless $opts[:no_threads]
+    PollManager.start_thread
+    SuicideManager.start_thread
+  end
 
   until $exception
     bm.draw_screen
@@ -158,14 +212,14 @@ begin
             bm.flash "Couldn't parse query."
           end
         when :list_labels
-          b = bm.spawn_unless_exists("Label List") { LabelListMode.new }
+          b = bm.spawn_unless_exists("Label list") { LabelListMode.new }
           b.mode.load_in_background
         when :compose
           mode = ComposeMode.new
           bm.spawn "New Message", mode
           mode.edit
         when :poll
-          bm.raise_to_front PollManager.buffer
+#          bm.raise_to_front PollManager.buffer
           reporting_thread { PollManager.poll }
         when :recall_draft
           case Index.num_results_for :label => :draft
@@ -178,7 +232,7 @@ begin
             BufferManager.spawn "Edit message", r
             r.edit
           else
-            b = BufferManager.spawn_unless_exists(:draft) do
+            b = BufferManager.spawn_unless_exists("All drafts") do
               mode = LabelSearchResultsMode.new [:draft]
             end
             b.mode.load_threads :num => b.content_height
@@ -197,15 +251,20 @@ rescue Exception => e
 ensure
   Redwood::finish
   stop_cursing
-  
-  # don't ask me why, but sometimes it's necessary to print something
-  # to stderr at this point or the exception doesn't get printed.
-  # doesn't get printed. WHY?
 
-  $stderr.puts " " 
-end
+  case $exception
+  when SuicideException
+    Redwood::log "I've been asked to commit sepuku. I obey!"
+    exit
+  when nil
+    Redwood::log "good night, sweet prince!"
+    Index.save
+  else
+    Redwood::log "oh crap, an exception"
+  end
 
-Index.save unless $exception # TODO: think about this
+  Redwood::unlock
+end
 
 if $exception 
   $stderr.puts <<EOS