inkjet 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e023659caec2f6d13e4ccac3a5d9e4f43ad44dac
4
- data.tar.gz: d60ea9025dc3235ab98070c75db4100e3d0c9945
3
+ metadata.gz: a4af8afe6467c85adc6846dad80f5358ae21af5a
4
+ data.tar.gz: c85db1efd92d913881477a74f08bea7e4afaca06
5
5
  SHA512:
6
- metadata.gz: b30fb2d6a9da6fe1d8a6e0e0792ab7dcca1023db54b7b39de68519183b8451d17a8e20068fa9f6e6f1677902f8b4f6fecb5ac4330d0e5d4af350d9362954d46b
7
- data.tar.gz: 19f0086725fafe39236dd71caa6257574fc809c9cac33e5ef0d6cc9cf9447be63be62dc26b6329be2e6145a9e462f1ad41871c3f9b8525f636d8803586227fd1
6
+ metadata.gz: 2e04395528ab2ac80135d801afb883c9d2df2d950d8dd99716bce72c816bc17b4cdc412fc1157dc75c20521695a4bcaa79a60179b127cda9cdaf80e2ee45a234
7
+ data.tar.gz: 3e65522f8da9e88f79cad5d568ea9ba061d6b36e01e9b8cddab01f739717b16c0751fbaacc7d35e314c4962bc967f8fd9a664f55f14cf7cbd68e028ebc126745
@@ -40,37 +40,37 @@ module Inkjet
40
40
  base.send :extend, self
41
41
  end
42
42
 
43
- def colorize(color, str)
44
- colorize!(color, str.clone)
43
+ def colorize(color, str, wrap=false)
44
+ colorize!(color, str.clone, wrap)
45
45
  end
46
46
 
47
- def colorize!(color, str)
48
- str.apply_inkjet_code!(Inkjet::Colors.foreground(color))
47
+ def colorize!(color, str, wrap=false)
48
+ str.apply_inkjet_code!(Inkjet::Colors.foreground(color), wrap)
49
49
  end
50
50
 
51
- def colorize_background(color, str)
52
- colorize_background!(color, str.clone)
51
+ def colorize_background(color, str, wrap=false)
52
+ colorize_background!(color, str.clone, wrap)
53
53
  end
54
54
 
55
- def colorize_background!(color, str)
56
- str.apply_inkjet_code!(Inkjet::Colors.background(color))
55
+ def colorize_background!(color, str, wrap=false)
56
+ str.apply_inkjet_code!(Inkjet::Colors.background(color), wrap)
57
57
  end
58
58
 
59
- def colorize_with_background(fg_color, bg_color, str)
60
- colorize_with_background!(fg_color, bg_color, str.clone)
59
+ def colorize_with_background(fg_color, bg_color, str, wrap=false)
60
+ colorize_with_background!(fg_color, bg_color, str.clone, wrap)
61
61
  end
62
62
 
63
- def colorize_with_background!(fg_color, bg_color, str)
64
- str.apply_inkjet_codes!(Inkjet::Colors.foreground(fg_color), Inkjet::Colors.background(bg_color))
63
+ def colorize_with_background!(fg_color, bg_color, str, wrap=false)
64
+ str.apply_inkjet_codes!([Inkjet::Colors.foreground(fg_color), Inkjet::Colors.background(bg_color)], wrap)
65
65
  end
66
66
 
67
67
  def method_missing(meth, *args)
68
68
  if meth.match(/\A(#{Inkjet::Colors::Colors.push("default").join("|")})_text(!?)\Z/)
69
- colorize($1, args.map(&:to_s).join(" "))
69
+ colorize($1, *args)#.map(&:to_s).join(" "))
70
70
  elsif meth.match(/\A(#{Inkjet::Colors::Colors.push("default").join("|")})_(background|bg)(!?)\Z/)
71
- colorize_background($1, args.map(&:to_s).join(" "))
71
+ colorize_background($1, *args)#.map(&:to_s).join(" "))
72
72
  elsif meth.match(/\A(#{Inkjet::Colors::Colors.push("default").join("|")})_(#{Inkjet::Colors::Colors.push("default").join("|")})(!?)\Z/)
73
- colorize_with_background($1, $2, args.map(&:to_s).join(" "))
73
+ colorize_with_background($1, $2, *args)#.map(&:to_s).join(" "))
74
74
  else
75
75
  super
76
76
  end
@@ -80,11 +80,11 @@ module Inkjet
80
80
  module String
81
81
  def method_missing(meth, *args)
82
82
  if meth.match(/\A(#{Inkjet::Colors::Colors.join("|")})(!?)\Z/)
83
- Inkjet.send("colorize#{$2}", $1, self)
83
+ Inkjet.send("colorize#{$2}", $1, self, *args)
84
84
  elsif meth.match(/\A(#{Inkjet::Colors::Colors.join("|")})_(background|bg)(!?)\Z/)
85
- Inkjet.send("colorize_background#{$3}", $1, self)
85
+ Inkjet.send("colorize_background#{$3}", $1, self, *args)
86
86
  elsif meth.match(/\A(#{Inkjet::Colors::Colors.join("|")})_(#{Inkjet::Colors::Colors.join("|")})(!?)\Z/)
87
- Inkjet.send("colorize_with_background#{$3}", $1, $2, self)
87
+ Inkjet.send("colorize_with_background#{$3}", $1, $2, self, *args)
88
88
  else
89
89
  super
90
90
  end
@@ -1,85 +1,80 @@
1
1
  module Inkjet
2
2
  module String
3
- def bold
4
- clone.bold!
5
- end
6
-
7
- def bold!
8
- apply_inkjet_code!(Inkjet::Bold)
3
+
4
+ def method_missing(meth, *args)
5
+ if meth.match(/(#{%w(bold dim underline blink invert hidden).join('|')})(!?)/)
6
+ send "apply_inkjet_code#{$2}", "Inkjet::#{meth.capitalize}".constantize, *args
7
+ else
8
+ super
9
+ end
9
10
  end
10
-
11
- def dim
12
- clone.dim!
11
+
12
+ def clean
13
+ clone.clean!
13
14
  end
14
15
 
15
- def dim!
16
- apply_inkjet_code!(Inkjet::Dim)
16
+ def clean!
17
+ gsub!(/\e\[[\d;]+m/, '')
17
18
  end
18
19
 
19
- def underline
20
- clone.underline!
20
+ def apply_inkjet_code(code, wrap=false)
21
+ clone.apply_inkjet_code!(code, wrap)
21
22
  end
22
23
 
23
- def underline!
24
- apply_inkjet_code!(Inkjet::Underline)
25
- end
26
-
27
- def blink
28
- clone.blink!
24
+ def apply_inkjet_code!(code, wrap=false)
25
+ if inkjet_strings.length > 0
26
+ replace(inkjet_strings.map do |istr|
27
+ if wrap # apply the code to every chunk within the string, formatted or unformatted
28
+
29
+ (istr[0].empty? ? "" : apply_inkjet_codes_to_substring([code], istr[0])) +
30
+ apply_inkjet_codes_to_substring(istr[1].push(code), istr[2]) +
31
+ (istr[3].empty? ? "" : apply_inkjet_codes_to_substring([code], istr[3]))
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
+
43
+ end
44
+ end.join(""))
45
+ else # wrap the entire unformatted string w/ the code
46
+ replace("\e[#{code}m#{self}\e[#{Inkjet::Close}m")
47
+ end
29
48
  end
30
49
 
31
- def blink!
32
- apply_inkjet_code!(Inkjet::Blink)
50
+ def apply_inkjet_codes(codes, wrap=false)
51
+ cloned = clone
52
+ codes.each { |code| cloned.apply_inkjet_code!(code, wrap) }
53
+ cloned
33
54
  end
34
55
 
35
- def invert
36
- clone.invert!
37
- end
38
-
39
- def invert!
40
- apply_inkjet_code!(Inkjet::Invert)
56
+ def apply_inkjet_codes!(codes, wrap=false)
57
+ codes.each { |code| apply_inkjet_code!(code, wrap) }
58
+ self
41
59
  end
42
60
 
43
- def hidden
44
- clone.hidden!
45
- end
61
+ protected
46
62
 
47
- def hidden!
48
- apply_inkjet_code!(Inkjet::Hidden)
49
- end
50
-
63
+ # return the first set of escape codes found in the string if no pre-string
51
64
  def inkjet_codes
52
- match(/\e\[([\d;]+)m/)[1].split(";")
65
+ inkjet_strings.first[0].empty? ? inkjet_strings.first[1] : []
53
66
  rescue
54
- Array.new
55
- end
56
-
57
- def apply_inkjet_code(code)
58
- clone.apply_inkjet_code!(c)
67
+ []
59
68
  end
60
69
 
61
- def apply_inkjet_code!(code)
62
- inkjet_codes.length > 0 ? sub!(/\e\[([\d;]+)m/, "\e[#{inkjet_codes.push(code).join(";")}m") : replace("\e[#{code}m#{self}\e[0m")
63
- end
64
-
65
- def apply_inkjet_codes(*codes)
66
- sclone = clone
67
- codes.each { |code| sclone.apply_inkjet_code!(code) }
68
- sclone
69
- end
70
-
71
- def apply_inkjet_codes!(*codes)
72
- codes.each { |code| apply_inkjet_code!(code) }
73
- self
70
+ def inkjet_strings
71
+ scan(/([^\e]*)\e\[([\d;]+)m([^\e]*)\e\[#{Inkjet::Close}m([^\e]*)/).map { |pre,codes,str,post| [pre, codes.split(";"), str, post] } # TODO make a class for these
74
72
  end
75
73
 
76
- def clean
77
- clone.clean!
74
+ def apply_inkjet_codes_to_substring(codes, substr)
75
+ "\e[#{codes.join(";")}m#{substr}\e[#{Inkjet::Close}m"
78
76
  end
79
77
 
80
- def clean!
81
- gsub!(/\e\[[\d;]+m/, '')
82
- end
83
78
  end
84
79
  end
85
80
 
@@ -1,3 +1,3 @@
1
1
  module Inkjet
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
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.1
4
+ version: 0.0.2
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-08 00:00:00.000000000 Z
11
+ date: 2013-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport