flatito 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 420f7b09c4eb71cded9e1c3765b1fab02d0385bcb7e51cebed2d6f8945e8b182
4
- data.tar.gz: 3bddb748de4716a151ccc60590f52c59d55b2506f744d217bb5bd19f05818dc7
3
+ metadata.gz: aaba984e7f2dd508d7f81eba275c575e3b256e394fbe513ea4620e1ec3631c3b
4
+ data.tar.gz: 6614248f57ed83e1e96cc633eca53a0ce5bddb7fe2248173ed0fad2daa64472d
5
5
  SHA512:
6
- metadata.gz: dde72b5db32e1d4da64f60127ce0fe0fb0072bba6db06e8a5888df3aec4839eb5ae46954f7dc5b31f819d4752c98937957d4d1082eee2b552dfff6647c9940dd
7
- data.tar.gz: 690ccabd218f757b9282d5e02c9c0f76f98ae8e5b14dde543b148888bf7b8d9489e848659dba4c888c04c7661d38c13abe1bde0a8f87dfcac5743dab62e2c1c4
6
+ metadata.gz: 9ab83eef1a77f864dff343f33c7570357dae4562be62707f375371e5fb48172357d293420eebeee123927f99ac1ebf54014293824768c2cb4401cbbd9612a864
7
+ data.tar.gz: b3013f55f89da75acc3cbc387f97522dc0ebb8082ef40c0d044f395f901207d84fb767fd1ff57dd545c41e246bb8b745f20d996cddf26586ff5efe2122a0e6b6
data/README.md CHANGED
@@ -22,7 +22,8 @@ If bundler is not being used to manage dependencies, install the gem by executin
22
22
 
23
23
  ```sh
24
24
  Usage: flatito PATH [options]
25
- Example: flatito . -k hello
25
+ Example: flatito . -k "search string" -e "json,yaml"
26
+ Example: cat file.yaml | flatito -k "search string"
26
27
 
27
28
  -h, --help Prints this help
28
29
  -V, --version Show version
data/exe/flatito CHANGED
@@ -6,14 +6,15 @@ require "flatito"
6
6
  require "optparse"
7
7
 
8
8
  # If no arguments are given, print help
9
- ARGV << "-h" if ARGV.empty?
9
+ stdin = $stdin.read unless $stdin.tty?
10
+ ARGV << "-h" if ARGV.empty? && !stdin
10
11
 
11
12
  options = {}
12
13
  OptionParser.new do |opts|
13
14
  opts.banner = <<~HEREDOC
14
15
  Usage: flatito PATH [options]
15
- Example: flatito . -k hello
16
-
16
+ Example: flatito . -k "search string" -e "json,yaml"
17
+ Example: cat file.yaml | flatito -k "search string"
17
18
  HEREDOC
18
19
 
19
20
  opts.on("-h", "--help", "Prints this help") do
@@ -43,8 +44,10 @@ OptionParser.new do |opts|
43
44
  end
44
45
  end.parse!
45
46
 
46
- begin
47
+ Flatito::Config.prepare_with_options(options)
48
+
49
+ if stdin
50
+ Flatito.flat_content(stdin, options)
51
+ else
47
52
  Flatito.search(ARGV, options)
48
- rescue Interrupt
49
- warn "\nInterrupted"
50
53
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flatito
4
+ module Config
5
+ @stdout = $stdout
6
+ @stderr = $stderr
7
+ @stdin = $stdin
8
+
9
+ class << self
10
+ attr_accessor :renderer, :stdout, :stder, :stdin
11
+
12
+ def prepare_with_options(options)
13
+ self.renderer = Renderer.build(options)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -8,14 +8,14 @@ module Flatito
8
8
 
9
9
  DEFAULT_EXTENSIONS = %w[json yml yaml].freeze
10
10
 
11
- attr_reader :paths, :search, :extensions, :options, :renderer
11
+ attr_reader :paths, :search, :extensions, :options, :print_items
12
12
 
13
13
  def initialize(paths, options = {})
14
14
  @paths = paths
15
15
  @search = options[:search]
16
16
  @extensions = prepare_extensions(options[:extensions] || DEFAULT_EXTENSIONS)
17
17
  @options = options
18
- @renderer = Renderer.build(options)
18
+ @print_items = PrintItems.new(search)
19
19
  end
20
20
 
21
21
  def call
@@ -36,14 +36,13 @@ module Flatito
36
36
 
37
37
  private
38
38
 
39
- def flat_and_filter(pathname)
40
- items = FlattenYaml.new(pathname).items
41
- items = filter_by_search(items) if search
42
-
43
- return unless items.any?
39
+ def renderer
40
+ Config.renderer
41
+ end
44
42
 
45
- renderer.print_pathname(pathname)
46
- renderer.print_items(items)
43
+ def flat_and_filter(pathname)
44
+ items = FlattenYaml.items_from_path(pathname)
45
+ print_items.print(items, pathname)
47
46
  end
48
47
 
49
48
  def prepare_extensions(extensions)
@@ -51,11 +50,5 @@ module Flatito
51
50
  ext.start_with?(".") ? ext : ".#{ext}"
52
51
  end
53
52
  end
54
-
55
- def filter_by_search(items)
56
- items.select do |item|
57
- regex.match?(item.key)
58
- end
59
- end
60
53
  end
61
54
  end
@@ -1,9 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "utils"
3
4
  module Flatito
4
5
  class FlattenYaml
6
+ include Utils
7
+
5
8
  Item = Struct.new(:key, :value, :line, keyword_init: true)
6
- def initialize(pathname)
9
+ class << self
10
+ def items_from_path(pathname)
11
+ content = File.read(pathname)
12
+ new(content, pathname: pathname).items
13
+ end
14
+
15
+ def items_from_content(content)
16
+ new(content).items
17
+ end
18
+ end
19
+
20
+ attr_reader :content, :pathname
21
+
22
+ def initialize(content, pathname: nil)
23
+ @content = content
7
24
  @pathname = pathname
8
25
  end
9
26
 
@@ -23,28 +40,20 @@ module Flatito
23
40
  end
24
41
  end
25
42
  end
26
- rescue StandardError => e
27
- warn "Error parsing #{@pathname}, #{e.message}"
28
43
  end
29
44
 
30
45
  def with_line_numbers
31
46
  handler = YAMLWithLineNumber::TreeBuilder.new
32
47
  handler.parser = Psych::Parser.new(handler)
33
48
 
34
- handler.parser.parse(File.read(@pathname))
49
+ handler.parser.parse(content)
35
50
  YAMLWithLineNumber::VisitorsToRuby.create.accept(handler.root)
36
51
  rescue Psych::SyntaxError
37
- warn "Invalid YAML #{@pathname}"
38
-
52
+ warn "Invalid format #{pathname}"
39
53
  []
40
- rescue StandardError => e
41
- warn "Error parsing #{@pathname}, #{e.message}"
42
-
54
+ rescue StandardError
55
+ warn "Error parsing #{pathname}"
43
56
  []
44
57
  end
45
-
46
- def truncate(string, max = 50)
47
- string.length > max ? "#{string[0...max]}..." : string
48
- end
49
58
  end
50
59
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flatito
4
+ class PrintItems
5
+ include RegexFromSearch
6
+ attr_reader :search
7
+
8
+ def initialize(search)
9
+ @search = search
10
+ end
11
+
12
+ def print(items, pathname = nil)
13
+ items = filter_by_search(items) if search
14
+ return unless items.any?
15
+
16
+ renderer.print_pathname(pathname) if pathname
17
+ renderer.print_items(items)
18
+ end
19
+
20
+ def filter_by_search(items)
21
+ items.select do |item|
22
+ regex.match?(item.key)
23
+ end
24
+ end
25
+
26
+ def renderer
27
+ Config.renderer
28
+ end
29
+ end
30
+ end
@@ -2,25 +2,23 @@
2
2
 
3
3
  require "io/console"
4
4
  require_relative "regex_from_search"
5
+ require_relative "utils"
5
6
 
6
7
  module Flatito
7
8
  class Renderer
8
- include RegexFromSearch
9
-
10
9
  def self.build(options)
11
- if tty?
10
+ if Config.stdout.tty?
12
11
  Renderer::TTY.new(options)
13
12
  else
14
13
  Renderer::Plain.new(options)
15
14
  end
16
15
  end
17
-
18
- def self.tty?
19
- $stdout.tty?
20
- end
21
16
  end
22
17
 
23
18
  class Base
19
+ include Utils
20
+ include RegexFromSearch
21
+
24
22
  attr_reader :search, :no_color
25
23
 
26
24
  def initialize(options)
@@ -33,7 +31,7 @@ module Flatito
33
31
  def ending; end
34
32
 
35
33
  def print_pathname(pathname)
36
- puts colorize(pathname.to_s, :light_blue)
34
+ stdout.puts colorize(pathname.to_s, :light_blue)
37
35
  end
38
36
 
39
37
  def print_items(items)
@@ -42,8 +40,8 @@ module Flatito
42
40
  items.each do |item|
43
41
  print_item(item, line_number_padding)
44
42
  end
45
- puts
46
- flush
43
+ stdout.puts
44
+ stdout.flush
47
45
  end
48
46
 
49
47
  def print_item(item, line_number_padding)
@@ -54,19 +52,11 @@ module Flatito
54
52
  ""
55
53
  end
56
54
 
57
- puts "#{line_number} #{matched_string(item.key)} #{value}"
55
+ stdout.puts "#{line_number} #{matched_string(item.key)} #{value}"
58
56
  end
59
57
 
60
58
  private
61
59
 
62
- def flush
63
- stdout.flush
64
- end
65
-
66
- def regex
67
- @regex ||= Regexp.new(search)
68
- end
69
-
70
60
  def matched_string(string)
71
61
  return string if search.nil? || no_color?
72
62
 
@@ -80,12 +70,8 @@ module Flatito
80
70
  ENV["TERM"] == "dumb" || ENV["NO_COLOR"] == "true" || no_color == true
81
71
  end
82
72
 
83
- def truncate(string, max = 50)
84
- string.length > max ? "#{string[0...max]}..." : string
85
- end
86
-
87
73
  def stdout
88
- $stdout
74
+ Config.stdout
89
75
  end
90
76
 
91
77
  def colorize(string, color)
@@ -95,7 +81,7 @@ module Flatito
95
81
 
96
82
  class Renderer::Plain < Base
97
83
  def ending
98
- puts
84
+ stdout.puts
99
85
  end
100
86
  end
101
87
 
@@ -128,19 +114,19 @@ module Flatito
128
114
  def ending
129
115
  clear_line
130
116
  show_cursor
131
- puts
117
+ stdout.puts
132
118
  end
133
119
 
134
120
  def hide_cursor
135
- print HIDE_CURSOR
121
+ stdout.print HIDE_CURSOR
136
122
  end
137
123
 
138
124
  def show_cursor
139
- print SHOW_CURSOR
125
+ stdout.print SHOW_CURSOR
140
126
  end
141
127
 
142
128
  def clear_line
143
- print CLEAR_LINE
129
+ stdout.print CLEAR_LINE
144
130
  end
145
131
 
146
132
  private
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flatito
4
+ module Utils
5
+ def truncate(string, max = 50)
6
+ string.length > max ? "#{string[0...max]}..." : string
7
+ end
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Flatito
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
@@ -30,8 +30,6 @@ module Flatito
30
30
 
31
31
  class ClassLoader < Psych::ClassLoader
32
32
  def find(klassname)
33
- @cache[klassname] ||= resolve(klassname)
34
- rescue ArgumentError
35
33
  Whatever.new(klassname)
36
34
  end
37
35
  end
@@ -56,9 +54,9 @@ module Flatito
56
54
  end
57
55
 
58
56
  class VisitorsToRuby < Psych::Visitors::ToRuby
59
- def self.create(symbolize_names: false, freeze: false, strict_integer: false)
57
+ def self.create(symbolize_names: false, freeze: false)
60
58
  class_loader = ClassLoader.new
61
- scanner = Psych::ScalarScanner.new(class_loader, strict_integer: strict_integer)
59
+ scanner = Psych::ScalarScanner.new(class_loader)
62
60
  new(scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze)
63
61
  end
64
62
 
data/lib/flatito.rb CHANGED
@@ -9,9 +9,20 @@ require_relative "flatito/finder"
9
9
  require_relative "flatito/yaml_with_line_number"
10
10
  require_relative "flatito/renderer"
11
11
  require_relative "flatito/regex_from_search"
12
+ require_relative "flatito/print_items"
13
+ require_relative "flatito/config"
12
14
 
13
15
  module Flatito
14
- def self.search(paths, options)
15
- Finder.new(paths, options).call
16
+ class << self
17
+ def search(paths, options = {})
18
+ Finder.new(paths, options).call
19
+ rescue Interrupt
20
+ warn "\nInterrupted"
21
+ end
22
+
23
+ def flat_content(content, options = {})
24
+ items = FlattenYaml.items_from_content(content)
25
+ PrintItems.new(options[:search]).print(items)
26
+ end
16
27
  end
17
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flatito
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - José Galisteo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-15 00:00:00.000000000 Z
11
+ date: 2024-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -41,11 +41,14 @@ files:
41
41
  - docs/screenshot.png
42
42
  - exe/flatito
43
43
  - lib/flatito.rb
44
+ - lib/flatito/config.rb
44
45
  - lib/flatito/finder.rb
45
46
  - lib/flatito/flatten_yaml.rb
47
+ - lib/flatito/print_items.rb
46
48
  - lib/flatito/regex_from_search.rb
47
49
  - lib/flatito/renderer.rb
48
50
  - lib/flatito/tree_iterator.rb
51
+ - lib/flatito/utils.rb
49
52
  - lib/flatito/version.rb
50
53
  - lib/flatito/yaml_with_line_number.rb
51
54
  homepage: https://github.com/ceritium/flatito
@@ -70,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
73
  - !ruby/object:Gem::Version
71
74
  version: '0'
72
75
  requirements: []
73
- rubygems_version: 3.5.6
76
+ rubygems_version: 3.5.3
74
77
  signing_key:
75
78
  specification_version: 4
76
79
  summary: Grep for YAML and JSON files