Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ceritium committed Mar 24, 2024
1 parent a3ffa1f commit f017a76
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
11 changes: 5 additions & 6 deletions lib/flatito/flatten_yaml.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
14 changes: 4 additions & 10 deletions lib/flatito/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -17,6 +16,9 @@ def self.build(options)
end

class Base
include Utils
include RegexFromSearch

attr_reader :search, :no_color

def initialize(options)
Expand Down Expand Up @@ -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?

Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions lib/flatito/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Flatito
module Utils
def truncate(string, max = 50)
string.length > max ? "#{string[0...max]}..." : string
end
end
end

0 comments on commit f017a76

Please sign in to comment.