treely 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1bec0d1ca8e19d52eeff6dbf056518ad4e19cced1abcd8f9d56ade1c0598064f
4
+ data.tar.gz: bfe3492ea4e59c7e1224fa60e4253944094ff43a8b87a70609844214833d55c0
5
+ SHA512:
6
+ metadata.gz: c70eaf3f2ff1bc59168af0d7a3af2f804bc0dfd16ad041872d2cc6596135225e5e72ba8fe236e5ee8724af7320b0e4f759e10b9762b0c11e33df908359a797a6
7
+ data.tar.gz: 47e31cb8706f82f9fcfcfe23da012cb2e8e3893f6ae3ba1fb9584061eed6544fd0137de6c9a70e8993302d5b437df4b627c6e0279df976195f6c6bc9ba82f715
data/bin/treely ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'json'
4
+ require 'pathname'
5
+
6
+ require 'treely'
7
+ require 'treely/directory'
8
+
9
+ require 'active_support/core_ext/object/blank'
10
+ require 'active_support/core_ext/string/inflections'
11
+ require 'mercenary'
12
+
13
+ Mercenary.program(:treely) do |program|
14
+ program.version Treely::VERSION
15
+ program.syntax 'treely .'
16
+
17
+ program.action do |args, options|
18
+ contents = if args[0].present?
19
+ Treely::Directory.new(args[0]).contents
20
+ else
21
+ JSON.parse(ARGF.read, symbolize_names: true)
22
+ end
23
+
24
+ if contents.any? { |item| item.has_key?(:files) }
25
+ puts Treely.tree(contents[0..-2])
26
+ puts %{}
27
+ puts "%s %s, %s %s" % [
28
+ contents[-1].fetch(:directories),
29
+ %{directories}.pluralize(contents[-1][:directories]),
30
+ contents[-1].fetch(:files),
31
+ %{files}.pluralize(contents[-1][:files])
32
+ ]
33
+ else
34
+ puts Treely.tree(contents)
35
+ puts %{}
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,33 @@
1
+ module Treely
2
+ class Configuration
3
+ attr_reader :style
4
+
5
+ def initialize
6
+ @style = {
7
+ :dot => %{\x2e},
8
+ :space => %{\x20},
9
+ :new_line => %{\x0a},
10
+ :bar => %{│ },
11
+ :indent => %{ },
12
+ :branch => %{├── },
13
+ :last_branch => %{└── }
14
+ }
15
+ end
16
+
17
+ def transform_style!(&block)
18
+ @style.transform_values!(&block)
19
+ end
20
+ end
21
+
22
+ def self.configuration
23
+ @configuration ||= Configuration.new
24
+ end
25
+
26
+ def self.configuration=(config)
27
+ @configuration = config
28
+ end
29
+
30
+ def self.configure
31
+ yield configuration
32
+ end
33
+ end
@@ -0,0 +1,39 @@
1
+ module Treely
2
+ class Directory
3
+ def initialize(path)
4
+ @pathname = Pathname(path)
5
+ @directories, @files = [], []
6
+ @ignore_pattern = /\A\.(\.?|\w+)\z/
7
+
8
+ unless @pathname.directory?
9
+ @contents = []
10
+ end
11
+ end
12
+
13
+ def contents
14
+ @contents ||= digger.call(@pathname)
15
+ [*@contents, { directories: @directories.size, files: @files.size }]
16
+ end
17
+
18
+ private
19
+
20
+ def digger
21
+ lambda do |pathname|
22
+ pathname.children.reject { |n| @ignore_pattern.match?(n.basename.to_s) }.map do |pathname|
23
+ if pathname.directory?
24
+ @directories << pathname
25
+ { name: pathname.basename.to_s, contents: digger.call(pathname) }
26
+ else
27
+ @files << pathname
28
+ { name: pathname.basename.to_s }
29
+ end
30
+ end.sort_by do |item|
31
+ [
32
+ item[:contents].class.to_s,
33
+ item[:name]
34
+ ]
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,31 @@
1
+ module Treely
2
+ class Tree
3
+ def initialize(source)
4
+ @style = Treely[:style]
5
+ @contents = %{}
6
+ treely source
7
+ end
8
+
9
+ def to_s
10
+ @contents
11
+ end
12
+
13
+ private
14
+
15
+ def treely(source, *args)
16
+ source.each.with_index do |item, index|
17
+ @contents << args.map { |name| @style[name] }.join
18
+ @contents << @style[index.zero? ? :dot : :bar]
19
+ @contents << @style[:new_line]
20
+ @contents << args.map { |name| @style[name] }.join
21
+ @contents << @style[source[index + 1] ? :branch : :last_branch]
22
+ @contents << item.fetch(:name)
23
+ @contents << @style[:new_line]
24
+
25
+ if item.has_key?(:contents)
26
+ treely item[:contents], *args, (source[index + 1] ? :bar : :indent)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
data/lib/treely.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'treely/configuration'
2
+ require 'treely/tree'
3
+
4
+ module Treely
5
+ module_function
6
+
7
+ def tree(source)
8
+ Tree.new(source)
9
+ end
10
+
11
+ def [](name)
12
+ configuration.send(name)
13
+ end
14
+
15
+ self.const_set 'version'.upcase, '1.0.5'
16
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: treely
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.5
5
+ platform: ruby
6
+ authors:
7
+ - matark
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mercenary
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.3.6
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.3.6
55
+ description:
56
+ email: matark@naver.com
57
+ executables:
58
+ - treely
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - bin/treely
63
+ - lib/treely.rb
64
+ - lib/treely/configuration.rb
65
+ - lib/treely/directory.rb
66
+ - lib/treely/tree.rb
67
+ homepage: https://opensource.matark.de/treely
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubygems_version: 3.0.3
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: A Ruby library for generating tree-like format
90
+ test_files: []