lib/sup/modes/thread-index-mode.rb in sup-0.2 vs lib/sup/modes/thread-index-mode.rb in sup-0.3
- old
+ new
@@ -3,13 +3,19 @@
## subclasses should implement:
## - is_relevant?
class ThreadIndexMode < LineCursorMode
DATE_WIDTH = Time::TO_NICE_S_MAX_LEN
- FROM_WIDTH = 15
+ MIN_FROM_WIDTH = 15
LOAD_MORE_THREAD_NUM = 20
+ HookManager.register "index-mode-size-widget", <<EOS
+Generates the per-thread size widget for each thread.
+Variables:
+ thread: The message thread to be formatted.
+EOS
+
register_keymap do |k|
k.add :load_threads, "Load #{LOAD_MORE_THREAD_NUM} more threads", 'M'
k.add :reload, "Refresh view", '@'
k.add :toggle_archived, "Toggle archived status", 'a'
k.add :toggle_starred, "Star or unstar all messages in thread", '*'
@@ -21,28 +27,32 @@
k.add :kill, "Kill thread (never to be seen in inbox again)", '&'
k.add :save, "Save changes now", '$'
k.add :jump_to_next_new, "Jump to next new thread", :tab
k.add :reply, "Reply to latest message in a thread", 'r'
k.add :forward, "Forward latest message in a thread", 'f'
- k.add :toggle_tagged, "Tag/untag current line", 't'
+ k.add :toggle_tagged, "Tag/untag selected thread", 't'
+ k.add :toggle_tagged_all, "Tag/untag all threads", 'T'
k.add :apply_to_tagged, "Apply next command to all tagged threads", ';'
end
def initialize hidden_labels=[], load_thread_opts={}
super()
- @mutex = Mutex.new
+ @mutex = Mutex.new # covers the following variables:
+ @threads = {}
+ @hidden_threads = {}
+ @size_widget_width = nil
+ @size_widgets = {}
+ @tags = Tagger.new self
+
+ ## these guys, and @text and @lines, are not covered
@load_thread = nil
@load_thread_opts = load_thread_opts
@hidden_labels = hidden_labels + LabelManager::HIDDEN_RESERVED_LABELS
@date_width = DATE_WIDTH
- @from_width = FROM_WIDTH
- @size_width = nil
- @tags = Tagger.new self
-
- initialize_threads
- update
+ initialize_threads # defines @ts and @ts_mutex
+ update # defines @text and @lines
UpdateManager.register self
@last_load_more_size = nil
to_load_more do |size|
@@ -53,26 +63,31 @@
end
end
def lines; @text.length; end
def [] i; @text[i]; end
- def contains_thread? t; !@lines[t].nil?; end
+ def contains_thread? t; @threads.include?(t) end
def reload
drop_all_threads
BufferManager.draw_screen
load_threads :num => buffer.content_height
end
## open up a thread view window
def select t=nil
- t ||= @threads[curpos] or return
+ t ||= cursor_thread or return
- ## TODO: don't regen text completely
- Redwood::reporting_thread do
- BufferManager.say("Loading message bodies...") do |sid|
- t.each { |m, *o| m.load_from_source! if m }
+ Redwood::reporting_thread("load messages for thread-view-mode") do
+ num = t.size
+ message = "Loading #{num.pluralize 'message body'}..."
+ BufferManager.say(message) do |sid|
+ t.each_with_index do |(m, *o), i|
+ next unless m
+ BufferManager.say "#{message} (#{i}/#{num})", sid if t.size > 1
+ m.load_from_source!
+ end
end
mode = ThreadViewMode.new t, @hidden_labels
BufferManager.spawn t.subj, mode
BufferManager.draw_screen
mode.jump_to_first_open
@@ -88,19 +103,23 @@
def multi_select threads
threads.each { |t| select t }
end
def handle_label_update sender, m
- t = @ts.thread_for(m) or return
+ t = @ts_mutex.synchronize { @ts.thread_for(m) } or return
+ handle_label_thread_update sender, t
+ end
+
+ def handle_label_thread_update sender, t
l = @lines[t] or return
update_text_for_line l
BufferManager.draw_screen
end
def handle_read_update sender, t
l = @lines[t] or return
- update_text_for_line @lines[t]
+ update_text_for_line l
BufferManager.draw_screen
end
def handle_archived_update *a; handle_read_update(*a); end
@@ -112,34 +131,40 @@
## overwrite me!
def is_relevant? m; false; end
def handle_add_update sender, m
- if is_relevant?(m) || @ts.is_relevant?(m)
+ @ts_mutex.synchronize do
+ return unless is_relevant?(m) || @ts.is_relevant?(m)
@ts.load_thread_for_message m
- update
- BufferManager.draw_screen
end
+ update
+ BufferManager.draw_screen
end
def handle_delete_update sender, mid
- if @ts.contains_id? mid
+ @ts_mutex.synchronize do
+ return unless @ts.contains_id? mid
@ts.remove mid
- update
- BufferManager.draw_screen
end
+ update
+ BufferManager.draw_screen
end
def update
- ## let's see you do THIS in python
- @threads = @ts.threads.select { |t| !@hidden_threads[t] }.sort_by { |t| t.date }.reverse
- @size_width = (@threads.max_of { |t| t.size } || 0).num_digits
+ @mutex.synchronize do
+ ## let's see you do THIS in python
+ @threads = @ts.threads.select { |t| !@hidden_threads[t] }.sort_by { |t| t.date }.reverse
+ @size_widgets = @threads.map { |t| size_widget_for_thread t }
+ @size_widget_width = @size_widgets.max_of { |w| w.length }
+ end
+
regen_text
end
def edit_message
- return unless(t = @threads[curpos])
+ return unless(t = cursor_thread)
message, *crap = t.find { |m, *o| m.has_label? :draft }
if message
mode = ResumeMode.new message
BufferManager.spawn "Edit message", mode
else
@@ -156,11 +181,11 @@
UpdateManager.relay self, :starred, t
end
end
def toggle_starred
- t = @threads[curpos] or return
+ t = cursor_thread or return
actually_toggle_starred t
update_text_for_line curpos
cursor_down
end
@@ -198,22 +223,22 @@
UpdateManager.relay self, :deleted, t
end
end
def toggle_archived
- t = @threads[curpos] or return
+ t = cursor_thread or return
actually_toggle_archived t
update_text_for_line curpos
end
def multi_toggle_archived threads
threads.each { |t| actually_toggle_archived t }
regen_text
end
def toggle_new
- t = @threads[curpos] or return
+ t = cursor_thread or return
t.toggle_label :unread
update_text_for_line curpos
cursor_down
end
@@ -221,27 +246,30 @@
threads.each { |t| t.toggle_label :unread }
regen_text
end
def multi_toggle_tagged threads
- @tags.drop_all_tags
+ @mutex.synchronize { @tags.drop_all_tags }
regen_text
end
def jump_to_next_new
- n = ((curpos + 1) ... lines).find { |i| @threads[i].has_label? :unread } || (0 ... curpos).find { |i| @threads[i].has_label? :unread }
+ n = @mutex.synchronize do
+ ((curpos + 1) ... lines).find { |i| @threads[i].has_label? :unread } ||
+ (0 ... curpos).find { |i| @threads[i].has_label? :unread }
+ end
if n
## jump there if necessary
jump_to_line n unless n >= topline && n < botline
set_cursor_pos n
else
BufferManager.flash "No new messages"
end
end
def toggle_spam
- t = @threads[curpos] or return
+ t = cursor_thread or return
multi_toggle_spam [t]
end
## both spam and deleted have the curious characteristic that you
## always want to hide the thread after either applying or removing
@@ -257,11 +285,11 @@
end
regen_text
end
def toggle_deleted
- t = @threads[curpos] or return
+ t = cursor_thread or return
multi_toggle_deleted [t]
end
## see comment for multi_toggle_spam
def multi_toggle_deleted threads
@@ -271,25 +299,25 @@
end
regen_text
end
def kill
- t = @threads[curpos] or return
+ t = cursor_thread or return
multi_kill [t]
end
def multi_kill threads
threads.each do |t|
t.apply_label :killed
hide_thread t
end
regen_text
- BufferManager.flash "Thread#{threads.size == 1 ? '' : 's'} killed."
+ BufferManager.flash "#{threads.size.pluralize 'Thread'} killed."
end
def save
- dirty_threads = (@threads + @hidden_threads.keys).select { |t| t.dirty? }
+ dirty_threads = @mutex.synchronize { (@threads + @hidden_threads.keys).select { |t| t.dirty? } }
return if dirty_threads.empty?
BufferManager.say("Saving threads...") do |say_id|
dirty_threads.each_with_index do |t, i|
BufferManager.say "Saving modified thread #{i + 1} of #{dirty_threads.length}...", say_id
@@ -310,20 +338,25 @@
save
super
end
def toggle_tagged
- t = @threads[curpos] or return
- @tags.toggle_tag_for t
+ t = cursor_thread or return
+ @mutex.synchronize { @tags.toggle_tag_for t }
update_text_for_line curpos
cursor_down
end
+
+ def toggle_tagged_all
+ @mutex.synchronize { @threads.each { |t| @tags.toggle_tag_for t } }
+ regen_text
+ end
def apply_to_tagged; @tags.apply_to_tagged; end
def edit_labels
- thread = @threads[curpos] or return
+ thread = cursor_thread or return
speciall = (@hidden_labels + LabelManager::RESERVED_LABELS).uniq
keepl, modifyl = thread.labels.partition { |t| speciall.member? t }
user_labels = BufferManager.ask_for_labels :label, "Labels for thread: ", modifyl, @hidden_labels
@@ -347,44 +380,43 @@
end
regen_text
end
def reply
- t = @threads[curpos] or return
+ t = cursor_thread or return
m = t.latest_message
return if m.nil? # probably won't happen
m.load_from_source!
mode = ReplyMode.new m
BufferManager.spawn "Reply to #{m.subj}", mode
end
def forward
- t = @threads[curpos] or return
+ t = cursor_thread or return
m = t.latest_message
return if m.nil? # probably won't happen
m.load_from_source!
- mode = ForwardMode.new m
- BufferManager.spawn "Forward of #{m.subj}", mode
- mode.edit_message
+ ForwardMode.spawn_nicely m
end
def load_n_threads_background n=LOAD_MORE_THREAD_NUM, opts={}
return if @load_thread # todo: wrap in mutex
- @load_thread = Redwood::reporting_thread do
+ @load_thread = Redwood::reporting_thread("load threads for thread-index-mode") do
num = load_n_threads n, opts
opts[:when_done].call(num) if opts[:when_done]
@load_thread = nil
end
end
+ ## TODO: figure out @ts_mutex in this method
def load_n_threads n=LOAD_MORE_THREAD_NUM, opts={}
@mbid = BufferManager.say "Searching for threads..."
orig_size = @ts.size
- last_update = Time.now - 9999 # oh yeah
+ last_update = Time.now
@ts.load_n_threads(@ts.size + n, opts) do |i|
- BufferManager.say "Loaded #{i} threads...", @mbid
if (Time.now - last_update) >= 0.25
+ BufferManager.say "Loaded #{i.pluralize 'thread'}...", @mbid
update
BufferManager.draw_screen
last_update = Time.now
end
end
@@ -394,11 +426,11 @@
BufferManager.clear @mbid
@mbid = nil
BufferManager.draw_screen
@ts.size - orig_size
end
- synchronized :load_n_threads
+ ignore_concurrent_calls :load_n_threads
def status
if (l = lines) == 0
"line 0 of 0"
else
@@ -410,11 +442,11 @@
n = opts[:num] || ThreadIndexMode::LOAD_MORE_THREAD_NUM
myopts = @load_thread_opts.merge({ :when_done => (lambda do |num|
opts[:when_done].call(num) if opts[:when_done]
if num > 0
- BufferManager.flash "Found #{num} threads."
+ BufferManager.flash "Found #{num.pluralize 'thread'}."
else
BufferManager.flash "No matches."
end
end)})
@@ -422,100 +454,178 @@
load_n_threads_background n, myopts
else
load_n_threads n, myopts
end
end
+ ignore_concurrent_calls :load_threads
+ def resize rows, cols
+ regen_text
+ super
+ end
+
protected
- def cursor_thread; @threads[curpos]; end
+ def size_widget_for_thread t
+ HookManager.run("index-mode-size-widget", :thread => t) || default_size_widget_for(t)
+ end
+ def cursor_thread; @mutex.synchronize { @threads[curpos] }; end
+
def drop_all_threads
@tags.drop_all_tags
initialize_threads
update
end
def hide_thread t
- raise "already hidden" if @hidden_threads[t]
- @hidden_threads[t] = true
- @threads.delete t
- @tags.drop_tag_for t
- end
-
- def show_thread t
- if @hidden_threads[t]
- @hidden_threads.delete t
- else
- @ts.add_thread t
+ @mutex.synchronize do
+ i = @threads.index(t) or return
+ raise "already hidden" if @hidden_threads[t]
+ @hidden_threads[t] = true
+ @threads.delete_at i
+ @size_widgets.delete_at i
+ @tags.drop_tag_for t
end
- update
end
def update_text_for_line l
return unless l # not sure why this happens, but it does, occasionally
- @text[l] = text_for_thread @threads[l]
- buffer.mark_dirty if buffer
+
+ need_update = false
+
+ @mutex.synchronize do
+ @size_widgets[l] = size_widget_for_thread @threads[l]
+
+ ## if the widget size has increased, we need to redraw everyone
+ need_update = @size_widgets[l].size > @size_widget_width
+ end
+
+ if need_update
+ update
+ else
+ @text[l] = text_for_thread_at l
+ buffer.mark_dirty if buffer
+ end
end
def regen_text
- @text = @threads.map_with_index { |t, i| text_for_thread t }
- @lines = @threads.map_with_index { |t, i| [t, i] }.to_h
+ threads = @mutex.synchronize { @threads }
+ @text = threads.map_with_index { |t, i| text_for_thread_at i }
+ @lines = threads.map_with_index { |t, i| [t, i] }.to_h
buffer.mark_dirty if buffer
end
- def author_text_for_thread t
- t.authors.map do |p|
- if AccountManager.is_account?(p)
- "me"
- elsif t.authors.size == 1
- p.mediumname
- else
- p.shortname
- end
- end.uniq.join ","
+ def authors; map { |m, *o| m.from if m }.compact.uniq; end
+
+ def author_names_and_newness_for_thread t
+ new = {}
+ authors = t.map do |m, *o|
+ next unless m
+
+ name =
+ if AccountManager.is_account?(m.from)
+ "me"
+ elsif t.authors.size == 1
+ m.from.mediumname
+ else
+ m.from.shortname
+ end
+
+ new[name] ||= m.has_label?(:unread)
+ name
+ end
+
+ authors.compact.uniq.map { |a| [a, new[a]] }
end
- def text_for_thread t
+ def text_for_thread_at line
+ t, size_widget = @mutex.synchronize { [@threads[line], @size_widgets[line]] }
+
date = t.date.to_nice_s
- from = author_text_for_thread t
- if from.length > @from_width
- from = from[0 ... (@from_width - 1)]
- from += "." unless from[-1] == ?\s
- end
new = t.has_label?(:unread)
starred = t.has_label?(:starred)
+ ## format the from column
+ cur_width = 0
+ ann = author_names_and_newness_for_thread t
+ from = []
+ ann.each_with_index do |(name, newness), i|
+ break if cur_width >= from_width
+ last = i == ann.length - 1
+
+ abbrev =
+ if cur_width + name.length > from_width
+ name[0 ... (from_width - cur_width - 1)] + "."
+ elsif cur_width + name.length == from_width
+ name[0 ... (from_width - cur_width)]
+ else
+ if last
+ name[0 ... (from_width - cur_width)]
+ else
+ name[0 ... (from_width - cur_width - 1)] + ","
+ end
+ end
+
+ cur_width += abbrev.length
+
+ if last && from_width > cur_width
+ abbrev += " " * (from_width - cur_width)
+ end
+
+ from << [(newness ? :index_new_color : (starred ? :index_starred_color : :index_old_color)), abbrev]
+ end
+
dp = t.direct_participants.any? { |p| AccountManager.is_account? p }
p = dp || t.participants.any? { |p| AccountManager.is_account? p }
- base_color =
+ subj_color =
if new
:index_new_color
elsif starred
:index_starred_color
else
:index_old_color
end
+ snippet = t.snippet + (t.snippet.empty? ? "" : "...")
+
+ size_widget_text = sprintf "%#{ @size_widget_width}s", size_widget
+
[
[:tagged_color, @tags.tagged?(t) ? ">" : " "],
[:none, sprintf("%#{@date_width}s", date)],
(starred ? [:starred_color, "*"] : [:none, " "]),
- [base_color, sprintf("%-#{@from_width}s", from)],
- [:none, t.size == 1 ? " " * (@size_width + 2) : sprintf("(%#{@size_width}d)", t.size)],
+ ] +
+ from +
+ [
+ [subj_color, size_widget_text],
[:to_me_color, dp ? " >" : (p ? ' +' : " ")],
- [base_color, t.subj + (t.subj.empty? ? "" : " ")],
+ [subj_color, t.subj + (t.subj.empty? ? "" : " ")],
] +
(t.labels - @hidden_labels).map { |label| [:label_color, "+#{label} "] } +
- [[:snippet_color, t.snippet]
+ [[:snippet_color, snippet]
]
+
end
- def dirty?; (@hidden_threads.keys + @threads).any? { |t| t.dirty? }; end
+ def dirty?; @mutex.synchronize { (@hidden_threads.keys + @threads).any? { |t| t.dirty? } } end
private
+
+ def default_size_widget_for t
+ case t.size
+ when 1
+ ""
+ else
+ "(#{t.size})"
+ end
+ end
+
+ def from_width
+ [(buffer.content_width.to_f * 0.2).to_i, MIN_FROM_WIDTH].max
+ end
def initialize_threads
@ts = ThreadSet.new Index.instance, $config[:thread_by_subject]
@ts_mutex = Mutex.new
@hidden_threads = {}