ark-util 0.2.2.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ark/utility.rb +163 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2a6bd3265dcdb1a7ab4533989f6f8cc86d10fe4f
4
+ data.tar.gz: 5fb51d42381ae0af8841e8cd1b4ff7c23014ca4e
5
+ SHA512:
6
+ metadata.gz: 9a8feba0cb8f50b1dbc7efe9f1b6b06e8cb887f5e54d8f411f598975bb915cecd7e53acc8ca88ebc91cfb83a368c6a1e70d3c81690dee3853442136e308310f3
7
+ data.tar.gz: 56ade2e7834f5e234941741c95e5e5342657097d50d8357d3f74f5e1ec5db120a7a157c740d0da59306c04e6af6733e8ae0a64bac1e5f020129b289961146286
@@ -0,0 +1,163 @@
1
+ # ark-util - utility library for ark-* gems
2
+ # Copyright 2015 Macquarie Sharpless <macquarie.sharpless@gmail.com>
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+
18
+ module Ark
19
+
20
+ # A stopwatch-like timer
21
+ class Timer
22
+ # Configuration details for Ark::Timer. Settings:
23
+ # [+:round+] Number of places to round the returned time to
24
+ Conf = {}
25
+ Conf[:round] = 2
26
+
27
+ # Reset the timer start time to now
28
+ def self.reset()
29
+ @@start = Time.now
30
+ end
31
+
32
+ # Return the time in seconds from the last call to #reset, or from the
33
+ # beginning of program execution
34
+ def self.time()
35
+ t = Time.now - @@start
36
+ t.round(Conf[:round]).to_s.ljust(5,'0')
37
+ end
38
+
39
+ reset
40
+
41
+ end
42
+
43
+ # Logging/messaging facilities intended for STDOUT
44
+ module Log
45
+ # Configuration details for Ark::Log. Settings:
46
+ # [+:quiet+] Suppress all messages of any verbosity
47
+ # [+:verbose+] Allow high-verbosity messages to be printed
48
+ # [+:timed+] Include the time since Timer#reset was called in all messages
49
+ Conf = {}
50
+ Conf[:quiet] = false
51
+ Conf[:verbose] = false
52
+ Conf[:timed] = true
53
+
54
+ # Write +msg+ to standard output according to verbosity settings. Not meant
55
+ # to be used directly
56
+ def say(msg, sym='...', loud=false, indent=0)
57
+ return false if Conf[:quiet]
58
+ return false if loud && !Conf[:verbose]
59
+ unless msg == ''
60
+ time = ""
61
+ if Conf[:timed]
62
+ time = Timer.time.to_s.ljust(4, '0')
63
+ time = time + " "
64
+ end
65
+ indent = " " * indent
66
+ indent = " " if indent == ""
67
+ puts "#{time}#{sym}#{indent}#{msg}"
68
+ else
69
+ puts
70
+ end
71
+ end
72
+ # Write a low-verbosity message to STDOUT
73
+ def msg(str, indent=0)
74
+ say(str, '>>>', false, indent)
75
+ end
76
+ # Write high-verbosity debugging information to STDOUT
77
+ def dbg(str, indent=0)
78
+ say(str, '...', true, indent)
79
+ end
80
+ # Write a high-verbosity warning to STDOUT
81
+ def wrn(str, indent=0)
82
+ say(str, '???', true, indent)
83
+ end
84
+
85
+ # Pulse a message for the duration of the execution of a block
86
+ def pulse(str, time, &block)
87
+ # TODO
88
+ end
89
+ end
90
+
91
+ # Methods for manipulating text
92
+ module Text
93
+ # Wrap a string to a given width, with an optional indent. Indented text
94
+ # will fall within the specified width.
95
+ # [+text+] The text to be wrapped
96
+ # [+width+] The number of columns to wrap within
97
+ # [+indent+] Indent each wrapped line of +text+ by this number of columns
98
+ def self.wrap(text, width: 78, indent: 0)
99
+ width -= indent
100
+ text = text.gsub(/\s+/, " ").gsub(/(.{1,#{width}})( |\Z)/, "\\1\n")
101
+ text.gsub(/^/, ' ' * indent)
102
+ end
103
+ end
104
+
105
+ # Build text progressively, line by line
106
+ class TextBuilder
107
+ # Initialize a TextBuilder instance
108
+ def initialize()
109
+ @lines = [[]]
110
+ @line = 0
111
+ end
112
+
113
+ # Push a string onto the current line
114
+ def push(str)
115
+ if str.is_a?(Array)
116
+ @lines[@line] += str.map(&:to_s)
117
+ else
118
+ @lines[@line] << str.to_s
119
+ end
120
+ end
121
+
122
+ # Add +str+ to the last string on the line. This avoids a space between the
123
+ # strings when the text is printed
124
+ def add(str)
125
+ @lines[@line][-1] += str.to_s
126
+ end
127
+
128
+ # Wrap the current line to +width+, with an optional +indent+. After
129
+ # wrapping, the current line will be the last line wrapped.
130
+ def wrap(width: 78, indent: 0)
131
+ text = @lines[@line].join(' ')
132
+ text = Text.wrap(text, width: width, indent: indent)
133
+ @lines.delete_at(@line)
134
+ @line -= 1
135
+ text.split("\n").each {|line| self.next(line) }
136
+ end
137
+
138
+ # Indent the current line by +count+ columns
139
+ def indent(count)
140
+ @lines[@line].unshift(' ' * (count - 1))
141
+ end
142
+
143
+ # Start a new line. If +str+ is provided, push +str+ onto the new line
144
+ def next(str=nil)
145
+ @lines << []
146
+ @line += 1
147
+ self.push(str) if str
148
+ end
149
+
150
+ # Insert a blank line and start the line after it. If +str+ is given, push
151
+ # +str+ onto the new line.
152
+ def skip(str=nil)
153
+ self.next()
154
+ self.next(str)
155
+ end
156
+
157
+ # Print the constructed text
158
+ def print()
159
+ @lines.map {|line| line.join(' ') }.join("\n")
160
+ end
161
+ end
162
+ end
163
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ark-util
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2.pre
5
+ platform: ruby
6
+ authors:
7
+ - Macquarie Sharpless
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Utility library for ark-* gems
14
+ email:
15
+ - macquarie.sharpless@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/ark/utility.rb
21
+ homepage: https://github.com/grimheart/ark-util
22
+ licenses:
23
+ - GPL-3.0
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">"
37
+ - !ruby/object:Gem::Version
38
+ version: 1.3.1
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.4.5.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Utility library for ark-* gems
45
+ test_files: []