tco 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,199 @@
1
+ # tco - terminal colouring application and library
2
+ # Copyright (c) 2013, 2014 Radek Pazdera
3
+
4
+ # MIT License
5
+
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files (the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions:
12
+
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+
24
+ module Tco
25
+ class Token
26
+ attr_reader :type
27
+
28
+ def initialize(type, value)
29
+ @type = type
30
+ @value = value
31
+ end
32
+
33
+ def to_s
34
+ @value
35
+ end
36
+ end
37
+
38
+ class Segment
39
+ attr_reader :value, :params
40
+
41
+ def initialize(value, params)
42
+ @value = value
43
+ @params = params
44
+ end
45
+
46
+ def to_s
47
+ @value
48
+ end
49
+
50
+ # For rspec assertions
51
+ def ==(other)
52
+ @value == other.value && @params == other.params
53
+ end
54
+ end
55
+
56
+ class Parser
57
+ def initialize(default_style=nil)
58
+ @default_params = {
59
+ :base_style => nil,
60
+ :fg => nil,
61
+ :bg => nil,
62
+ :bright => false,
63
+ :underline => false
64
+ }
65
+
66
+ if default_style
67
+ @default_params[:fg] = default_style.fg
68
+ @default_params[:bg] = default_style.bg
69
+ @default_params[:bright] = default_style.bright
70
+ @default_params[:underline] = default_style.underline
71
+ end
72
+ end
73
+
74
+ def parse(string)
75
+ lexer string
76
+ parser
77
+ @segments
78
+ end
79
+
80
+ private
81
+ def submit_token(type, value)
82
+ return if value == ""
83
+
84
+ if type == :normal && @tokens.length > 0 && @tokens[-1].type == :normal
85
+ prev = @tokens.pop
86
+ @tokens.push Token.new :normal, prev.to_s + value
87
+ else
88
+ @tokens.push Token.new type, value
89
+ end
90
+ end
91
+
92
+ def lexer(in_str)
93
+ state = :default
94
+ @tokens = []
95
+ token = ""
96
+
97
+ in_str.split("").each do |c|
98
+ case state
99
+ when :default
100
+ case c
101
+ when "{"
102
+ submit_token :normal, token
103
+ token = c
104
+
105
+ state = :o_br
106
+ when "}"
107
+ submit_token :normal, token
108
+ token = c
109
+
110
+ state = :c_br
111
+ else
112
+ token << c
113
+ end
114
+ when :o_br
115
+ case c
116
+ when "{"
117
+ state = :definition
118
+ else
119
+ state = :default
120
+ end
121
+ token << c
122
+ when :definition
123
+ case c
124
+ when /[a-zA-Z0-9\:\-#]/
125
+ token << c
126
+ when /\s/
127
+ submit_token :definition, token
128
+ token = ""
129
+
130
+ state = :default
131
+ else
132
+ token << c
133
+ state = :default
134
+ end
135
+ when :c_br
136
+ token << c
137
+ if c == "}"
138
+ submit_token :end, token
139
+ token = ""
140
+ end
141
+ state = :default
142
+ end
143
+ end
144
+ submit_token :normal, token unless token == ""
145
+ end
146
+
147
+ def add_segment(t, params)
148
+ return if t.to_s == ""
149
+
150
+ if @segments.length > 0 && @segments[-1].params == params
151
+ prev = @segments.pop
152
+ @segments.push Segment.new prev.to_s + t.to_s, params
153
+ else
154
+ @segments.push Segment.new t.to_s, params
155
+ end
156
+ end
157
+
158
+ def parser
159
+ @segments = []
160
+ params = @default_params
161
+
162
+ stack = []
163
+ @tokens.each do |t|
164
+ case t.type
165
+ when :definition
166
+ prev_params = params
167
+ stack.push params
168
+
169
+ params = prev_params.clone
170
+
171
+ df = t.to_s[2..-1]
172
+ if df.include? ":"
173
+ c = df.split ":"
174
+ c.push "" if df[-1] == ":"
175
+
176
+ params[:fg] = c[0] unless c[0] == "" || c[0] == "-"
177
+ params[:bg] = c[1] unless c[1] == "" || c[1] == "-"
178
+ params[:bright] = true if c.length > 2 && c[2].include?("b")
179
+ params[:underline] = true if c.length > 2 && c[2].include?("u")
180
+ else
181
+ params[:base_style] = df
182
+ params[:fg] = nil
183
+ params[:bg] = nil
184
+ params[:bright] = false
185
+ params[:underline] = false
186
+ end
187
+ when :end
188
+ if stack.length > 0
189
+ params = stack.pop
190
+ else
191
+ add_segment t, params
192
+ end
193
+ else
194
+ add_segment t, params
195
+ end
196
+ end
197
+ end
198
+ end
199
+ end
@@ -0,0 +1,51 @@
1
+ # tco - terminal colouring application and library
2
+ # copyright (c) 2013, 2014 radek pazdera
3
+
4
+ # mit license
5
+
6
+ # permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files (the "software"), to deal
8
+ # in the software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the software, and to permit persons to whom the software is
11
+ # furnished to do so, subject to the following conditions:
12
+
13
+ # the above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the software.
15
+
16
+ # the software is provided "as is", without warranty of any kind, express or
17
+ # implied, including but not limited to the warranties of merchantability,
18
+ # fitness for a particular purpose and noninfringement. in no event shall the
19
+ # authors or copyright holders be liable for any claim, damages or other
20
+ # liability, whether in an action of contract, tort or otherwise, arising from,
21
+ # out of or in connection with the software or the use or other dealings in
22
+ # the software.
23
+
24
+ module Tco
25
+ class Style
26
+ attr_accessor :fg, :bg, :bright, :underline
27
+
28
+ def initialize(fg=nil, bg=nil, bright=false, underline=false)
29
+ @fg = fg
30
+ @bg = bg
31
+ @bright = bright
32
+ @underline = underline
33
+ end
34
+
35
+ def to_a
36
+ [@fg, @bg, @bright, @underline]
37
+ end
38
+
39
+ def to_h
40
+ {:fg => @fg, :bg => @bg, :bright => @bright, :underline => @underline}
41
+ end
42
+
43
+ def to_ary
44
+ to_a
45
+ end
46
+
47
+ def ==(o)
48
+ @fg == o.fg && @bg == o.bg && @bright == o.bright && @underline == o.underline
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,26 @@
1
+ # tco - terminal colouring application and library
2
+ # Copyright (c) 2013, 2014 Radek Pazdera
3
+
4
+ # MIT License
5
+
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files (the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions:
12
+
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+
24
+ module Tco
25
+ VERSION = "0.1.0"
26
+ end
@@ -0,0 +1,166 @@
1
+ # tco - terminal colouring application and library
2
+ # Copyright (c) 2013, 2014 Radek Pazdera
3
+
4
+ # MIT License
5
+
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files (the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions:
12
+
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+
24
+ require "utils"
25
+
26
+ require "tco/parser"
27
+
28
+ RSpec.configure do |c|
29
+ c.include Utils
30
+ end
31
+
32
+ describe Tco do
33
+ describe Tco::Parser do
34
+ before :all do
35
+ @p = Tco::Parser.new
36
+ end
37
+
38
+ it "handles no definitions" do
39
+ string = "London"
40
+ expected = [
41
+ Tco::Segment.new("London", get_params)
42
+ ]
43
+
44
+ segments = @p.parse string
45
+ segments.should == expected
46
+ end
47
+
48
+ it "works with fg colour only" do
49
+ string = "The City of {{grey: London}}, UK"
50
+ expected = [
51
+ Tco::Segment.new("The City of ", get_params),
52
+ Tco::Segment.new("London", get_params(nil, "grey")),
53
+ Tco::Segment.new(", UK", get_params)
54
+ ]
55
+
56
+ segments = @p.parse string
57
+ segments.should == expected
58
+ end
59
+
60
+ it "works with bg colour only" do
61
+ string = "The City of {{-:red London}}, UK"
62
+ expected = [
63
+ Tco::Segment.new("The City of ", get_params),
64
+ Tco::Segment.new("London", get_params(nil, nil, "red")),
65
+ Tco::Segment.new(", UK", get_params)
66
+ ]
67
+
68
+ segments = @p.parse string
69
+ segments.should == expected
70
+ end
71
+
72
+ it "works with both colours" do
73
+ string = "The City of {{grey:red London}}, UK"
74
+ expected = [
75
+ Tco::Segment.new("The City of ", get_params),
76
+ Tco::Segment.new("London", get_params(nil, "grey", "red")),
77
+ Tco::Segment.new(", UK", get_params)
78
+ ]
79
+
80
+ segments = @p.parse string
81
+ segments.should == expected
82
+ end
83
+
84
+ it "makes it bold" do
85
+ string = "The City of {{::b London}}, UK"
86
+ expected = [
87
+ Tco::Segment.new("The City of ", get_params),
88
+ Tco::Segment.new("London", get_params(nil, nil, nil, true)),
89
+ Tco::Segment.new(", UK", get_params)
90
+ ]
91
+
92
+ segments = @p.parse string
93
+ segments.should == expected
94
+ end
95
+
96
+ it "underlines the text" do
97
+ string = "The City of {{::u London}}, UK"
98
+ expected = [
99
+ Tco::Segment.new("The City of ", get_params),
100
+ Tco::Segment.new("London", get_params(nil, nil, nil, false, true)),
101
+ Tco::Segment.new(", UK", get_params)
102
+ ]
103
+
104
+ segments = @p.parse string
105
+ segments.should == expected
106
+ end
107
+
108
+ it "works with a style" do
109
+ string = "The City of {{alert London}}, UK"
110
+ expected = [
111
+ Tco::Segment.new("The City of ", get_params),
112
+ Tco::Segment.new("London", get_params("alert")),
113
+ Tco::Segment.new(", UK", get_params)
114
+ ]
115
+
116
+ segments = @p.parse string
117
+ segments.should == expected
118
+ end
119
+
120
+ it "can be nested" do
121
+ string = "The {{alert City of {{grey:red London}},}} UK"
122
+ expected = [
123
+ Tco::Segment.new("The ", get_params),
124
+ Tco::Segment.new("City of ", get_params("alert")),
125
+ Tco::Segment.new("London", get_params("alert", "grey", "red")),
126
+ Tco::Segment.new(",", get_params("alert")),
127
+ Tco::Segment.new(" UK", get_params)
128
+ ]
129
+
130
+ segments = @p.parse string
131
+ segments.should == expected
132
+ end
133
+
134
+ it "doesn't process empty template" do
135
+ string = "London {{}}"
136
+ expected = [
137
+ Tco::Segment.new("London {{}}", get_params),
138
+ ]
139
+
140
+ segments = @p.parse string
141
+ segments.should == expected
142
+ end
143
+
144
+ it "doesn't process template without a space" do
145
+ string = "{{grey:redLondon}}"
146
+ expected = [
147
+ Tco::Segment.new("{{grey:redLondon}}", get_params),
148
+ ]
149
+
150
+ segments = @p.parse string
151
+ segments.should == expected
152
+ end
153
+
154
+ it "ignores mismatched endings" do
155
+ string = "}} {{grey:red London}} }}"
156
+ expected = [
157
+ Tco::Segment.new("}} ", get_params),
158
+ Tco::Segment.new("London", get_params(nil, "grey", "red")),
159
+ Tco::Segment.new(" }}", get_params),
160
+ ]
161
+
162
+ segments = @p.parse string
163
+ segments.should == expected
164
+ end
165
+ end
166
+ end
@@ -0,0 +1,106 @@
1
+ # tco - terminal colouring application and library
2
+ # Copyright (c) 2013, 2014 Radek Pazdera
3
+
4
+ # MIT License
5
+
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files (the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions:
12
+
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+
24
+ require "tco"
25
+ require "tco/config"
26
+
27
+ describe Tco do
28
+ describe "String class extension" do
29
+ before :all do
30
+ config = Tco::Config.new
31
+ config.options["palette"] = "extended"
32
+ config.options["output"] = "raw"
33
+ Tco::reconfigure config
34
+ end
35
+
36
+ describe "#fg" do
37
+ it "works with references" do
38
+ result = "London".fg "@17"
39
+ result.should eql "\e[38;5;17mLondon\e[0m"
40
+ end
41
+
42
+ it "works with rgb" do
43
+ result = "London".fg "#00005f"
44
+ result.should eql "\e[38;5;17mLondon\e[0m"
45
+ end
46
+
47
+ it "works with hex" do
48
+ result = "London".fg "0x00005f"
49
+ result.should eql "\e[38;5;17mLondon\e[0m"
50
+ end
51
+
52
+ it "works with names" do
53
+ result = "London".fg "black"
54
+ result.should eql "\e[38;5;0mLondon\e[0m"
55
+ end
56
+ end
57
+
58
+ describe "#bg" do
59
+ it "works with references" do
60
+ result = "London".bg "@17"
61
+ result.should eql "\e[48;5;17mLondon\e[0m"
62
+ end
63
+
64
+ it "works with rgb" do
65
+ result = "London".bg "#00005f"
66
+ result.should eql "\e[48;5;17mLondon\e[0m"
67
+ end
68
+
69
+ it "works with hex" do
70
+ result = "London".bg "0x00005f"
71
+ result.should eql "\e[48;5;17mLondon\e[0m"
72
+ end
73
+
74
+ it "works with names" do
75
+ result = "London".bg "black"
76
+ result.should eql "\e[48;5;0mLondon\e[0m"
77
+ end
78
+ end
79
+
80
+ describe "#bright" do
81
+ it "makes the fond bold" do
82
+ result = "London".bright
83
+ result.should eql "\e[1mLondon\e[0m"
84
+ end
85
+ end
86
+
87
+ describe "#underline" do
88
+ it "underlines the text" do
89
+ result = "London".underline
90
+ result.should eql "\e[4mLondon\e[0m"
91
+ end
92
+ end
93
+
94
+ it "can be combined" do
95
+ result = "London".bright.underline.fg("@17").bg("@17")
96
+ result.should eql "\e[48;5;17m\e[38;5;17m\e[4m\e[1mLondon\e[0m\e[0m\e[0m\e[0m"
97
+ end
98
+
99
+ describe "#decorate" do
100
+ it "processes templates" do
101
+ result = " {{::u London}} ".decorate
102
+ result.should eql " \e[4mLondon\e[0m "
103
+ end
104
+ end
105
+ end
106
+ end