From 5debe177ddf8c0e4268f0e6b36f1eedc4b9cfd49 Mon Sep 17 00:00:00 2001 From: William Morgan Date: Mon, 3 Aug 2009 12:27:14 -0400 Subject: [PATCH] minor code style tweaks and whitespace removals --- bin/sup | 2 +- bin/sup-sync | 4 +--- bin/sup-sync-back | 2 +- bin/sup-tweak-labels | 17 ++++++++--------- lib/sup.rb | 2 +- lib/sup/buffer.rb | 4 ++-- lib/sup/crypto.rb | 26 ++++++++++++-------------- lib/sup/mbox/loader.rb | 2 +- lib/sup/modes/scroll-mode.rb | 4 ++-- lib/sup/modes/text-mode.rb | 4 ++-- lib/sup/poll.rb | 2 +- lib/sup/source.rb | 30 ++++++++++++++++++------------ lib/sup/thread.rb | 9 ++++----- lib/sup/util.rb | 6 +++--- 14 files changed, 57 insertions(+), 57 deletions(-) diff --git a/bin/sup b/bin/sup index 1febefd..786fba2 100755 --- a/bin/sup +++ b/bin/sup @@ -200,7 +200,7 @@ begin end end end unless $opts[:no_initial_poll] - + imode.load_threads :num => ibuf.content_height, :when_done => lambda { |num| reporting_thread("poll after loading inbox") { sleep 1; PollManager.poll } unless $opts[:no_threads] || $opts[:no_initial_poll] } if $opts[:compose] diff --git a/bin/sup-sync b/bin/sup-sync index a8cb768..d445bd8 100755 --- a/bin/sup-sync +++ b/bin/sup-sync @@ -9,9 +9,7 @@ PROGRESS_UPDATE_INTERVAL = 15 # seconds class Float def to_s; sprintf '%.2f', self; end - def to_time_s - infinite? ? "unknown" : super - end + def to_time_s; infinite? ? "unknown" : super end end class Numeric diff --git a/bin/sup-sync-back b/bin/sup-sync-back index adadcb3..7635274 100755 --- a/bin/sup-sync-back +++ b/bin/sup-sync-back @@ -108,7 +108,7 @@ EOS source.reset! num_dropped = num_moved = num_scanned = 0 - + out_fp = Tempfile.new "sup-sync-back-#{source.id}" Redwood::PollManager.each_message_from source do |m| num_scanned += 1 diff --git a/bin/sup-tweak-labels b/bin/sup-tweak-labels index b2c6b1d..c9ad8e8 100755 --- a/bin/sup-tweak-labels +++ b/bin/sup-tweak-labels @@ -38,8 +38,8 @@ be seen by running "sup-add --help". Options: EOS - opt :add, "One or more labels (comma-separated) to add to every message from the specified sources", :type => String - opt :remove, "One or more labels (comma-separated) to remove from every message from the specified sources, if those labels are present", :type => String + opt :add, "One or more labels (comma-separated) to add to every message from the specified sources", :default => "" + opt :remove, "One or more labels (comma-separated) to remove from every message from the specified sources, if those labels are present", :default => "" opt :query, "A Sup search query", :type => String text < 10).map { |c| [c.full_address, c.email] } contacts = ContactManager.contacts.map { |c| [ContactManager.alias_for(c), c.full_address, c.email] } diff --git a/lib/sup/crypto.rb b/lib/sup/crypto.rb index 8ec277b..f68009d 100644 --- a/lib/sup/crypto.rb +++ b/lib/sup/crypto.rb @@ -116,21 +116,19 @@ class CryptoManager output = run_gpg "--decrypt #{payload_fn.path}" if $?.success? - decrypted_payload, sig_lines = - if output =~ /\A(.*?)((^gpg: .*$)+)\Z/m - [$1, $2] + decrypted_payload, sig_lines = if output =~ /\A(.*?)((^gpg: .*$)+)\Z/m + [$1, $2] + else + [output, nil] + end + + sig = if sig_lines # encrypted & signed + if sig_lines =~ /^gpg: (Good signature from .*$)/ + Chunk::CryptoNotice.new :valid, $1, sig_lines.split("\n") else - [output, nil] - end - - sig = - if sig_lines # encrypted & signed - if sig_lines =~ /^gpg: (Good signature from .*$)/ - Chunk::CryptoNotice.new :valid, $1, sig_lines.split("\n") - else - Chunk::CryptoNotice.new :invalid, $1, sig_lines.split("\n") - end + Chunk::CryptoNotice.new :invalid, $1, sig_lines.split("\n") end + end notice = Chunk::CryptoNotice.new :valid, "This message has been decrypted for display" [RMail::Parser.read(decrypted_payload), sig, notice] @@ -145,7 +143,7 @@ private def unknown_status lines=[] Chunk::CryptoNotice.new :unknown, "Unable to determine validity of cryptographic signature", lines end - + def cant_find_binary ["Can't find gpg binary in path."] end diff --git a/lib/sup/mbox/loader.rb b/lib/sup/mbox/loader.rb index f35c7df..26177f7 100644 --- a/lib/sup/mbox/loader.rb +++ b/lib/sup/mbox/loader.rb @@ -47,7 +47,7 @@ class Loader < Source raise OutOfSyncSourceError, "mbox file is smaller than last recorded message offset. Messages have probably been deleted by another client." end end - + def start_offset; 0; end def end_offset; File.size @f; end diff --git a/lib/sup/modes/scroll-mode.rb b/lib/sup/modes/scroll-mode.rb index 63b48ec..c131425 100644 --- a/lib/sup/modes/scroll-mode.rb +++ b/lib/sup/modes/scroll-mode.rb @@ -3,7 +3,7 @@ module Redwood class ScrollMode < Mode ## we define topline and botline as the top and bottom lines of any ## content in the currentview. - + ## we left leftcol and rightcol as the left and right columns of any ## content in the current view. but since we're operating in a ## line-centric fashion, rightcol is always leftcol + the buffer @@ -223,7 +223,7 @@ protected raise "nil text for color '#{color}'" if text.nil? # good for debugging l = text.display_length no_fill = i != a.size - 1 - + if xpos + l < @leftcol buffer.write ln - @topline, 0, "", :color => color, :highlight => opts[:highlight] diff --git a/lib/sup/modes/text-mode.rb b/lib/sup/modes/text-mode.rb index 273c028..7c9e7d8 100644 --- a/lib/sup/modes/text-mode.rb +++ b/lib/sup/modes/text-mode.rb @@ -14,7 +14,7 @@ class TextMode < ScrollMode buffer.mark_dirty if buffer super() end - + def save_to_disk fn = BufferManager.ask_for_filename :filename, "Save to file: ", @filename save_to_file(fn) { |f| f.puts text } if fn @@ -50,7 +50,7 @@ class TextMode < ScrollMode @lines << @text.length if buffer ensure_mode_validity - buffer.mark_dirty + buffer.mark_dirty end end diff --git a/lib/sup/poll.rb b/lib/sup/poll.rb index 0c8f51d..4374242 100644 --- a/lib/sup/poll.rb +++ b/lib/sup/poll.rb @@ -35,7 +35,7 @@ EOS @thread = nil @last_poll = nil @polling = false - + self.class.i_am_the_instance self end diff --git a/lib/sup/source.rb b/lib/sup/source.rb index 1bb7797..fb9089f 100644 --- a/lib/sup/source.rb +++ b/lib/sup/source.rb @@ -34,12 +34,12 @@ class Source ## To write a new source, subclass this class, and implement: ## ## - start_offset - ## - end_offset (exclusive!) + ## - end_offset (exclusive!) (or, #done?) ## - load_header offset ## - load_message offset ## - raw_header offset ## - raw_message offset - ## - check + ## - check (optional) ## - next (or each, if you prefer): should return a message and an ## array of labels. ## @@ -78,6 +78,7 @@ class Source @dirty = false end + ## overwrite me if you have a disk incarnation (currently used only for sup-sync-back) def file_path; nil end def to_s; @uri.to_s; end @@ -92,20 +93,23 @@ class Source ## to proactively notify the user of any source problems. def check; end + ## yields successive offsets and labels, starting at #cur_offset. + ## + ## when implementing a source, you can overwrite either #each or #next. the + ## default #each just calls next over and over. def each self.cur_offset ||= start_offset until done? - n, labels = self.next - raise "no message" unless n - yield n, labels + offset, labels = self.next + yield offset, labels end end - ## read a raw email header from a filehandle (or anything that responds to - ## #gets), and turn it into a hash of key-value pairs. + ## utility method to read a raw email header from an IO stream and turn it + ## into a hash of key-value pairs. minor special semantics for certain headers. ## - ## WARNING! THIS IS A SPEED-CRITICAL SECTION. Everything you do here will have - ## a significant effect on Sup's processing speed of email from ALL sources. + ## THIS IS A SPEED-CRITICAL SECTION. Everything you do here will have a + ## significant effect on Sup's processing speed of email from ALL sources. ## Little things like string interpolation, regexp interpolation, += vs <<, ## all have DRAMATIC effects. BE CAREFUL WHAT YOU DO! def self.parse_raw_email_header f @@ -116,9 +120,11 @@ class Source case line ## these three can occur multiple times, and we want the first one when /^(Delivered-To|X-Original-To|Envelope-To):\s*(.*?)\s*$/i; header[last = $1.downcase] ||= $2 - ## mark this guy specially. not sure why i care. + ## regular header: overwrite (not that we should see more than one) + ## TODO: figure out whether just using the first occurrence changes + ## anything (which would simplify the logic slightly) when /^([^:\s]+):\s*(.*?)\s*$/i; header[last = $1.downcase] = $2 - when /^\r*$/; break + when /^\r*$/; break # blank line signifies end of header else if last header[last] << " " unless header[last].empty? @@ -144,7 +150,7 @@ protected ## convenience function def parse_raw_email_header f; self.class.parse_raw_email_header f end - + def Source.expand_filesystem_uri uri uri.gsub "~", File.expand_path("~") end diff --git a/lib/sup/thread.rb b/lib/sup/thread.rb index 81ce7e8..ca3f30d 100644 --- a/lib/sup/thread.rb +++ b/lib/sup/thread.rb @@ -103,15 +103,14 @@ class Thread def toggle_label label if has_label? label remove_label label - return false + false else apply_label label - return true + true end end def set_labels l; each { |m, *o| m && m.labels = l }; end - def has_label? t; any? { |m, *o| m && m.has_label?(t) }; end def save_state index; each { |m, *o| m && m.save_state(index) }; end @@ -132,7 +131,7 @@ class Thread end def latest_message - inject(nil) do |a, b| + inject(nil) do |a, b| b = b.first if a.nil? b @@ -163,7 +162,7 @@ class Container @id = id @message, @parent, @thread = nil, nil, nil @children = [] - end + end def each_with_stuff parent=nil yield self, 0, parent diff --git a/lib/sup/util.rb b/lib/sup/util.rb index c3cc4b2..5776d7e 100644 --- a/lib/sup/util.rb +++ b/lib/sup/util.rb @@ -216,8 +216,8 @@ class String newpos = case state when :escaped_instring, :escaped_outstring then pos else index(/[,"\\]/, pos) - end - + end + if newpos char = self[newpos] else @@ -542,7 +542,7 @@ class Recoverable def has_errors?; !@error.nil?; end def method_missing m, *a, &b; __pass m, *a, &b end - + def id; __pass :id; end def to_s; __pass :to_s; end def to_yaml x; __pass :to_yaml, x; end -- 2.45.2