Skip to content
This repository has been archived by the owner on Mar 14, 2021. It is now read-only.

Commit

Permalink
Added compact output format
Browse files Browse the repository at this point in the history
  • Loading branch information
tsdh committed Mar 5, 2008
1 parent 7f2f097 commit 1b91a20
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions rdictcc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
# takes a string encoded RDictCcEntry (DBM-value) and formats them
# userfriendly.
class RDictCcEntry

@@output_format = :normal

def self.set_output_format( format )
@@output_format = format
end

def initialize
@value_hash = {}
end
Expand Down Expand Up @@ -80,13 +87,26 @@ def to_s
##
# Given a string-encoded (with to_s()) RDictCcEntry formats it in a readable,
# userfriendly way.
def RDictCcEntry.format_str( str )
def RDictCcEntry.format_str( str)
parts = str.strip!.split(/#<>#/)
s = ""
parts.each do |part|
subparts = part.split(/=<>/)
s << subparts[0] + ":\n" # That's the description
subparts[1].split(/:<>:/).each { |trans| s << " - " + trans + "\n" }
if @@output_format == :compact
s << "- " + subparts[0] + ": "
else
s << subparts[0] + ":\n"
end
subparts[1].split(/:<>:/).each do |trans|
if @@output_format == :compact
s << trans + " / "
else
s << " - " + trans + "\n"
end
end
if @@output_format == :compact
s << "\n"
end
end
s
end
Expand Down Expand Up @@ -209,6 +229,7 @@ def extract_word( phrase )
end

class RDictCcQueryEvaluator

def initialize
if !File.exists? DICT_DIR
puts "There's no ~/.rdictcc directory! You have to import an dict.cc\n" +
Expand All @@ -225,11 +246,9 @@ def initialize
def read_db
for file in [DICT_FILE_DE, DICT_FILE_EN] do
if file == DICT_FILE_DE
puts "DE-EN Translations:"
puts "==================="
puts "{DE-EN}"
else
puts "\nEN-DE Translations:"
puts "==================="
puts "\n{EN-DE}"
end

DBM.open(file, nil, DBM::READER) do |dbm|
Expand Down Expand Up @@ -347,7 +366,7 @@ def interactive_mode
opts.separator "Misc options:"
opts.on("-v", "--version", "Show rdictcc.rb's version") do
# TODO: Set version after changes!
puts "<2007-08-08 Wed 18:50>"
puts "<2008-03-05 Wed 09:29>"
exit 0
end

Expand All @@ -361,6 +380,13 @@ def interactive_mode
exit 0
end

opts.separator ""
opts.separator "Format options:"
opts.on("-c", "--compact", "Use compact output format") do
RDictCcEntry.set_output_format :compact
end


opts.separator ""
opts.separator "Query option:"
opts.on("-s", "--simple", "Translate the word given as QUERY (default)") do
Expand All @@ -382,4 +408,4 @@ def interactive_mode
end

evaluator = RDictCcQueryEvaluator.new
evaluator.query $query_str.concat(ARGV.join(" "))
evaluator.query($query_str.concat(ARGV.join(" ")))

0 comments on commit 1b91a20

Please sign in to comment.