From c897468d277ab350a645dd4219ce5922975b8156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Galisteo?= Date: Sun, 24 Mar 2024 19:14:45 +0100 Subject: [PATCH] Refactor --- lib/flatito/flatten_yaml.rb | 11 +++++------ lib/flatito/renderer.rb | 14 ++++---------- lib/flatito/utils.rb | 9 +++++++++ 3 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 lib/flatito/utils.rb diff --git a/lib/flatito/flatten_yaml.rb b/lib/flatito/flatten_yaml.rb index 2f31de9..bea8838 100644 --- a/lib/flatito/flatten_yaml.rb +++ b/lib/flatito/flatten_yaml.rb @@ -1,7 +1,10 @@ # frozen_string_literal: true +require_relative "utils" module Flatito class FlattenYaml + include Utils + Item = Struct.new(:key, :value, :line, keyword_init: true) class << self def items_from_path(pathname) @@ -46,15 +49,11 @@ def with_line_numbers handler.parser.parse(content) YAMLWithLineNumber::VisitorsToRuby.create.accept(handler.root) rescue Psych::SyntaxError - warn "Invalid format #{@pathname}" + warn "Invalid format #{pathname}" [] rescue StandardError - warn "Error parsing #{@pathname}" + warn "Error parsing #{pathname}" [] end - - def truncate(string, max = 50) - string.length > max ? "#{string[0...max]}..." : string - end end end diff --git a/lib/flatito/renderer.rb b/lib/flatito/renderer.rb index 8c68140..0949bd0 100644 --- a/lib/flatito/renderer.rb +++ b/lib/flatito/renderer.rb @@ -2,11 +2,10 @@ require "io/console" require_relative "regex_from_search" +require_relative "utils" module Flatito class Renderer - include RegexFromSearch - def self.build(options) if Config.stdout.tty? Renderer::TTY.new(options) @@ -17,6 +16,9 @@ def self.build(options) end class Base + include Utils + include RegexFromSearch + attr_reader :search, :no_color def initialize(options) @@ -55,10 +57,6 @@ def print_item(item, line_number_padding) private - def regex - @regex ||= Regexp.new(search) - end - def matched_string(string) return string if search.nil? || no_color? @@ -72,10 +70,6 @@ def no_color? ENV["TERM"] == "dumb" || ENV["NO_COLOR"] == "true" || no_color == true end - def truncate(string, max = 50) - string.length > max ? "#{string[0...max]}..." : string - end - def stdout Config.stdout end diff --git a/lib/flatito/utils.rb b/lib/flatito/utils.rb new file mode 100644 index 0000000..733023a --- /dev/null +++ b/lib/flatito/utils.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module Flatito + module Utils + def truncate(string, max = 50) + string.length > max ? "#{string[0...max]}..." : string + end + end +end