null+****@clear*****
null+****@clear*****
Sat Jun 2 17:35:46 JST 2012
SHIMADA Koji 2012-06-02 17:35:46 +0900 (Sat, 02 Jun 2012) New Revision: 7d389e2e5cf8b9bb8cfccdbcd6ba418d7850dc17 Log: Extract term renderers Added files: lib/logaling/command/renderers.rb Modified files: lib/logaling/command.rb lib/logaling/command/application.rb Modified: lib/logaling/command.rb (+1 -0) =================================================================== --- lib/logaling/command.rb 2012-06-02 11:05:50 +0900 (fbeecbc) +++ lib/logaling/command.rb 2012-06-02 17:35:46 +0900 (e549f71) @@ -18,3 +18,4 @@ require "logaling/command/version" require "logaling/command/application" require "logaling/command/pager" +require "logaling/command/renderers" Modified: lib/logaling/command/application.rb (+14 -59) =================================================================== --- lib/logaling/command/application.rb 2012-06-02 11:05:50 +0900 (9ea0d56) +++ lib/logaling/command/application.rb 2012-06-02 17:35:46 +0900 (97457cc) @@ -237,12 +237,20 @@ module Logaling::Command max_str_size = terms.map{|term| term[:source_term].size}.sort.last run_pager terms.each_with_index do |term, i| - source_string = extract_keyword_and_coloring(term[:snipped_source_term], term[:source_term]) - target_string = extract_keyword_and_coloring(term[:snipped_target_term], term[:target_term]) - note_string = build_note_string(term[:note]) - glossary_name_string = build_glossary_name_string(term[:glossary_name], @repository, @config) - printer(source_string, target_string, note_string, - glossary_name_string, max_str_size, i, terms.length) + case options["output"] + when "terminal" + term_renderer = Logaling::Command::Renderers::TermRenderer.new(term, @repository, @config, options) + term_renderer.max_str_size = max_str_size + term_renderer.render + when "csv" + term_renderer = Logaling::Command::Renderers::TermCsvRenderer.new(term, @repository, @config, options) + term_renderer.render + when "json" + term_renderer = Logaling::Command::Renderers::TermJsonRenderer.new(term, @repository, @config, options) + term_renderer.index = i + term_renderer.last_index = terms.length + term_renderer.render + end end else "source-term <#{source_term}> not found" @@ -343,65 +351,12 @@ module Logaling::Command Pager.run unless options["no-pager"] end - def decorate_glossary_name(glossary_name) - glossary_name.foreground(:white).background(:green) - end - - def build_glossary_name_string(glossary_name, repository, config) - glossary_name_string = "" - if repository.glossary_counts > 1 - if glossary_name == config.glossary - glossary_name_string = decorate_glossary_name(glossary_name) - end - end - glossary_name_string - end - - def build_note_string(note) - note.to_s unless note.empty? - end - - def extract_keyword_and_coloring(snipped_term, term) - return term if snipped_term.empty? || options["no-color"] - display_string = snipped_term.map do |word| - word.is_a?(Hash) ? word[:keyword].bright : word - end - display_string.join - end - def check_logaling_home_exists unless File.exist?(@logaling_home) raise Logaling::CommandFailed, "Input existing directory as logaling-home." end end - def printer(source_string, target_string, note=nil, - glossary_name, max_str_size, i, last) - case options["output"] - when "terminal" - unless note - format = target_string + "\t" + glossary_name - else - format = target_string + "\t# " + note + "\t" + glossary_name - end - printf(" %-#{max_str_size+10}s %s\n", source_string, format) - when "csv" - items = [source_string, target_string, note, - @config.source_language, @config.target_language] - print(CSV.generate {|csv| csv << items}) - when "json" - puts("[") if i == 0 - puts(",") if i > 0 - record = { - :source => source_string, :target => target_string, :note => note, - :source_language => @config.source_language, - :target_language => @config.target_language - } - print JSON.pretty_generate(record) - puts("\n]") if i == last-1 - end - end - def check_import_parameter(glossary_info) unless glossary_info[:name] && glossary_info[:url] raise Logaling::CommandFailed, "Do 'loga import tmx <glossary name> <url or path>'" Added: lib/logaling/command/renderers.rb (+87 -0) 100644 =================================================================== --- /dev/null +++ lib/logaling/command/renderers.rb 2012-06-02 17:35:46 +0900 (0c6cac5) @@ -0,0 +1,87 @@ +module Logaling::Command + module Renderers + class TermRenderer + attr_accessor :max_str_size + + def initialize(term, repository, config, options) + @term = term + @repository = repository + @config = config + @options = options + @max_str_size = 0 + end + + def render + unless note + format = target_term + "\t" + glossary_name + else + format = target_term + "\t# " + note + "\t" + glossary_name + end + printf(" %-#{@max_str_size+10}s %s\n", source_term, format) + end + + def glossary_name + if****@repos*****_counts > 1 + if @term[:glossary_name] ==****@confi***** + @term[:glossary_name].foreground(:white).background(:green) + else + @term[:glossary_name] + end + else + "" + end + end + + def note + @term[:note].to_s unless @term[:note].empty? + end + + def source_term + extract_keyword_and_coloring(@term[:snipped_source_term], @term[:source_term]) + end + + def target_term + extract_keyword_and_coloring(@term[:snipped_target_term], @term[:target_term]) + end + + private + def extract_keyword_and_coloring(snipped_term, term) + return term if snipped_term.empty? || @options["no-color"] + display_string = snipped_term.map do |word| + word.is_a?(Hash) ? word[:keyword].bright : word + end + display_string.join + end + end + + class TermCsvRenderer < TermRenderer + def render + items = [source_term, target_term, note, + @config.source_language, @config.target_language] + print(CSV.generate {|csv| csv << items}) + end + end + + class TermJsonRenderer < TermRenderer + attr_accessor :index, :last_index + + def initialize(term, repository, config, options) + super + @index = 0 + @last_index = 0 + end + + def render + puts("[") if @index == 0 + puts(",") if @index > 0 + record = { + :source => source_term, :target => target_term, :note => note, + :source_language => @config.source_language, + :target_language => @config.target_language + } + print JSON.pretty_generate(record) + puts("\n]") if @index == @last_index-1 + end + end + end +end