Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle utf-8 completions and r/w encodings for labels and contacts #120

Merged
merged 3 commits into from
Aug 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion bin/sup
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8

#!/usr/bin/env ruby

$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
Expand Down Expand Up @@ -416,7 +418,7 @@ unless Redwood::exceptions.empty?
We are very sorry. It seems that an error occurred in Sup. Please
accept our sincere apologies. Please submit the contents of
#{BASE_DIR}/exception-log.txt and a brief report of the
circumstances to https://github.com/sup-heliotrope/sup/issues so that
circumstances to https://github.com/sup-heliotrope/sup/issues so that
we might address this problem. Thank you!

Sincerely,
Expand Down
2 changes: 2 additions & 0 deletions lib/sup.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8

require 'rubygems'
require 'yaml'
YAML::ENGINE.yamler = 'psych'
Expand Down
9 changes: 6 additions & 3 deletions lib/sup/buffer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8

require 'etc'
require 'thread'

Expand Down Expand Up @@ -466,7 +468,7 @@ def ask_many_with_completions domain, question, completions, default=nil

prefix.fix_encoding
target.fix_encoding
completions.select { |x| x =~ /^#{Regexp::escape target}/i }.map { |x| [prefix + x, x] }
completions.select { |x| x =~ /^#{Regexp::escape target}/iu }.map { |x| [prefix + x, x] }
end
end

Expand All @@ -479,7 +481,7 @@ def ask_many_emails_with_completions domain, question, completions, default=nil
prefix = prefix.join(", ") + (prefix.empty? ? "" : ", ")
prefix.fix_encoding

completions.select { |x| x =~ /^#{Regexp::escape target}/i }.sort_by { |c| [ContactManager.contact_for(c) ? 0 : 1, c] }.map { |x| [prefix + x, x] }
completions.select { |x| x =~ /^#{Regexp::escape target}/iu }.sort_by { |c| [ContactManager.contact_for(c) ? 0 : 1, c] }.map { |x| [prefix + x, x] }
end
end

Expand All @@ -492,7 +494,7 @@ def ask_for_filename domain, question, default=nil, allow_directory=false
if dir
[[s.sub(full, dir), "~#{name}"]]
else
users.select { |u| u =~ /^#{Regexp::escape name}/ }.map do |u|
users.select { |u| u =~ /^#{Regexp::escape name}/u }.map do |u|
[s.sub("~#{name}", "~#{u}"), "~#{u}"]
end
end
Expand Down Expand Up @@ -551,6 +553,7 @@ def ask_for_contacts domain, question, default_contacts=[]

completions = (recent + contacts).flatten.uniq
completions += HookManager.run("extra-contact-addresses") || []

answer = BufferManager.ask_many_emails_with_completions domain, question, completions, default

if answer
Expand Down
4 changes: 3 additions & 1 deletion lib/sup/contact.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8

module Redwood

class ContactManager
Expand Down Expand Up @@ -54,7 +56,7 @@ def person_for email; @e2p[email] end
def is_aliased_contact? person; !@p2a[person].nil? end

def save
File.open(@fn, "w") do |f|
File.open(@fn, "w:UTF-8") do |f|
@p2a.sort_by { |(p, a)| [p.full_address, a] }.each do |(p, a)|
f.puts "#{a || ''}: #{p.full_address}"
end
Expand Down
4 changes: 3 additions & 1 deletion lib/sup/label.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8

module Redwood

class LabelManager
Expand Down Expand Up @@ -77,7 +79,7 @@ def delete t

def save
return unless @modified
File.open(@fn, "w") { |f| f.puts @labels.keys.sort_by { |l| l.to_s } }
File.open(@fn, "w:UTF-8") { |f| f.puts @labels.keys.sort_by { |l| l.to_s } }
@new_labels = {}
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/sup/modes/compose_mode.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8

module Redwood

class ComposeMode < EditMessageMode
Expand Down
9 changes: 9 additions & 0 deletions lib/sup/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,15 @@ def wrap len
#
# Not Ruby 1.8 compatible
def fix_encoding
# first try to set the string to utf-8 and check if it is valid
# in case ruby just thinks it is something else
orig_encoding = encoding
force_encoding 'UTF-8'
return self if valid_encoding?

# that didn't work, go back to original and try to convert
force_encoding orig_encoding

encode!('UTF-8', :invalid => :replace, :undef => :replace)

# do this anyway in case string is set to be UTF-8, encoding to
Expand Down