ectoplasm 1.1.0

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: c1eb29f58f2221ef27a4f8f927700751434a9dcaf611b363d9d8e37475ab6dd8
4
+ data.tar.gz: 1d7a409eb8144a8f0d4aaeb4f5c021558187a2ae6dc919fbf68c17a0a25ed6e2
5
+ SHA512:
6
+ metadata.gz: 9ebc3a14e186fccdba32800f8070d87cf161199101fa4cc003c7a585c906c7a358e6788dc695e73beeabf2b1779d28dcccf30c548b24432251027b4ff1e62bad
7
+ data.tar.gz: b7f0953c64c18024fe555cf2d68a4444c432ae567e291a8ab988b403e9c61b1f61b5ad09904fec57734114ccb167559da588d5524a4e7f21249ce1412baabd85
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ vscode/
2
+ *.lock
3
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "rake", ">= 12.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Christian Neubauer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Ectoplasm
2
+
3
+ A helper library for console output
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ectoplasm"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/ectoplasm.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/ectoplasm'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "ectoplasm"
5
+ spec.version = Ectoplasm::VERSION
6
+ spec.authors = ["Christian Neubauer"]
7
+ spec.email = ["me@christianneubauer.de"]
8
+
9
+ spec.summary = "A helper library for console output"
10
+ spec.description = "Adds some extension methods to build in types to add prettier console output"
11
+ spec.homepage = "https://bitbucket.org/cneubaur/ectoplasm-ruby"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
14
+
15
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://bitbucket.org/cneubaur/ectoplasm-ruby"
19
+ spec.metadata["changelog_uri"] = "https://bitbucket.org/cneubaur/ectoplasm-ruby/src/master/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
data/lib/ectoplasm.rb ADDED
@@ -0,0 +1,219 @@
1
+ module Ectoplasm
2
+ module Version
3
+ MAJOR = 1
4
+ MINOR = 1
5
+ TINY = 0
6
+ end
7
+
8
+ VERSION = [Version::MAJOR, Version::MINOR, Version::TINY].compact * '.'
9
+ end
10
+
11
+ class String
12
+ @@colored = false
13
+
14
+ def self.colored!
15
+ @@colored = true
16
+ end
17
+
18
+ def white; self; end
19
+ def red; colored '31'; end
20
+ def green; colored '32'; end
21
+ def yellow; colored '33'; end
22
+ def blue; colored '34'; end
23
+ def magenta; colored '35'; end
24
+ def cyan; colored '36'; end
25
+ def grey; colored '90'; end
26
+
27
+ alias ok green
28
+ alias error red
29
+ alias warn yellow
30
+ alias info blue
31
+ alias dim grey
32
+
33
+ def indent amount, char: ' '
34
+ self.lines.map { |line| char * amount + line }.join "\n"
35
+ end
36
+
37
+ def length
38
+ m = /\e\[\d{2}m(.*)\e\[0m/.match self
39
+ return self.chars.count - 9 if m
40
+ self.chars.count
41
+ end
42
+
43
+ def lines separator="\n"
44
+ self.split separator
45
+ end
46
+
47
+ def frmt props, prefix_suffix=['<', '>']
48
+ str = self
49
+
50
+ props.keys.each do |key|
51
+ placeholder = prefix_suffix[0] + key.to_s + prefix_suffix[1]
52
+ str = str.gsub(placeholder, props[key])
53
+ end
54
+
55
+ str
56
+ end
57
+
58
+ private
59
+
60
+ def colored ansi_color
61
+ return self if !@@colored
62
+ "\e[#{ansi_color}m#{self}\e[0m"
63
+ end
64
+ end
65
+
66
+
67
+ class Object
68
+ def pretty width: nil
69
+ self.to_s
70
+ end
71
+ end
72
+
73
+
74
+ class TrueClass
75
+ def pretty width: nil
76
+ 'yes'.green
77
+ end
78
+ end
79
+
80
+
81
+ class FalseClass
82
+ def pretty width: nil
83
+ 'no'.yellow
84
+ end
85
+ end
86
+
87
+
88
+ class NilClass
89
+ def pretty width: nil
90
+ 'n/a'.grey
91
+ end
92
+ end
93
+
94
+
95
+ class Array
96
+ def pretty width: 25
97
+ return 'empty'.grey if self.length == 0
98
+
99
+ list_length = self.map { |x| x.to_s.length }.reduce(:+)
100
+ return self.join ', ' if list_length && list_length < 30
101
+
102
+ self
103
+ .select { |x| x != nil && x != '' }
104
+ .map do |x|
105
+ ' - ' + x.pretty(width: width-3).strip.gsub(/\n/, "\n ")
106
+ end
107
+ .join "\n"
108
+ end
109
+
110
+ def table header: nil, mappings: {}, with_index: false, limit: 50
111
+ header = self[0].keys if header == nil
112
+ heading = header.is_a?(Array) ? header : self[0].keys
113
+
114
+ table_data = self.slice(0, limit).map do |row|
115
+ heading.map do |key|
116
+ mappings.has_key?(key) ? mappings[key][row, row[key]] : row[key]
117
+ end
118
+ end
119
+
120
+ table_data.insert(0, heading) if header != false
121
+
122
+ data_sizes = table_data.map do |row|
123
+ row.map { |data| data.to_s.length }
124
+ end
125
+
126
+ column_sizes = data_sizes[0]
127
+ .zip(*data_sizes[1..-1])
128
+ .map { |row| row.max }
129
+
130
+ table = table_data.map { |row| column_sizes.zip row }
131
+
132
+ table_str = ''
133
+ table.each_with_index do |row, index|
134
+ if with_index
135
+ if index == 0
136
+ table_str += ' '
137
+ else
138
+ table_str += "[#{index}] "
139
+ end
140
+ end
141
+
142
+ row.each do |col_size, data|
143
+ table_str += (data.to_s + ' ' * (col_size - data.to_s.length)) + ' '
144
+ end
145
+
146
+ table_str += "\n"
147
+ end
148
+ table_str += '[...]' if self.count > limit
149
+ print table_str
150
+ end
151
+ end
152
+
153
+
154
+ class Hash
155
+ def pretty indent: 0, width: 25
156
+ s = ''
157
+
158
+ self
159
+ .select { |key, value| value != nil || value != '' }
160
+ .map do |key, value|
161
+ value = true if value == 'true'
162
+ value = false if value == 'false'
163
+ value = '********' if /password|pwd|pass|passwd|secret/ =~ key.to_s
164
+
165
+ if value.is_a? Hash
166
+ s += key.to_s.cyan.indent(indent) + "\n"
167
+ s += value.pretty(width: width-indent-2).indent(indent+2)
168
+ s += "\n"
169
+ next
170
+ end
171
+
172
+ s += ' ' * indent
173
+
174
+ if value.is_a? Array
175
+ list = value.pretty(width: width)
176
+
177
+ if list.lines.count > 1
178
+ s += key.to_s.cyan + "\n"
179
+ s += value.pretty(width: width).indent(indent)
180
+ s += "\n"
181
+ next
182
+ end
183
+
184
+ value = list
185
+ end
186
+
187
+ s += key.to_s.cyan
188
+ s += ' ' + '.' * (width-key.to_s.length-indent) if width-key.to_s.length > indent
189
+ s += ': '
190
+ s += value.pretty + "\n"
191
+ end
192
+
193
+ s
194
+ end
195
+ end
196
+
197
+
198
+ class Float
199
+ def duration
200
+ seconds = self
201
+
202
+ if seconds > 60
203
+ seconds = seconds.to_i
204
+ minutes = seconds / 60
205
+ seconds = seconds % 60
206
+ end
207
+
208
+ if minutes && minutes > 60
209
+ hours = minutes / 60
210
+ minutes = minutes % 60
211
+ end
212
+
213
+ duration_str = "#{seconds}s"
214
+ duration_str = "#{minutes}m #{duration_str}" if minutes
215
+ duration_str = "#{hours}h #{duration_str}" if hours
216
+
217
+ duration_str
218
+ end
219
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ectoplasm
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Christian Neubauer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-11-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Adds some extension methods to build in types to add prettier console
14
+ output
15
+ email:
16
+ - me@christianneubauer.de
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - bin/console
27
+ - bin/setup
28
+ - ectoplasm.gemspec
29
+ - lib/ectoplasm.rb
30
+ homepage: https://bitbucket.org/cneubaur/ectoplasm-ruby
31
+ licenses:
32
+ - MIT
33
+ metadata:
34
+ homepage_uri: https://bitbucket.org/cneubaur/ectoplasm-ruby
35
+ source_code_uri: https://bitbucket.org/cneubaur/ectoplasm-ruby
36
+ changelog_uri: https://bitbucket.org/cneubaur/ectoplasm-ruby/src/master/CHANGELOG.md
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 2.5.0
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubygems_version: 3.1.2
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: A helper library for console output
56
+ test_files: []