inkjet 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/lib/inkjet.rb +2 -6
- data/lib/inkjet/indent.rb +36 -6
- data/lib/inkjet/string.rb +84 -40
- data/lib/inkjet/styles.rb +50 -0
- data/lib/inkjet/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 834476aeabcd5ab348125c5fb525db4859ee4dfa
|
4
|
+
data.tar.gz: a6f6b0b36d1a46213858d02febd83774bfd7b14c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: daf70655f31d52f49a3f29d7d0487d4824ab8eede474c7044a80b38ab58d61fc8ae589850e9584d0a36de9ee1326be9f4c747de134a2d1e42186c6dc17636e72
|
7
|
+
data.tar.gz: 6024564d3bea03575d45bd7e00aeb1de9d172453385c775357c31cfcb8500e1128236e6dd4bb7a4e709a518c2fba462699df62d37bc2f9a98d0a21d0ab6c84fd
|
data/lib/inkjet.rb
CHANGED
@@ -1,17 +1,13 @@
|
|
1
1
|
require 'active_support/inflector'
|
2
2
|
require 'inkjet/string'
|
3
3
|
require 'inkjet/colors'
|
4
|
+
require 'inkjet/styles'
|
4
5
|
require 'inkjet/indent'
|
5
6
|
|
6
7
|
module Inkjet
|
7
8
|
Close = 0
|
8
|
-
Bold = 1
|
9
|
-
Dim = 2
|
10
|
-
Underline = 4
|
11
|
-
Blink = 5
|
12
|
-
Invert = 7
|
13
|
-
Hidden = 8
|
14
9
|
include Colors::Formatters
|
10
|
+
include Styles::Formatters
|
15
11
|
|
16
12
|
def self.escape(code)
|
17
13
|
"\e[#{code.to_s.chomp('m')}m"
|
data/lib/inkjet/indent.rb
CHANGED
@@ -1,29 +1,59 @@
|
|
1
1
|
module Inkjet
|
2
2
|
module Indent
|
3
3
|
TABSTOP = 2 # TODO set based on config
|
4
|
+
|
5
|
+
class Formatter
|
6
|
+
def call_on(str)
|
7
|
+
str.send @meth, *@args
|
8
|
+
end
|
9
|
+
|
10
|
+
protected
|
11
|
+
|
12
|
+
def initialize(meth, *args)
|
13
|
+
@meth = meth.to_sym
|
14
|
+
@args = args
|
15
|
+
end
|
16
|
+
end
|
4
17
|
|
5
18
|
def self.indent(*args, &block)
|
6
|
-
|
19
|
+
@@spaces ||= 0
|
7
20
|
if block_given?
|
8
|
-
|
21
|
+
@@spaces += args[0] || TABSTOP
|
22
|
+
scoped_formatters = formatters.clone
|
23
|
+
|
9
24
|
class_eval &block
|
10
|
-
|
25
|
+
|
26
|
+
@@formatters = scoped_formatters
|
27
|
+
@@spaces -= args[0] || TABSTOP
|
11
28
|
else
|
12
29
|
spaces = args[1] || TABSTOP
|
13
|
-
"#{padding(
|
30
|
+
"#{padding(@@spaces + spaces.to_i)}#{args[0].to_s.split("\n").join("\n#{padding(@@spaces + spaces.to_i)}")}"
|
14
31
|
end
|
15
32
|
end
|
16
33
|
|
34
|
+
def self.formatters
|
35
|
+
@@formatters ||= []
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.format_with(fmeth, *fargs)
|
39
|
+
formatters.push(Formatter.new(fmeth, *fargs))
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.apply_formatters(output)
|
43
|
+
formatters.each { |f| output = f.call_on(output) }
|
44
|
+
output
|
45
|
+
end
|
46
|
+
|
17
47
|
def self.padding(spaces)
|
18
48
|
spaces.times.map {" "}.join
|
19
49
|
end
|
20
50
|
|
21
51
|
def self.puts(output)
|
22
|
-
STDOUT.puts indent(output, 0)
|
52
|
+
STDOUT.puts apply_formatters(indent(output))#indent(output, 0)
|
23
53
|
end
|
24
54
|
|
25
55
|
def self.print(output)
|
26
|
-
STDOUT.print indent(output, 0)
|
56
|
+
STDOUT.print apply_formatters(indent(output))#indent(output, 0)
|
27
57
|
end
|
28
58
|
|
29
59
|
module String
|
data/lib/inkjet/string.rb
CHANGED
@@ -1,52 +1,94 @@
|
|
1
1
|
module Inkjet
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
super
|
9
|
-
end
|
2
|
+
class Substring
|
3
|
+
attr_accessor :pre, :codes, :string, :post
|
4
|
+
|
5
|
+
def push(code)
|
6
|
+
codes.push(code)
|
7
|
+
self
|
10
8
|
end
|
11
9
|
|
12
|
-
def
|
13
|
-
|
10
|
+
def unshift(code)
|
11
|
+
codes.unshift(code)
|
12
|
+
self
|
14
13
|
end
|
15
14
|
|
15
|
+
def formattable?(chunk)
|
16
|
+
!chunk.nil? && !chunk.empty? #&& !chunk.match(/\A[\r\n]+\Z/)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Formats a chunk of the substring
|
20
|
+
#
|
21
|
+
# If no codes are provided, or the chunk is empty, it si not formatted
|
22
|
+
def format_chunk(chunk, codes)
|
23
|
+
codes = [codes].flatten
|
24
|
+
return chunk if !formattable?(chunk) || (codes.nil? || codes.empty?)
|
25
|
+
"\e[#{codes.join(";")}m#{chunk}\e[#{Inkjet::Close}m"
|
26
|
+
end
|
27
|
+
|
28
|
+
# Format the string by applying the proper codes to various chunks of the string.
|
29
|
+
#
|
30
|
+
# The primary string is formatted with this object's @codes
|
31
|
+
#
|
32
|
+
# If a super_code is provided, it is used to format the superstring (pre/post) chunks,
|
33
|
+
# otherwise they are left unformatted.
|
34
|
+
def format(super_code=nil)
|
35
|
+
format_chunk(pre, super_code) +
|
36
|
+
format_chunk(string, codes) +
|
37
|
+
format_chunk(post, super_code)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def initialize(pre, codes, string, post)
|
43
|
+
@pre = pre
|
44
|
+
@codes = codes
|
45
|
+
@string = string
|
46
|
+
@post = post
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
module String
|
51
|
+
|
52
|
+
# Clean out all escape codes from the formatted string
|
16
53
|
def clean!
|
17
54
|
gsub!(/\e\[[\d;]+m/, '')
|
18
55
|
end
|
19
|
-
|
20
|
-
def
|
21
|
-
clone.
|
56
|
+
|
57
|
+
def clean
|
58
|
+
clone.clean!
|
22
59
|
end
|
23
60
|
|
61
|
+
# Apply a formatting code to the appropriate chunks in the string.
|
62
|
+
#
|
63
|
+
# If forcing a wrap, all chunks within the string get formatted with the new code, regardless of whether they were previously formatted
|
64
|
+
#
|
65
|
+
# If there are codes applied to the first chunk of the string, apply the new code to all previously formatted chunks with matching codes
|
66
|
+
#
|
67
|
+
# If the first chunk of the string is unformatted, apply the new code to all unformatted chunks
|
68
|
+
#
|
69
|
+
# If the string is completely unformatted, enclose self within the provided code
|
24
70
|
def apply_inkjet_code!(code, wrap=false)
|
25
|
-
if
|
26
|
-
|
71
|
+
if inkjet_substrings.length > 0
|
72
|
+
|
73
|
+
replace(inkjet_substrings.map do |substr|
|
27
74
|
if wrap # apply the code to every chunk within the string, formatted or unformatted
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
elsif !inkjet_codes.empty? && istr[1] == inkjet_codes # apply the code only to the formatted chunks of the 'primary string'
|
34
|
-
|
35
|
-
istr[0] + apply_inkjet_codes_to_substring(istr[1].push(code), istr[2]) + istr[3]
|
36
|
-
|
37
|
-
else # apply the code only to the unformatted chunks of the 'primary string' leaving any formatted substrings untouched
|
38
|
-
|
39
|
-
(istr[0].empty? ? "" : apply_inkjet_codes_to_substring([code], istr[0])) +
|
40
|
-
apply_inkjet_codes_to_substring(istr[1], istr[2]) +
|
41
|
-
(istr[3].empty? ? "" : apply_inkjet_codes_to_substring([code], istr[3]))
|
42
|
-
|
75
|
+
substr.push(code).format(code)
|
76
|
+
elsif !inkjet_codes.empty? && substr.codes == inkjet_codes # apply the code only to formatted chunks with matching codes
|
77
|
+
substr.push(code).format
|
78
|
+
else # apply the code only to the unformatted chunks
|
79
|
+
substr.format(code)
|
43
80
|
end
|
44
|
-
end.join
|
45
|
-
|
81
|
+
end.join)
|
82
|
+
|
83
|
+
else # enclose the entire unformatted string w/ the code
|
46
84
|
replace("\e[#{code}m#{self}\e[#{Inkjet::Close}m")
|
47
85
|
end
|
48
86
|
end
|
49
87
|
|
88
|
+
def apply_inkjet_code(code, wrap=false)
|
89
|
+
clone.apply_inkjet_code!(code, wrap)
|
90
|
+
end
|
91
|
+
|
50
92
|
def apply_inkjet_codes(codes, wrap=false)
|
51
93
|
cloned = clone
|
52
94
|
codes.each { |code| cloned.apply_inkjet_code!(code, wrap) }
|
@@ -60,19 +102,21 @@ module Inkjet
|
|
60
102
|
|
61
103
|
protected
|
62
104
|
|
63
|
-
#
|
105
|
+
# Return the first set of escape codes found in the string if no pre-string.
|
106
|
+
#
|
107
|
+
# This determines whether the main chunks of the string are already formatted or not,
|
108
|
+
# and helps decide whether multiple chunks of a string should be formatted when not
|
109
|
+
# forcing everything to be wrapped.
|
64
110
|
def inkjet_codes
|
65
|
-
|
111
|
+
inkjet_substrings.first.pre.empty? ? inkjet_substrings.first.codes : []
|
66
112
|
rescue
|
67
113
|
[]
|
68
114
|
end
|
69
115
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
def apply_inkjet_codes_to_substring(codes, substr)
|
75
|
-
"\e[#{codes.join(";")}m#{substr}\e[#{Inkjet::Close}m"
|
116
|
+
# Scan the string for formatted chunks and their surrounding unformatted chunks and
|
117
|
+
# return an array of Inkjet::Substring objects.
|
118
|
+
def inkjet_substrings
|
119
|
+
scan(/([^\e]*)\e\[([\d;]+)m([^\e]*)\e\[#{Inkjet::Close}m([^\e]*)/).map { |pre,codes,str,post| Inkjet::Substring.new(pre, codes.split(";"), str, post) }
|
76
120
|
end
|
77
121
|
|
78
122
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Inkjet
|
2
|
+
module Styles
|
3
|
+
Styles = %w(bold dim underline blink invert hidden)
|
4
|
+
Bold = 1
|
5
|
+
Dim = 2
|
6
|
+
Underline = 4
|
7
|
+
Blink = 5
|
8
|
+
Invert = 7
|
9
|
+
Hidden = 8
|
10
|
+
|
11
|
+
def self.style(style)
|
12
|
+
"Inkjet::Styles::#{style.capitalize}".constantize rescue raise("Style does not exist: '#{style}'")
|
13
|
+
end
|
14
|
+
|
15
|
+
module Formatters
|
16
|
+
def self.included(base)
|
17
|
+
base.send :extend, self
|
18
|
+
end
|
19
|
+
|
20
|
+
def stylize(style, str, wrap=false)
|
21
|
+
stylize!(style, str.clone, wrap)
|
22
|
+
end
|
23
|
+
|
24
|
+
def stylize!(style, str, wrap=false)
|
25
|
+
str.apply_inkjet_code!(Inkjet::Styles.style(style), wrap)
|
26
|
+
end
|
27
|
+
|
28
|
+
def method_missing(meth, *args)
|
29
|
+
if meth.match(/\A(#{Inkjet::Styles::Styles.join("|")})(!?)\Z/)
|
30
|
+
stylize($1, *args)
|
31
|
+
else
|
32
|
+
super(meth, *args)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module String
|
38
|
+
def method_missing(meth, *args)
|
39
|
+
if meth.match(/\A(#{Inkjet::Styles::Styles.join("|")})(!?)\Z/)
|
40
|
+
Inkjet.send("stylize#{$2}", $1, self, *args)
|
41
|
+
else
|
42
|
+
super(meth, *args)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
String.send :include, Inkjet::Styles::String
|
data/lib/inkjet/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: inkjet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Rebec
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-06-
|
11
|
+
date: 2013-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -46,6 +46,7 @@ extensions: []
|
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
48
|
- lib/inkjet/string.rb
|
49
|
+
- lib/inkjet/styles.rb
|
49
50
|
- lib/inkjet/indent.rb
|
50
51
|
- lib/inkjet/configuration.rb
|
51
52
|
- lib/inkjet/colors.rb
|