class I18n::Processes::Reports::Terminal
Public Instance Methods
changed_keys(diff = nil)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 28 def changed_keys(diff = nil) if diff print_title "#{diff.count} keys' value changed" print_table headings: [Rainbow('key').cyan.bright, Rainbow('Current').cyan.bright, 'Previous'] do |t| t.rows = diff.map do |key, value| [key, value[:current], value[:previous]] end end else print_success 'No key have been changed.' end end
check_normalized_results(non_normalized)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 108 def check_normalized_results(non_normalized) if non_normalized.empty? print_success 'All data is normalized' return end log_stderr Rainbow('The following data requires normalization:').yellow puts non_normalized log_stderr Rainbow('Run `i18n-processes normalize` to fix').yellow end
eq_base_keys(tree = task.eq_base_keys)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 74 def eq_base_keys(tree = task.eq_base_keys) keys = tree.root_key_value_data(true) if keys.present? print_title eq_base_title(keys) print_locale_key_value_data_table keys else print_info Rainbow('No translations are the same as base value').cyan end end
forest_stats(forest, stats = task.forest_stats(forest))
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 88 def forest_stats(forest, stats = task.forest_stats(forest)) text = if stats[:locale_count] == 1 "has #{stats[:key_count]} keys in total. On average, values are #{stats[:value_chars_avg]} characters long, keys have #{stats[:key_segments_avg]} segments." else "has #{stats[:key_count]} keys across #{stats[:locale_count]} locales. On average, values are #{stats[:value_chars_avg]} characters long, keys have #{stats[:key_segments_avg]} segments, a locale has #{stats[:per_locale_avg]} keys." end title = Rainbow("Forest (#{stats.slice(:locales)})").bright print_info "#{Rainbow(title).cyan} #{Rainbow(text).cyan}" end
icon(type)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 43 def icon(type) glyph = missing_type_info(type)[:glyph] { missing_used: Rainbow(glyph).red, missing_diff: Rainbow(glyph).yellow }[type] end
missing_keys(forest = task.missing_keys)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 10 def missing_keys(forest = task.missing_keys) forest = collapse_missing_tree! forest if forest.present? print_title missing_title(forest) print_table headings: [Rainbow('Locale').cyan.bright, Rainbow('Key').cyan.bright, 'Value in other locales or source'] do |t| t.rows = sort_by_attr!(forest_to_attr(forest)).map do |a| [{ value: Rainbow(format_locale(a[:locale])).cyan, alignment: :center }, format_key(a[:key], a[:data]), missing_key_info(a)] end end else print_success 'No translations are missing.' end end
mv_results(results)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 98 def mv_results(results) results.each do |(from, to)| if to print_info "#{Rainbow(from).cyan} #{Rainbow('⮕').yellow.bright} #{Rainbow(to).cyan}" else print_info "#{Rainbow(from).red}#{Rainbow(' 🗑').red.bright}" end end end
show_tree(tree)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 84 def show_tree(tree) print_locale_key_value_data_table tree.root_key_value_data(true) end
unused_keys(tree = task.unused_keys)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 64 def unused_keys(tree = task.unused_keys) keys = tree.root_key_value_data(true) if keys.present? print_title unused_title(keys) print_locale_key_value_data_table keys else print_success 'Every translation is in use.' end end
used_keys(used_tree = task.used_tree)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 48 def used_keys(used_tree = task.used_tree) # For the used tree we may have usage nodes that are not leaves as references. keys_nodes = used_tree.nodes.select { |node| node.data[:occurrences].present? }.map do |node| [node.full_key(root: false), node] end print_title used_title(keys_nodes, used_tree.first.root.data[:key_filter]) # Group multiple nodes if keys_nodes.present? keys_nodes.sort! { |a, b| a[0] <=> b[0] }.each do |key, node| print_occurrences node, key end else print_error 'No key usages found.' end end
Private Instance Methods
first_occurrence(leaf)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 212 def first_occurrence(leaf) # @type [I18n::Processes::Scanners::KeyOccurrences] occurrences = leaf[:data][:occurrences] # @type [I18n::Processes::Scanners::Occurrence] first = occurrences.first [ Rainbow("#{first.path}:#{first.line_num}").green, ("(#{occurrences.length - 1} more)" if occurrences.length > 1) ].compact.join(' ') end
format_key(key, data)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 129 def format_key(key, data) if data[:ref_info] from, to = data[:ref_info] resolved = key[0...to.length] after = key[to.length..-1] " #{Rainbow(from).yellow}#{Rainbow(after).cyan}\n" \ "#{Rainbow('⮕').yellow.bright} #{Rainbow(resolved).yellow.bright}" else Rainbow(key).cyan end end
format_reference_desc(node_data)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 145 def format_reference_desc(node_data) return nil unless node_data case node_data[:ref_type] when :reference_usage Rainbow('(ref)').yellow.bright when :reference_usage_resolved Rainbow('(resolved ref)').yellow.bright when :reference_usage_key Rainbow('(ref key)').yellow.bright end end
format_value(val)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 141 def format_value(val) val.is_a?(Symbol) ? "#{Rainbow('⮕ ').yellow.bright}#{Rainbow(val).yellow}" : val.to_s.strip end
highlight_key(full_key, line, range = (0..-1))
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 224 def highlight_key(full_key, line, range = (0..-1)) line.dup.tap do |s| s[range] = s[range].sub(full_key) do |m| highlight_string m end end end
indent(txt, n = 2)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 198 def indent(txt, n = 2) txt.gsub(/^/, ' ' * n) end
key_occurrence(full_key, occurrence)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 206 def key_occurrence(full_key, occurrence) location = Rainbow("#{occurrence.path}:#{occurrence.line_num}").green source = highlight_key(occurrence.raw_key || full_key, occurrence.line, occurrence.line_pos..-1).strip "#{location} #{source}" end
missing_key_info(leaf)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 120 def missing_key_info(leaf) if leaf[:type] == :missing_used first_occurrence leaf else "#{Rainbow(leaf[:data][:missing_diff_locale]).cyan} "\ "#{format_value(leaf[:value].is_a?(String) ? leaf[:value].strip : leaf[:value])}" end end
print_error(message)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 190 def print_error(message) log_stderr(Rainbow(message).red.bright) end
print_info(message)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 194 def print_info(message) log_stderr message end
print_locale_key_value_data_table(locale_key_value_datas)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 167 def print_locale_key_value_data_table(locale_key_value_datas) if locale_key_value_datas.present? print_table headings: [Rainbow("Locale").cyan.bright, Rainbow("Key").cyan.bright, "Value"] do |t| t.rows = locale_key_value_datas.map { |(locale, k, v, data)| [{ value: Rainbow(locale).cyan, alignment: :center }, format_key(k, data), format_value(v)] } end else puts 'ø' end end
print_occurrences(node, full_key = node.full_key)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 157 def print_occurrences(node, full_key = node.full_key) occurrences = node.data[:occurrences] puts [Rainbow(full_key).bright, format_reference_desc(node.data), (Rainbow(occurrences.size).green if occurrences.size > 1)].compact.join ' ' occurrences.each do |occurrence| puts " #{key_occurrence full_key, occurrence}" end end
print_success(message)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 186 def print_success(message) log_stderr Rainbow("✓ #{["Good job!", "Well done!", "Perfect!"].sample} #{message}").green.bright end
print_table(opts, &block)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 202 def print_table(opts, &block) puts ::Terminal::Table.new(opts, &block) end
print_title(title)
click to toggle source
# File lib/i18n/processes/reports/terminal.rb, line 181 def print_title(title) log_stderr "#{Rainbow(title.strip).bright} #{I18n::Processes::RainbowUtils.faint_color('|')} " \ "#{"i18n-tasks v#{I18n::Processes::VERSION}"}" end