term_utils 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,43 @@
1
+ # frozen-string-literal: true
2
+ #
3
+ # Copyright (C) 2019 Thomas Baron
4
+ #
5
+ # This file is part of term_utils.
6
+ #
7
+ # term_utils is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, version 3 of the License.
10
+ #
11
+ # term_utils is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with term_utils. If not, see <https://www.gnu.org/licenses/>.
18
+ module TermUtils
19
+ # The ff module provides a way to find files.
20
+ module FF
21
+ # Represents a query configuration.
22
+ class Config
23
+ # @return [Array<Regexp>]
24
+ attr_accessor :ignore_list
25
+ # @return [Integer]
26
+ attr_accessor :min_depth
27
+ # @return [Integer]
28
+ attr_accessor :max_depth
29
+ # @return [Boolean]
30
+ attr_accessor :sorted
31
+ def initialize
32
+ @ignore_list = []
33
+ @min_depth = nil
34
+ @max_depth = nil
35
+ @sorted = false
36
+ end
37
+ def initialize_copy(other)
38
+ @ignore_list = other.ignore_list.dup
39
+ super
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,153 @@
1
+ # frozen-string-literal: true
2
+ #
3
+ # Copyright (C) 2019 Thomas Baron
4
+ #
5
+ # This file is part of term_utils.
6
+ #
7
+ # term_utils is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, version 3 of the License.
10
+ #
11
+ # term_utils is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with term_utils. If not, see <https://www.gnu.org/licenses/>.
18
+ module TermUtils
19
+ # The ff module provides a way to find files.
20
+ module FF
21
+ # Represents a query cursor.
22
+ class Cursor
23
+ # Represents a directory context.
24
+ Context = Struct.new(:path, :components, :entries)
25
+ # @return [Integer]
26
+ attr_reader :index
27
+ # @param config [TermUtils::FF::Config]
28
+ # @param path [String]
29
+ def initialize(config, path)
30
+ @config = config
31
+ @path = path
32
+ @contexts = []
33
+ push_context(path)
34
+ @index = 0
35
+ end
36
+ # Starts the query.
37
+ # @return [TermUtils::FF::Cursor, nil]
38
+ def bootstrap
39
+ res = self
40
+ if @config.min_depth
41
+ while depth < @config.min_depth
42
+ res = following0
43
+ break unless res
44
+ end
45
+ end
46
+ res
47
+ end
48
+ # Returns the next cursor.
49
+ # @return [TermUtils::FF::Cursor, nil]
50
+ def following
51
+ res = nil
52
+ loop do
53
+ res = following0
54
+ break unless res
55
+ if @config.min_depth
56
+ next if depth < @config.min_depth
57
+ end
58
+ if @config.max_depth
59
+ next if depth > @config.max_depth
60
+ end
61
+ @index += 1
62
+ break
63
+ end
64
+ res
65
+ end
66
+ # Returns the result associated to this one.
67
+ # @return [String]
68
+ def result
69
+ @contexts.last.path
70
+ end
71
+ # @return [Integer]
72
+ def depth
73
+ @contexts.last.components.length
74
+ end
75
+ # @return [String]
76
+ def relative_path
77
+ @contexts.last.components.join("/")
78
+ end
79
+ # @return [String]
80
+ def name
81
+ @contexts.last.components[-1]
82
+ end
83
+ # Returns the path components.
84
+ # @return [Array<String>]
85
+ def components
86
+ @contexts.last.components.dup
87
+ end
88
+ private
89
+ # Returns the next cursor.
90
+ # @return [TermUtils::FF::Cursor, nil]
91
+ def following0
92
+ ctx = @contexts.last
93
+ if ctx
94
+ if ctx.entries
95
+ # Directory.
96
+ until ctx.entries.empty?
97
+ name = ctx.entries.first
98
+ break if accept_name(name)
99
+ ctx.entries.shift
100
+ end
101
+ if ctx.entries.empty?
102
+ # Walked through every entry of directory.
103
+ @contexts.pop
104
+ following0
105
+ else
106
+ # Directory entry.
107
+ new_path = StringIO.new
108
+ new_path << ctx.path
109
+ new_path << "/" if ctx.path[-1] != "/"
110
+ new_path << ctx.entries.first
111
+ new_components = ctx.components.dup
112
+ new_components << ctx.entries.first
113
+ push_context(new_path.string, new_components)
114
+ ctx.entries.shift
115
+ self
116
+ end
117
+ else
118
+ # Not directory.
119
+ @contexts.pop
120
+ following0
121
+ end
122
+ end
123
+ end
124
+ # Pushes a new context on the stask.
125
+ # @param path [String]
126
+ def push_context(path, components = [])
127
+ entries = nil
128
+ if File.directory? path
129
+ entries = Dir.entries(path)
130
+ entries.sort! if @config.sorted
131
+ end
132
+ @contexts.push(Context.new(path, components, entries))
133
+ end
134
+ # Tests whether a given name shall be accepted.
135
+ # @param name [String]
136
+ # @return [Boolean]
137
+ def accept_name(name)
138
+ if (name != ".") and (name != "..")
139
+ ret = true
140
+ @config.ignore_list.each do |i|
141
+ if i.match? name
142
+ ret = false
143
+ break
144
+ end
145
+ end
146
+ ret
147
+ else
148
+ false
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,66 @@
1
+ # frozen-string-literal: true
2
+ #
3
+ # Copyright (C) 2019 Thomas Baron
4
+ #
5
+ # This file is part of term_utils.
6
+ #
7
+ # term_utils is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, version 3 of the License.
10
+ #
11
+ # term_utils is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with term_utils. If not, see <https://www.gnu.org/licenses/>.
18
+ module TermUtils
19
+ # The ff module provides a way to find files.
20
+ module FF
21
+ # Represents a file system query.
22
+ class Query
23
+ def initialize
24
+ @config = TermUtils::FF::Config.new
25
+ end
26
+ def initialize_copy(other)
27
+ @config = other.config.dup
28
+ super
29
+ end
30
+ # Adds a Regexp to ignore.
31
+ # @param regexp [Regexp]
32
+ # @return [TermUtils::FF::Query]
33
+ def ignore(regexp)
34
+ @config.ignore_list << regexp
35
+ self
36
+ end
37
+ # Sets a minimum depth.
38
+ # @param depth [Integer]
39
+ # @return [TermUtils::FF::Query]
40
+ def min_depth(depth)
41
+ @config.min_depth = depth
42
+ self
43
+ end
44
+ # Sets a maximum depth.
45
+ # @param depth [Integer]
46
+ # @return [TermUtils::FF::Query]
47
+ def max_depth(depth)
48
+ @config.max_depth = depth
49
+ self
50
+ end
51
+ # Sets whether results shall be sorted.
52
+ # @param sorted [Boolean]
53
+ # @return [TermUtils::FF::Query]
54
+ def sort(sorted = true)
55
+ @config.sorted = sorted
56
+ self
57
+ end
58
+ # Executes this one.
59
+ # @param path [String]
60
+ # @return [TermUtils::FF::Cursor]
61
+ def exec(path)
62
+ TermUtils::FF::Cursor.new(@config.dup, path).bootstrap
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,18 @@
1
+ # Copyright (C) 2019 Thomas Baron
2
+ #
3
+ # This file is part of term_utils.
4
+ #
5
+ # term_utils is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, version 3 of the License.
8
+ #
9
+ # term_utils is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with term_utils. If not, see <https://www.gnu.org/licenses/>.
16
+ require 'term_utils/ff/config'
17
+ require 'term_utils/ff/cursor'
18
+ require 'term_utils/ff/query'
@@ -1,3 +1,5 @@
1
+ # Copyright (C) 2019 Thomas Baron
2
+ #
1
3
  # This file is part of term_utils.
2
4
  #
3
5
  # term_utils is free software: you can redistribute it and/or modify
@@ -12,6 +14,7 @@
12
14
  # You should have received a copy of the GNU General Public License
13
15
  # along with term_utils. If not, see <https://www.gnu.org/licenses/>.
14
16
  module TermUtils
17
+ # The tab module provides a way to print formatted tables.
15
18
  module Tab
16
19
  # Represents a table.
17
20
  class Table
@@ -53,7 +56,6 @@ module TermUtils
53
56
  end
54
57
  # Creates a new table printer.
55
58
  # @param io [IO]
56
- # @param values [Array<Object>, Hash<Symbol, Object>]
57
59
  # @param opts [Hash]
58
60
  # @option opts [Integer] :offset
59
61
  # @option opts [Integer] :column_separator_width
@@ -250,12 +252,25 @@ module TermUtils
250
252
  @io = io
251
253
  @options = options
252
254
  end
255
+ # Prints an empty line.
253
256
  def line
254
257
  @io.puts ""
255
258
  end
259
+ # Prints a header row.
260
+ # @param values [Array<Object>, Hash<Symbol, Object>]
261
+ # @param opts [Hash]
262
+ # @option opts [Integer] :offset
263
+ # @option opts [Integer] :column_separator_width
264
+ # @return [nil]
256
265
  def header(values = nil, opts = {})
257
266
  @table.print_header(@io, values, @options.merge(opts))
258
267
  end
268
+ # Prints a data row.
269
+ # @param values [Array<Object>, Hash<Symbol, Object>]
270
+ # @param opts [Hash]
271
+ # @option opts [Integer] :offset
272
+ # @option opts [Integer] :column_separator_width
273
+ # @return [nil]
259
274
  def data(values, opts = {})
260
275
  @table.print_data(@io, values, @options.merge(opts))
261
276
  end
@@ -299,7 +314,6 @@ module TermUtils
299
314
  # Creates a new table printer.
300
315
  # @param id [Symbol]
301
316
  # @param io [IO]
302
- # @param values [Array<Object>, Hash<Symbol, Object>]
303
317
  # @param opts [Hash]
304
318
  # @option opts [Integer] :offset
305
319
  # @option opts [Integer] :column_separator_width
@@ -316,10 +330,15 @@ module TermUtils
316
330
  def self.define_table(id, opts = {}, &block)
317
331
  @@default_holder.define_table(id, opts = {}, &block)
318
332
  end
333
+ # Finds a table.
334
+ # @param id [Symbol]
335
+ # @return [Tab::Table, nil]
336
+ def self.find_table(id)
337
+ @@default_holder.find_table(id)
338
+ end
319
339
  # Creates a new table printer.
320
340
  # @param id [Symbol]
321
341
  # @param io [IO]
322
- # @param values [Array<Object>, Hash<Symbol, Object>]
323
342
  # @param opts [Hash]
324
343
  # @option opts [Integer] :offset
325
344
  # @option opts [Integer] :column_separator_width
data/lib/term_utils.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # Copyright (C) 2019 Thomas Baron
2
+ #
1
3
  # This file is part of term_utils.
2
4
  #
3
5
  # term_utils is free software: you can redistribute it and/or modify
@@ -11,4 +13,5 @@
11
13
  #
12
14
  # You should have received a copy of the GNU General Public License
13
15
  # along with term_utils. If not, see <https://www.gnu.org/licenses/>.
16
+ require 'term_utils/ff'
14
17
  require 'term_utils/tab'
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "term_utils"
3
+ s.version = "0.2.0"
4
+ s.date = "2019-10-17"
5
+ s.summary = "Terminal utilities."
6
+ s.description = <<-EOS
7
+ Provides terminal utilities like table formatting.
8
+ EOS
9
+ s.authors = ["Thomas Baron"]
10
+ s.email = "tvbaron at gmail dot com"
11
+ s.files = Dir["lib/**/*.rb", "doc/**/*"] + ["AUTHORS", "CHANGELOG.md", "COPYING", "Rakefile", "README.md", "term_utils.gemspec"]
12
+ s.homepage = "https://git.yellowcube.net/tvb/term_utils"
13
+ s.license = "GPL-3.0-only"
14
+ s.required_ruby_version = '>= 2.2.0'
15
+ s.metadata["yard.run"] = "yri"
16
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: term_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Baron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-16 00:00:00.000000000 Z
11
+ date: 2019-10-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'Provides terminal utilities like table formatting.
14
14
 
@@ -18,12 +18,48 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - AUTHORS
22
+ - CHANGELOG.md
23
+ - COPYING
24
+ - README.md
25
+ - Rakefile
26
+ - doc/TermUtils.html
27
+ - doc/TermUtils/FF.html
28
+ - doc/TermUtils/FF/Config.html
29
+ - doc/TermUtils/FF/Cursor.html
30
+ - doc/TermUtils/FF/Cursor/Context.html
31
+ - doc/TermUtils/FF/Query.html
32
+ - doc/TermUtils/Tab.html
33
+ - doc/TermUtils/Tab/Column.html
34
+ - doc/TermUtils/Tab/Holder.html
35
+ - doc/TermUtils/Tab/Printer.html
36
+ - doc/TermUtils/Tab/Table.html
37
+ - doc/_index.html
38
+ - doc/class_list.html
39
+ - doc/css/common.css
40
+ - doc/css/full_list.css
41
+ - doc/css/style.css
42
+ - doc/file.README.html
43
+ - doc/file_list.html
44
+ - doc/frames.html
45
+ - doc/index.html
46
+ - doc/js/app.js
47
+ - doc/js/full_list.js
48
+ - doc/js/jquery.js
49
+ - doc/method_list.html
50
+ - doc/top-level-namespace.html
21
51
  - lib/term_utils.rb
52
+ - lib/term_utils/ff.rb
53
+ - lib/term_utils/ff/config.rb
54
+ - lib/term_utils/ff/cursor.rb
55
+ - lib/term_utils/ff/query.rb
22
56
  - lib/term_utils/tab.rb
57
+ - term_utils.gemspec
23
58
  homepage: https://git.yellowcube.net/tvb/term_utils
24
59
  licenses:
25
60
  - GPL-3.0-only
26
- metadata: {}
61
+ metadata:
62
+ yard.run: yri
27
63
  post_install_message:
28
64
  rdoc_options: []
29
65
  require_paths:
@@ -32,7 +68,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
32
68
  requirements:
33
69
  - - ">="
34
70
  - !ruby/object:Gem::Version
35
- version: '0'
71
+ version: 2.2.0
36
72
  required_rubygems_version: !ruby/object:Gem::Requirement
37
73
  requirements:
38
74
  - - ">="