Skip to content

Commit

Permalink
Refactor...
Browse files Browse the repository at this point in the history
  • Loading branch information
ceritium committed Mar 19, 2024
1 parent 556d243 commit 9c647b4
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 32 deletions.
2 changes: 2 additions & 0 deletions exe/flatito
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ OptionParser.new do |opts|
end
end.parse!

Flatito::Config.parse_global_options(options)

if stdin
Flatito.flat_content(stdin, options)
else
Expand Down
20 changes: 11 additions & 9 deletions lib/flatito.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
require_relative "flatito/renderer"
require_relative "flatito/regex_from_search"
require_relative "flatito/print_items"
require_relative "flatito/config"

module Flatito
def self.search(paths, options)
Finder.new(paths, options).call
rescue Interrupt
warn "\nInterrupted"
end
class << self
def search(paths, options = {})
Finder.new(paths, options).call
rescue Interrupt
warn "\nInterrupted"
end

def self.flat_content(content, options)
items = FlattenYaml.items_from_content(content)
renderer = Renderer.build(options)
PrintItems.call(renderer, options[:search], items)
def flat_content(content, options = {})
items = FlattenYaml.items_from_content(content)
PrintItems.call(options[:search], items)
end
end
end
17 changes: 17 additions & 0 deletions lib/flatito/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Flatito
module Config
@stdout = $stdout
@stderr = $stderr
@stdin = $stdin

class << self
attr_accessor :renderer, :stdout, :stder, :stdin

def parse_global_options(options)
self.renderer = Renderer.build(options)
end
end
end
end
9 changes: 6 additions & 3 deletions lib/flatito/finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ class Finder

DEFAULT_EXTENSIONS = %w[json yml yaml].freeze

attr_reader :paths, :search, :extensions, :options, :renderer
attr_reader :paths, :search, :extensions, :options

def initialize(paths, options = {})
@paths = paths
@search = options[:search]
@extensions = prepare_extensions(options[:extensions] || DEFAULT_EXTENSIONS)
@options = options
@renderer = Renderer.build(options)
end

def call
Expand All @@ -36,9 +35,13 @@ def call

private

def renderer
Config.renderer
end

def flat_and_filter(pathname)
items = FlattenYaml.items_from_path(pathname)
PrintItems.call(renderer, search, items, pathname)
PrintItems.call(search, items, pathname)
end

def prepare_extensions(extensions)
Expand Down
6 changes: 5 additions & 1 deletion lib/flatito/print_items.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
module Flatito
class PrintItems
def self.call(renderer, search, items, pathname = nil)
def self.call(search, items, pathname = nil)
items = filter_by_search(items) if search
return unless items.any?

renderer.print_pathname(pathname) if pathname
renderer.print_items(items)
end

def self.renderer
Config.renderer
end
end
end
30 changes: 11 additions & 19 deletions lib/flatito/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ class Renderer
include RegexFromSearch

def self.build(options)
if tty?
if Config.stdout.tty?
Renderer::TTY.new(options)
else
Renderer::Plain.new(options)
end
end

def self.tty?
$stdout.tty?
end
end

class Base
Expand All @@ -33,7 +29,7 @@ def print_file_progress(pathname); end
def ending; end

def print_pathname(pathname)
puts colorize(pathname.to_s, :light_blue)
stdout.puts colorize(pathname.to_s, :light_blue)
end

def print_items(items)
Expand All @@ -42,8 +38,8 @@ def print_items(items)
items.each do |item|
print_item(item, line_number_padding)
end
puts
flush
stdout.puts
stdout.flush
end

def print_item(item, line_number_padding)
Expand All @@ -54,15 +50,11 @@ def print_item(item, line_number_padding)
""
end

puts "#{line_number} #{matched_string(item.key)} #{value}"
stdout.puts "#{line_number} #{matched_string(item.key)} #{value}"
end

private

def flush
stdout.flush
end

def regex
@regex ||= Regexp.new(search)
end
Expand All @@ -85,7 +77,7 @@ def truncate(string, max = 50)
end

def stdout
$stdout
Config.stdout
end

def colorize(string, color)
Expand All @@ -95,7 +87,7 @@ def colorize(string, color)

class Renderer::Plain < Base
def ending
puts
stdout.puts
end
end

Expand Down Expand Up @@ -128,19 +120,19 @@ def print_pathname(pathname)
def ending
clear_line
show_cursor
puts
stdout.puts
end

def hide_cursor
print HIDE_CURSOR
stdout.print HIDE_CURSOR
end

def show_cursor
print SHOW_CURSOR
stdout.print SHOW_CURSOR
end

def clear_line
print CLEAR_LINE
stdout.print CLEAR_LINE
end

private
Expand Down
31 changes: 31 additions & 0 deletions test/flatito/config_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require "test_helper"

class Flatito::ConfigTest < Minitest::Test
class DummyTTY < StringIO
def tty?
true
end
end

class DummyNotTTY < StringIO
def tty?
false
end
end

test "when stdout is tty set renderer as tty" do
Flatito::Config.stdout = DummyTTY.new
Flatito::Config.parse_global_options({})

assert_kind_of Flatito::Renderer::TTY, Flatito::Config.renderer
end

test "when stdout is not tty set renderer as plain" do
Flatito::Config.stdout = DummyNotTTY.new
Flatito::Config.parse_global_options({})

assert_kind_of Flatito::Renderer::Plain, Flatito::Config.renderer
end
end

0 comments on commit 9c647b4

Please sign in to comment.