ansiterm 0.1.0 → 0.2.0

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: fd09f57b3373ed993f3c332f543fbeafa2626cc7
4
- data.tar.gz: 0c046c9f0c3c3215bdc57bb15396724a1c4ef9ab
3
+ metadata.gz: 0df537615fddd8fe2fed0443af028139d1444979
4
+ data.tar.gz: c5c72311de8e4022f7d8f951038f782ef8eadf57
5
5
  SHA512:
6
- metadata.gz: '07481354e1d2dc0af995a64e3d246617e12c01bf84a7d6434f0155969948fb507f4b91a886e1392a332f030e6921ca1ee64ae5ff7748e246e1f86e5eb1a10384'
7
- data.tar.gz: 469c7747ad4c930d3a3f759bc772e5018668932b89200fd0b9ca4970e22ad14d1098b1c01e6422fe2939e2283bc7a64b1524366807cbec070390e94151b82f8b
6
+ metadata.gz: e61b1784d2b29c07bca256043698320180542b7b1a44a3a3af5680e8eaa6c4ebf15bbf1a84cc620772b193e7321733a24d030df990684943b177b69e1fb09f82
7
+ data.tar.gz: 6e1a7d0b76d5f7f11f85761fabddf6372294681d3f744fa4522d76fc30cab08bd27ce3426d3e01f780b1825135a4e876b1b3dd58e7bce31f67d7c97500b8420e
data/lib/ansiterm.rb CHANGED
@@ -1,2 +1,4 @@
1
1
  require "ansiterm/version"
2
2
  require "ansiterm/attr"
3
+ require "ansiterm/string"
4
+
data/lib/ansiterm/attr.rb CHANGED
@@ -35,7 +35,10 @@ module AnsiTerm
35
35
  end
36
36
 
37
37
  def add_flag(flags); merge({flags: @flags | flags}); end
38
-
38
+ def clear_flag(flags); merge({flags: @flags & ~BOLD}); end
39
+
40
+ def reset; self.class.new; end
41
+ def normal; clear_flag(BOLD); end
39
42
  def bold; add_flag(BOLD); end
40
43
  def underline; add_flag(UNDERLINE); end
41
44
  def crossed_out; add_flag(CROSSED_OUT); end
@@ -44,11 +47,20 @@ module AnsiTerm
44
47
  def underline?; (@flags & UNDERLINE) != 0; end
45
48
  def crossed_out?; (@flags & CROSSED_OUT) != 0; end
46
49
 
50
+ def normal?
51
+ @flags == NORMAL &&
52
+ @fgcol.nil? &&
53
+ @bgcol.nil?
54
+ end
55
+
47
56
  def transition_to(other)
48
57
  t = []
49
58
  t << [other.fgcol] if other.fgcol != self.fgcol
50
59
  t << [other.bgcol] if other.bgcol != self.bgcol
51
- t << [1] if other.bold? && !self.bold?
60
+
61
+ if other.bold? != self.bold?
62
+ t << [other.bold? ? 1 : 22]
63
+ end
52
64
 
53
65
  if other.underline? != self.underline?
54
66
  t << [other.underline? ? 4 : 24]
@@ -57,6 +69,8 @@ module AnsiTerm
57
69
  if other.crossed_out? != self.crossed_out?
58
70
  t << [other.crossed_out? ? 9 : 29]
59
71
  end
72
+
73
+ return "\e[0m" if other.normal? && !self.normal? && t.length != 1
60
74
 
61
75
  if t.empty?
62
76
  ""
@@ -0,0 +1,118 @@
1
+
2
+ module AnsiTerm
3
+
4
+ class String
5
+ def initialize(str="")
6
+ parse(str)
7
+ end
8
+
9
+ def to_str
10
+ out = ""
11
+ a = Attr.new
12
+ @str.length.times.each do |i|
13
+ if a != @attrs[i]
14
+ old = a
15
+ a = @attrs[i]||Attr.new
16
+ out << old.transition_to(a)
17
+ end
18
+ out << @str[i]
19
+ end
20
+ out
21
+ end
22
+
23
+ def encoding
24
+ @str.encoding
25
+ end
26
+
27
+ def length
28
+ @str.length
29
+ end
30
+
31
+ def set(str,attrs)
32
+ @str, @attrs = str,Array(attrs)
33
+ end
34
+
35
+ def[] i
36
+ str = @str[i]
37
+ if str
38
+ a = self.class.new
39
+ a.set(str,@attrs[i])
40
+ a
41
+ else
42
+ nil
43
+ end
44
+ end
45
+
46
+
47
+ private
48
+
49
+ def parse_color(par, params, a, attr_name)
50
+ col = par
51
+ if col == 38 || col == 48
52
+ par = params.shift
53
+ if par == "5"
54
+ col = [col,5,params.shift].join(";")
55
+ elsif par == "2"
56
+ col = [col,5,params.shift,params.shift, params.shift].join(";")
57
+ end
58
+ end
59
+ a.merge(attr_name => col)
60
+ end
61
+
62
+ def parse(str)
63
+ @str = ""
64
+ @attrs = []
65
+ a = AnsiTerm::Attr.new
66
+
67
+ max = str.length
68
+ i = 0
69
+
70
+ while i < max
71
+ c = str[i]
72
+ if c == "\e" && str[i+1] == "[" # CSI
73
+ params = ""
74
+ i += 2
75
+ while i < max && str[i].ord < 0x40
76
+ params << str[i]
77
+ i+=1
78
+ end
79
+ final = str[i]
80
+
81
+ if final == "m"
82
+ params = params.split(";")
83
+ while par = params.shift
84
+ par = par.to_i
85
+ case par
86
+ when 0
87
+ a = a.reset
88
+ when 1
89
+ a = a.bold
90
+ when 4
91
+ a = a.underline
92
+ when 9
93
+ a = a.crossed_out
94
+ when 22
95
+ a = a.normal
96
+ when 24
97
+ a = a.clear_flag(Attr::UNDERLINE)
98
+ when 29
99
+ a = a.clear_flag(Attr::CROSSED_OUT)
100
+ when 30..39
101
+ a = parse_color(par, params, a, :fgcol)
102
+ when 40..49
103
+ a = parse_color(par, params, a, :bgcol)
104
+ else
105
+ @str << "[unknown escape: #{par}]"
106
+ end
107
+ end
108
+ end
109
+ else
110
+ @str << c
111
+ @attrs << a
112
+ end
113
+ i += 1
114
+ end
115
+ self
116
+ end
117
+ end
118
+ end
@@ -1,3 +1,3 @@
1
1
  module AnsiTerm
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ansiterm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vidar Hokstad
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-20 00:00:00.000000000 Z
11
+ date: 2017-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,6 +70,7 @@ files:
70
70
  - bin/setup
71
71
  - lib/ansiterm.rb
72
72
  - lib/ansiterm/attr.rb
73
+ - lib/ansiterm/string.rb
73
74
  - lib/ansiterm/version.rb
74
75
  homepage: https://github.com/vidarh/ansiterm
75
76
  licenses: