delicious-cli 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,224 @@
1
+ require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
2
+ require 'set'
3
+
4
+ ##
5
+ # cute.
6
+ #
7
+ # >> "this is red".red
8
+ #
9
+ # >> "this is red with a blue background (read: ugly)".red_on_blue
10
+ #
11
+ # >> "this is red with an underline".red.underline
12
+ #
13
+ # >> "this is really bold and really blue".bold.blue
14
+ #
15
+ # >> Colored.red "This is red" # but this part is mostly untested
16
+ module Colored
17
+ extend self
18
+
19
+ ###########################################################################
20
+
21
+ @@is_tty = STDOUT.isatty
22
+
23
+ COLORS = {
24
+ 'black' => 30,
25
+ 'red' => 31,
26
+ 'green' => 32,
27
+ 'yellow' => 33,
28
+ 'blue' => 34,
29
+ 'magenta' => 35,
30
+ 'cyan' => 36,
31
+ 'white' => 37
32
+ }
33
+
34
+ EXTRAS = {
35
+ 'clear' => 0,
36
+ 'bold' => 1,
37
+ 'light' => 1,
38
+ 'underline' => 4,
39
+ 'reversed' => 7
40
+ }
41
+
42
+ #
43
+ # BBS-style numeric color codes.
44
+ #
45
+ BBS_COLOR_TABLE = {
46
+ 0 => :black,
47
+ 1 => :blue,
48
+ 2 => :green,
49
+ 3 => :cyan,
50
+ 4 => :red,
51
+ 5 => :magenta,
52
+ 6 => :yellow,
53
+ 7 => :white,
54
+ 8 => :light_black,
55
+ 9 => :light_blue,
56
+ 10 => :light_green,
57
+ 11 => :light_cyan,
58
+ 12 => :light_red,
59
+ 13 => :light_magenta,
60
+ 14 => :light_yellow,
61
+ 15 => :light_white,
62
+ }
63
+
64
+ VALID_COLORS = Set.new(
65
+ COLORS.keys +
66
+ COLORS.map { |k,v| "light_#{k}" } +
67
+ COLORS.map { |k,v| "bold_#{k}" }
68
+ )
69
+
70
+ ###########################################################################
71
+
72
+ COLORS.each do |color, value|
73
+ define_method(color) do
74
+ colorize(self, :foreground => color)
75
+ end
76
+
77
+ define_method("on_#{color}") do
78
+ colorize(self, :background => color)
79
+ end
80
+
81
+ define_method("light_#{color}") do
82
+ colorize(self, :foreground => color, :extra => 'bold')
83
+ end
84
+
85
+ COLORS.each do |highlight, value|
86
+ next if color == highlight
87
+ define_method("#{color}_on_#{highlight}") do
88
+ colorize(self, :foreground => color, :background => highlight)
89
+ end
90
+ end
91
+ end
92
+
93
+ EXTRAS.each do |extra, value|
94
+ next if extra == 'clear'
95
+ define_method(extra) do
96
+ colorize(self, :extra => extra)
97
+ end
98
+ end
99
+
100
+ define_method(:to_eol) do
101
+ tmp = sub(/^(\e\[[\[\e0-9;m]+m)/, "\\1\e[2K")
102
+ if tmp == self
103
+ return "\e[2K" << self
104
+ end
105
+ tmp
106
+ end
107
+
108
+ ###########################################################################
109
+
110
+ def colorize(string=nil, options = {})
111
+ if string == nil
112
+ return tagged_colors(self)
113
+ end
114
+
115
+ if @@is_tty
116
+ colored = [color(options[:foreground]), color("on_#{options[:background]}"), extra(options[:extra])].compact * ''
117
+ colored << string
118
+ colored << extra(:clear)
119
+ else
120
+ string
121
+ end
122
+ end
123
+
124
+ def colors
125
+ @@colors ||= COLORS.keys.sort
126
+ end
127
+
128
+ def extra(extra_name)
129
+ extra_name = extra_name.to_s
130
+ "\e[#{EXTRAS[extra_name]}m" if EXTRAS[extra_name]
131
+ end
132
+
133
+ def color(color_name)
134
+ background = color_name.to_s =~ /on_/
135
+ color_name = color_name.to_s.sub('on_', '')
136
+ return unless color_name && COLORS[color_name]
137
+ "\e[#{COLORS[color_name] + (background ? 10 : 0)}m"
138
+ end
139
+
140
+ ###########################################################################
141
+
142
+ def enable!
143
+ @@is_tty = true
144
+ end
145
+
146
+ alias_method :force!, :enable!
147
+
148
+ def disable!
149
+ @@is_tty = false
150
+ end
151
+
152
+ def is_tty?
153
+ @@is_tty
154
+ end
155
+
156
+ #
157
+ # Is this string legal?
158
+ #
159
+ def valid_tag?(tag)
160
+ VALID_COLORS.include?(tag) or
161
+ (
162
+ string =~ /^\d+$/ and
163
+ BBS_COLOR_TABLE.include?(tag.to_i)
164
+ )
165
+ end
166
+
167
+ #
168
+ # Colorize a string that has "color tags".
169
+ #
170
+ # Examples:
171
+ #
172
+ # Colors as words:
173
+ # puts "<light_yellow><light_white>*</light_white> Hey mom! I am <light_green>SO</light_green> colourized right now.</light_yellow>".colorize
174
+ #
175
+ # Numeric ANSI colors (from the BBS days):
176
+ # puts "<10><5>*</5> Hey mom! I am <9>SO</9> colourized right now.</10>".colorize
177
+ #
178
+ def tagged_colors(string)
179
+ stack = []
180
+
181
+ # split the string into tags and literal strings
182
+ tokens = string.split(/(<\/?[\w\d_]+>)/)
183
+ tokens.delete_if { |token| token.size == 0 }
184
+
185
+ result = ""
186
+
187
+ tokens.each do |token|
188
+
189
+ # token is an opening tag!
190
+
191
+ if /<([\w\d_]+)>/ =~ token and valid_tag?($1)
192
+ stack.push $1
193
+
194
+ # token is a closing tag!
195
+
196
+ elsif /<\/([\w\d_]+)>/ =~ token and valid_tag?($1)
197
+
198
+ # if this color is on the stack somwehere...
199
+ if pos = stack.rindex($1)
200
+ # close the tag by removing it from the stack
201
+ stack.delete_at pos
202
+ else
203
+ raise "Error: tried to close an unopened color tag -- #{token}"
204
+ end
205
+
206
+ # token is a literal string!
207
+
208
+ else
209
+
210
+ color = (stack.last || "white")
211
+ color = BBS_COLOR_TABLE[color.to_i] if color =~ /^\d+$/
212
+ result << token.send(color)
213
+
214
+ end
215
+
216
+ end
217
+
218
+ result
219
+ end
220
+
221
+
222
+ end unless Object.const_defined? :Colored
223
+
224
+ String.send(:include, Colored)
@@ -5,7 +5,7 @@ require 'open3'
5
5
  ## Load the colorize gem, and define the "hilite" function
6
6
  begin
7
7
 
8
- require 'delicious-cli/colorize'
8
+ require 'delicious-cli/colored'
9
9
 
10
10
  # Colourized hilite...
11
11
  class String
@@ -28,14 +28,6 @@ begin
28
28
 
29
29
  end
30
30
 
31
- rescue LoadError
32
-
33
- STDERR.puts "Note: You should install the 'colorize' gem for extra prettiness.\n"
34
- # Monochrome hilite does nothing...
35
- class String
36
- def hilite(words); self; end
37
- end
38
-
39
31
  end
40
32
 
41
33
 
@@ -108,10 +100,10 @@ def display(post, query=nil, indent_size=11)
108
100
  url = post["href"].hilite(query, :light_blue)
109
101
  tag_lines = post["tag"].wrap(wrap_size-2).map { |line| line.hilite(query, :light_cyan) }
110
102
 
111
- if post["extended"].any?
112
- ext_lines = post["extended"].wrap(wrap_size).map { |line| line.hilite(query, :white) }
113
- else
103
+ if post["extended"].blank?
114
104
  ext_lines = []
105
+ else
106
+ ext_lines = post["extended"].wrap(wrap_size).map { |line| line.hilite(query, :white) }
115
107
  end
116
108
 
117
109
  # date / description
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delicious-cli
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
- - 3
9
- - 2
10
- version: 0.3.2
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
11
10
  platform: ruby
12
11
  authors:
13
12
  - epitron
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-10-25 00:00:00 -04:00
17
+ date: 2010-11-29 00:00:00 -05:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -26,7 +25,6 @@ dependencies:
26
25
  requirements:
27
26
  - - ">="
28
27
  - !ruby/object:Gem::Version
29
- hash: 3
30
28
  segments:
31
29
  - 0
32
30
  version: "0"
@@ -45,7 +43,7 @@ extra_rdoc_files:
45
43
  files:
46
44
  - lib/delicious-cli.rb
47
45
  - lib/delicious-cli/api.rb
48
- - lib/delicious-cli/colorize.rb
46
+ - lib/delicious-cli/colored.rb
49
47
  - lib/delicious-cli/db.rb
50
48
  - lib/delicious-cli/display.rb
51
49
  - lib/delicious-cli/log.rb
@@ -61,8 +59,8 @@ homepage: http://github.com/epitron/delicious-cli
61
59
  licenses: []
62
60
 
63
61
  post_install_message:
64
- rdoc_options:
65
- - --charset=UTF-8
62
+ rdoc_options: []
63
+
66
64
  require_paths:
67
65
  - lib
68
66
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -70,7 +68,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
68
  requirements:
71
69
  - - ">="
72
70
  - !ruby/object:Gem::Version
73
- hash: 3
74
71
  segments:
75
72
  - 0
76
73
  version: "0"
@@ -79,7 +76,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
76
  requirements:
80
77
  - - ">="
81
78
  - !ruby/object:Gem::Version
82
- hash: 3
83
79
  segments:
84
80
  - 0
85
81
  version: "0"
@@ -1,275 +0,0 @@
1
- #
2
- # Colorize String class extension.
3
- #
4
- class String
5
-
6
- #
7
- # Colors Hash
8
- #
9
- COLORS = {
10
- :black => 0,
11
- :red => 1,
12
- :green => 2,
13
- :yellow => 3,
14
- :blue => 4,
15
- :magenta => 5,
16
- :cyan => 6,
17
- :white => 7,
18
- :default => 9,
19
-
20
- :light_black => 10,
21
- :light_red => 11,
22
- :light_green => 12,
23
- :light_yellow => 13,
24
- :light_blue => 14,
25
- :light_magenta => 15,
26
- :light_cyan => 16,
27
- :light_white => 17
28
- }
29
-
30
- #
31
- # Modes Hash
32
- #
33
- MODES = {
34
- :default => 0, # Turn off all attributes
35
- #:bright => 1, # Set bright mode
36
- :underline => 4, # Set underline mode
37
- :blink => 5, # Set blink mode
38
- :swap => 7, # Exchange foreground and background colors
39
- :hide => 8 # Hide text (foreground color would be the same as background)
40
- }
41
-
42
- protected
43
-
44
- #
45
- # Set color values in new string intance
46
- #
47
- def set_color_parameters( params )
48
- if (params.instance_of?(Hash))
49
- @color = params[:color]
50
- @background = params[:background]
51
- @mode = params[:mode]
52
- @uncolorized = params[:uncolorized]
53
- self
54
- else
55
- nil
56
- end
57
- end
58
-
59
- public
60
-
61
- #
62
- # Change color of string
63
- #
64
- # Examples:
65
- #
66
- # puts "This is blue".colorize( :blue )
67
- # puts "This is light blue".colorize( :light_blue )
68
- # puts "This is also blue".colorize( :color => :blue )
69
- # puts "This is blue with red background".colorize( :color => :light_blue, :background => :red )
70
- # puts "This is blue with red background".colorize( :light_blue ).colorize( :background => :red )
71
- # puts "This is blue text on red".blue.on_red
72
- # puts "This is red on blue".colorize( :red ).on_blue
73
- # puts "This is red on blue and underline".colorize( :red ).on_blue.underline
74
- # puts "This is blue text on red".blue.on_red.blink
75
- #
76
- def colorize( params=nil )
77
-
78
- return self.tagged_colors unless params
79
- return self unless STDOUT.isatty
80
-
81
- begin
82
- require 'Win32/Console/ANSI' if PLATFORM =~ /win32/
83
- rescue LoadError
84
- raise 'You must gem install win32console to use color on Windows'
85
- end
86
-
87
-
88
- color_parameters = {}
89
-
90
- if (params.instance_of?(Hash))
91
- color_parameters[:color] = COLORS[params[:color]]
92
- color_parameters[:background] = COLORS[params[:background]]
93
- color_parameters[:mode] = MODES[params[:mode]]
94
- elsif (params.instance_of?(Symbol))
95
- color_parameters[:color] = COLORS[params]
96
- end
97
-
98
- color_parameters[:color] ||= @color || 9
99
- color_parameters[:background] ||= @background || 9
100
- color_parameters[:mode] ||= @mode || 0
101
-
102
- color_parameters[:uncolorized] ||= @uncolorized || self.dup
103
-
104
- # calculate bright mode
105
- color_parameters[:color] += 50 if color_parameters[:color] > 10
106
-
107
- color_parameters[:background] += 50 if color_parameters[:background] > 10
108
-
109
- return "\033[#{color_parameters[:mode]};#{color_parameters[:color]+30};#{color_parameters[:background]+40}m#{color_parameters[:uncolorized]}\033[0m".set_color_parameters( color_parameters )
110
- end
111
-
112
-
113
- #
114
- # Return uncolorized string
115
- #
116
- def uncolorize
117
- return @uncolorized || self
118
- end
119
-
120
- #
121
- # Return true if sting is colorized
122
- #
123
- def colorized?
124
- return !@uncolorized.nil?
125
- end
126
-
127
- #
128
- # Make some color and on_color methods
129
- #
130
- COLORS.each_key do | key |
131
- eval <<-"end_eval"
132
- def #{key.to_s}
133
- return self.colorize( :color => :#{key.to_s} )
134
- end
135
- def on_#{key.to_s}
136
- return self.colorize( :background => :#{key.to_s} )
137
- end
138
- end_eval
139
- end
140
-
141
- #
142
- # Methods for modes
143
- #
144
- MODES.each_key do | key |
145
- eval <<-"end_eval"
146
- def #{key.to_s}
147
- return self.colorize( :mode => :#{key.to_s} )
148
- end
149
- end_eval
150
- end
151
-
152
- class << self
153
-
154
- #
155
- # Return array of available modes used by colorize method
156
- #
157
- def modes
158
- keys = []
159
- MODES.each_key do | key |
160
- keys << key
161
- end
162
- keys
163
- end
164
-
165
- #
166
- # Return array of available colors used by colorize method
167
- #
168
- def colors
169
- keys = []
170
- COLORS.each_key do | key |
171
- keys << key
172
- end
173
- keys
174
- end
175
-
176
- #
177
- # Display color matrix with color names.
178
- #
179
- def color_matrix( txt = "[X]" )
180
- size = String.colors.length
181
- String.colors.each do | color |
182
- String.colors.each do | back |
183
- print txt.colorize( :color => color, :background => back )
184
- end
185
- puts " < #{color}"
186
- end
187
- String.colors.reverse.each_with_index do | back, index |
188
- puts "#{"|".rjust(txt.length)*(size-index)} < #{back}"
189
- end
190
- end
191
- end
192
- puts
193
-
194
- #
195
- # BBS-style numeric color codes.
196
- #
197
- BBS_COLOR_TABLE = {
198
- 0 => :black,
199
- 1 => :blue,
200
- 2 => :green,
201
- 3 => :cyan,
202
- 4 => :red,
203
- 5 => :magenta,
204
- 6 => :yellow,
205
- 7 => :white,
206
- 8 => :light_black,
207
- 9 => :light_blue,
208
- 10 => :light_green,
209
- 11 => :light_cyan,
210
- 12 => :light_red,
211
- 13 => :light_magenta,
212
- 14 => :light_yellow,
213
- 15 => :light_white,
214
- }
215
-
216
- #
217
- def valid_color?(string)
218
- COLORS.include?(string.to_sym) or (string =~ /^\d+$/ and BBS_COLOR_TABLE.include?(string.to_i))
219
- end
220
-
221
- #
222
- # Colorize a string that has "color tags".
223
- #
224
- # Examples:
225
- #
226
- # Colors as words:
227
- # puts "<light_yellow><light_white>*</light_white> Hey mom! I am <light_green>SO</light_green> colourized right now.</light_yellow>".colorize
228
- #
229
- # Numeric ANSI colors (from the BBS days):
230
- # puts "<10><5>*</5> Hey mom! I am <9>SO</9> colourized right now.</10>".colorize
231
- #
232
- def tagged_colors
233
- stack = []
234
-
235
- # matchers for just the color part of the tag
236
- open_tag_re = /<([\w\d_]+)>/
237
- close_tag_re = /<\/([\w\d_]+)>/
238
-
239
- # split the string into tags and texts
240
- tokens = self.split(/(<\/?[\w\d_]+>)/)
241
- tokens.delete_if { |token| token.size == 0 }
242
-
243
- result = ""
244
-
245
- tokens.each do |token|
246
-
247
- if open_tag_re =~ token and valid_color?($1)
248
-
249
- stack.push $1
250
-
251
- elsif close_tag_re =~ token and valid_color?($1)
252
-
253
- # if this color is on the stack somwehere...
254
- if pos = stack.rindex($1)
255
- # close the tag by removing it from the stack
256
- stack.delete_at pos
257
- else
258
- raise "Error: tried to close an unopened color tag -- #{token}"
259
- end
260
-
261
- else
262
-
263
- color = (stack.last || "white")
264
- color = BBS_COLOR_TABLE[color.to_i] if color =~ /^\d+$/
265
- result << token.colorize(color.to_sym)
266
-
267
- end
268
-
269
- end
270
-
271
- result
272
- end
273
-
274
- end
275
-