djan 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +8 -0
  3. data/LICENSE.txt +24 -0
  4. data/README.md +10 -0
  5. data/djan.gemspec +49 -0
  6. data/lib/djan.rb +256 -0
  7. metadata +82 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d64827460567bc9f5efba4e2adf3820e88a15cf684299e36b493e08ed7bac0e8
4
+ data.tar.gz: 9967cec6108f6c5feb68c2c99df058ab9e891d9fc63c2847013aad4121f17350
5
+ SHA512:
6
+ metadata.gz: af593982048900c49c2969fd9873236ca5d431ff51c87aa0db81ecd50047d18178b19fb257f343d7a524108ecd5a645eacb8f997e7e7c74e83fdcbfe9283c1b7
7
+ data.tar.gz: ef639c3aadc66eacfc59485f8a913aa07ce9601cb6cb4327a59c3007e430f60def7ee7095c8c5e5aba4a99b7709112c208180be2b08eba7514c3222a557f635e
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+
2
+ # CHANGELOG.md
3
+
4
+
5
+ ## djan 1.0.0
6
+
7
+ * Initial, standalone, release
8
+
data/LICENSE.txt ADDED
@@ -0,0 +1,24 @@
1
+
2
+ Copyright (c) 2017-2025, John Mettraux, jmettraux+flor@gmail.com
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
21
+
22
+
23
+ Made in Japan
24
+
data/README.md ADDED
@@ -0,0 +1,10 @@
1
+
2
+ # djan
3
+
4
+ <!-- [![tests](https://github.com/floraison/fugit/workflows/test/badge.svg)](https://github.com/floraison/fugit/actions) -->
5
+ [![Gem Version](https://badge.fury.io/rb/djan.svg)](http://badge.fury.io/rb/djan)
6
+
7
+ ## LICENSE
8
+
9
+ MIT, see [LICENSE.txt](LICENSE.txt)
10
+
data/djan.gemspec ADDED
@@ -0,0 +1,49 @@
1
+
2
+ Gem::Specification.new do |s|
3
+
4
+ s.name = 'djan'
5
+
6
+ s.version = File.read(
7
+ File.expand_path('../lib/djan.rb', __FILE__)
8
+ ).match(/ VERSION *= *['"]([^'"]+)/)[1]
9
+
10
+ s.platform = Gem::Platform::RUBY
11
+ s.authors = [ 'John Mettraux' ]
12
+ s.email = [ 'jmettraux+flor@gmail.com' ]
13
+ s.homepage = 'https://github.com/floraison/djan'
14
+ s.license = 'MIT'
15
+ s.summary = 'Pretty printing for flor'
16
+
17
+ s.description = %{
18
+ Pretty pretting for floraison and flor.
19
+ }.strip
20
+
21
+ s.metadata = {
22
+ 'changelog_uri' => s.homepage + '/blob/master/CHANGELOG.md',
23
+ 'bug_tracker_uri' => s.homepage + '/issues',
24
+ 'documentation_uri' => s.homepage,
25
+ 'homepage_uri' => s.homepage,
26
+ 'source_code_uri' => s.homepage,
27
+ #'mailing_list_uri' => 'https://groups.google.com/forum/#!forum/floraison',
28
+ #'wiki_uri' => s.homepage + '/wiki',
29
+ 'rubygems_mfa_required' => 'true',
30
+ }
31
+
32
+ #s.files = `git ls-files`.split("\n")
33
+ s.files = Dir[
34
+ '{README,CHANGELOG,CREDITS,LICENSE}.{md,txt}',
35
+ #'Makefile',
36
+ 'lib/**/*.rb', #'spec/**/*.rb', 'test/**/*.rb',
37
+ "#{s.name}.gemspec",
38
+ ]
39
+
40
+ #s.add_runtime_dependency 'tzinfo'
41
+ # this dependency appears in 'et-orbi'
42
+
43
+ s.add_runtime_dependency 'colorato', '~> 1.0'
44
+
45
+ s.add_development_dependency 'minitest', '~> 5.0'
46
+
47
+ s.require_path = 'lib'
48
+ end
49
+
data/lib/djan.rb ADDED
@@ -0,0 +1,256 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'io/console'
4
+
5
+ require 'colorato'
6
+
7
+
8
+ module Djan
9
+
10
+ VERSION = '1.0.0'
11
+
12
+ extend self
13
+
14
+ def to_djan(x, opts={})
15
+
16
+ out = StringIO.new
17
+ out.set_encoding('UTF-8')
18
+
19
+ opts[:c] =
20
+ opts[:colours] == false ||
21
+ opts[:colors] == false ||
22
+ opts[:colour] == false ||
23
+ opts[:color] == false ? Colorato.no_colours :
24
+ Colorato.colours
25
+
26
+ if [ :console, true ].include?(opts[:width])
27
+ opts[:width] = IO.console.winsize[1] rescue 80
28
+ #elsif opts[:width].is_a?(Integer)
29
+ # let it go
30
+ elsif mw = (opts[:mw] || opts[:maxwidth] || opts[:max_width])
31
+ opts[:width] = [ (IO.console.winsize[1] rescue 80), mw ].min
32
+ end
33
+ opts[:indent] ||= 0 if opts[:width]
34
+
35
+ opts[:str_escape] ||= []
36
+
37
+ to_dj(x, out, opts)
38
+
39
+ out.string
40
+ end
41
+
42
+ alias to_d to_djan
43
+
44
+ # to_d, but without colours
45
+ #
46
+ def to_dnc(x)
47
+
48
+ to_djan(x, colours: false)
49
+ end
50
+
51
+ def to_dj(x, out, opts)
52
+
53
+ case x
54
+ when nil then nil_to_d(x, out, opts)
55
+ when String then string_to_d(x, out, opts)
56
+ when Hash then object_to_d(x, out, opts)
57
+ when Array then array_to_d(x, out, opts)
58
+ when TrueClass then boolean_to_d(x.to_s, out, opts)
59
+ when FalseClass then boolean_to_d(x.to_s, out, opts)
60
+ else num_to_d(x.to_s, out, opts)
61
+ end
62
+ end
63
+
64
+ def len(x, opts)
65
+
66
+ opts = opts.merge(c: Colorato.no_colours, indent: nil, width: nil)
67
+ o = StringIO.new
68
+
69
+ to_dj(x, o, opts)
70
+
71
+ o.string.length
72
+ end
73
+
74
+ def adjust(x, opts)
75
+
76
+ i = opts[:indent]
77
+ w = opts[:width]
78
+
79
+ return opts unless i && w && (i + len(x, opts) < w)
80
+ opts.merge(indent: nil)
81
+ end
82
+
83
+ def newline(out, opts)
84
+
85
+ out << "\n"
86
+ end
87
+
88
+ def space(out, opts, force=false)
89
+
90
+ out << ' ' if force || ! opts[:compact]
91
+ end
92
+
93
+ def newline_or_space(out, opts)
94
+
95
+ if kt = opts[:keytab]
96
+ out << ' ' * kt
97
+ :indent
98
+ elsif opts[:indent]
99
+ newline(out, opts)
100
+ :newline
101
+ elsif ! opts[:compact]
102
+ space(out, opts)
103
+ :space
104
+ end
105
+ end
106
+
107
+ def indent_space(out, opts)
108
+
109
+ return if opts.delete(:first)
110
+ i = opts[:indent]
111
+ out << ' ' * i if i
112
+ end
113
+
114
+ def indent(opts, os={})
115
+
116
+ if kt = os[:keytab]
117
+ opts.merge(indent: nil, keytab: kt)
118
+ elsif i = opts[:indent]
119
+ opts.merge(indent: i + (os[:inc] || 1) * 2, first: os[:first])
120
+ else
121
+ opts
122
+ end
123
+ end
124
+
125
+ def object_to_d(x, out, opts)
126
+
127
+ inner = opts.delete(:inner)
128
+
129
+ indent_space(out, opts)
130
+
131
+ return c_inf('{}', out, opts) if x.empty?
132
+
133
+ opts = adjust(x, opts)
134
+
135
+ unless inner
136
+ c_inf('{', out, opts); space(out, opts)
137
+ end
138
+
139
+ key_max_len =
140
+ if opts[:compact]
141
+ nil
142
+ else
143
+ i = opts[:indent]
144
+ w = opts[:width]
145
+ #
146
+ kml, vml =
147
+ x.inject([ 0, 0 ]) { |(kl, vl), (k, v)|
148
+ [ [ kl, len(k.to_s, opts) ].max, [ vl, len(v, opts) ].max ] }
149
+ kml += 1
150
+ #
151
+ if i && w && i + kml + 1 + vml < w
152
+ kml
153
+ else
154
+ nil
155
+ end
156
+ end
157
+
158
+ x.each_with_index do |(k, v), ii|
159
+
160
+ kl = string_to_d(k, out, indent(opts, first: ii == 0))
161
+ c_inf(':', out, opts)
162
+
163
+ kt = key_max_len ? key_max_len - kl : nil
164
+ r = newline_or_space(out, opts.merge(keytab: kt))
165
+
166
+ to_dj(v, out, indent(opts, inc: 2, keytab: r == :newline ? kt : 1))
167
+
168
+ if ii < x.size - 1
169
+ c_inf(',', out, opts)
170
+ newline_or_space(out, opts)
171
+ end
172
+ end
173
+
174
+ unless inner
175
+ space(out, opts); c_inf('}', out, opts)
176
+ end
177
+ end
178
+
179
+ def array_to_d(x, out, opts)
180
+
181
+ inner = opts.delete(:inner)
182
+
183
+ indent_space(out, opts)
184
+
185
+ return c_inf('[]', out, opts) if x.empty?
186
+
187
+ opts = adjust(x, opts)
188
+
189
+ unless inner
190
+ c_inf('[', out, opts); space(out, opts)
191
+ end
192
+
193
+ x.each_with_index do |e, i|
194
+ to_dj(e, out, indent(opts, first: i == 0))
195
+ if i < x.size - 1
196
+ c_inf(',', out, opts)
197
+ newline_or_space(out, opts)
198
+ end
199
+ end
200
+
201
+ unless inner
202
+ space(out, opts); c_inf(']', out, opts)
203
+ end
204
+ end
205
+
206
+ def string_to_d(x, out, opts)
207
+
208
+ x = x.to_s
209
+
210
+ indent_space(out, opts)
211
+
212
+ if (
213
+ opts[:json] ||
214
+ x.match(/\A[^: \b\f\n\r\t"',()\[\]{}#\\+%\/><^!=-]+\z/) == nil ||
215
+ x.to_i.to_s == x ||
216
+ x.to_f.to_s == x ||
217
+ opts[:str_escape].include?(x)
218
+ ) then
219
+ s = x.inspect
220
+ c_str(s, out, opts)
221
+ s.length
222
+ else
223
+ c_str(x, out, opts)
224
+ x.length
225
+ end
226
+ end
227
+
228
+ def boolean_to_d(x, out, opts)
229
+
230
+ indent_space(out, opts); x ? c_tru(x, out, opts) : c_fal(x, out, opts)
231
+ end
232
+
233
+ def num_to_d(x, out, opts)
234
+
235
+ indent_space(out, opts); c_num(x, out, opts)
236
+ end
237
+
238
+ def nil_to_d(x, out, opts)
239
+
240
+ indent_space(out, opts); c_nil('null', out, opts)
241
+ end
242
+
243
+ def c_inf(s, out, opts); out << opts[:c].dark_gray(s); end
244
+
245
+ def c_nil(s, out, opts); out << opts[:c].dark_gray(s); end
246
+ def c_tru(s, out, opts); out << opts[:c].green(s); end
247
+ def c_fal(s, out, opts); out << opts[:c].red(s); end
248
+ #def c_str(s, out, opts); out << opts[:c].brown(s); end
249
+ def c_str(s, out, opts)
250
+ out << opts[:c].brown(s)
251
+ #out << opts[:c].brown(s).tap { |x| p [ x, x.encoding ] }
252
+ #out << opts[:c].brown(s).encode('UTF-8')
253
+ end
254
+ def c_num(s, out, opts); out << opts[:c].light_blue(s); end
255
+ end
256
+
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: djan
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - John Mettraux
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-12-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorato
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ description: Pretty pretting for floraison and flor.
42
+ email:
43
+ - jmettraux+flor@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - CHANGELOG.md
49
+ - LICENSE.txt
50
+ - README.md
51
+ - djan.gemspec
52
+ - lib/djan.rb
53
+ homepage: https://github.com/floraison/djan
54
+ licenses:
55
+ - MIT
56
+ metadata:
57
+ changelog_uri: https://github.com/floraison/djan/blob/master/CHANGELOG.md
58
+ bug_tracker_uri: https://github.com/floraison/djan/issues
59
+ documentation_uri: https://github.com/floraison/djan
60
+ homepage_uri: https://github.com/floraison/djan
61
+ source_code_uri: https://github.com/floraison/djan
62
+ rubygems_mfa_required: 'true'
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.5.16
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Pretty printing for flor
82
+ test_files: []