delicious-cli 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,275 @@
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
+
@@ -19,6 +19,13 @@ Sample record:
19
19
 
20
20
  #################################################################
21
21
 
22
+ def printflush(string)
23
+ print string
24
+ STDOUT.flush
25
+ end
26
+
27
+ #################################################################
28
+
22
29
  class Database
23
30
 
24
31
  @@filename = configfile('delicious.marshal')
@@ -49,12 +56,10 @@ class Database
49
56
  all = true if @@posts.empty?
50
57
 
51
58
  if all
52
- print " |_ Retrieving all links..."
53
- STDOUT.flush
59
+ printflush " |_ Retrieving all links..."
54
60
  posts = Delicious.posts_all
55
61
  else
56
- print " |_ Retrieving new links..."
57
- STDOUT.flush
62
+ printflush " |_ Retrieving new links..."
58
63
  posts = Delicious.posts_since(most_recent_time)
59
64
  end
60
65
 
@@ -67,12 +72,12 @@ class Database
67
72
  return
68
73
  end
69
74
 
70
- print " |_ Processing links..."
75
+ printflush " |_ Processing links..."
71
76
  posts.each { |post| post["time_string"] = post["time"]; post["time"] = DateTime.parse(post["time_string"]) }
72
77
  @@posts += posts.sort_by { |post| post["time"] }
73
78
  puts "done!"
74
79
 
75
- print " |_ Saving database..."
80
+ printflush " |_ Saving database..."
76
81
  save!
77
82
 
78
83
  puts "done!"
@@ -1,11 +1,9 @@
1
1
  require 'date'
2
- require 'colorize'
3
2
 
4
3
  #################################################################
5
4
  ## Load the colorize gem, and define the "hilite" function
6
5
  begin
7
- require 'rubygems'
8
- require 'colorize'
6
+ require 'delicious-cli/colorize'
9
7
  # Colourized hilite...
10
8
  class String
11
9
  def hilite(words, color=:white)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delicious-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - epitron
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-10 00:00:00 -04:00
12
+ date: 2009-09-18 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -22,16 +22,6 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: "0"
24
24
  version:
25
- - !ruby/object:Gem::Dependency
26
- name: colorize
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: "0"
34
- version:
35
25
  description: A commandline tool which lets you download all your delicious.com links and search them (with pretty color-coded results).
36
26
  email: chris@ill-logic.com
37
27
  executables:
@@ -45,6 +35,7 @@ extra_rdoc_files:
45
35
  files:
46
36
  - lib/delicious-cli.rb
47
37
  - lib/delicious-cli/api.rb
38
+ - lib/delicious-cli/colorize.rb
48
39
  - lib/delicious-cli/db.rb
49
40
  - lib/delicious-cli/display.rb
50
41
  - lib/delicious-cli/log.rb