epitron-delicious-cli 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/delicious-cli.rb CHANGED
@@ -29,6 +29,11 @@ def search(words)
29
29
  matches.each { |match| display(match, words) }
30
30
  end
31
31
 
32
+ def last(n)
33
+ posts = Database.last(n)
34
+ posts.each { |post| display(post) }
35
+ end
36
+
32
37
  def sync
33
38
  puts "* Synchronizing database..."
34
39
  Database.sync
@@ -112,6 +117,14 @@ def main
112
117
  options.redownload = true
113
118
  end
114
119
 
120
+ opts.on("-l", "--list [num]", "List the last [num] links (default 5, 0 for all)") do |opt|
121
+ if opt
122
+ options.list = opt.to_i
123
+ else
124
+ options.list = 5
125
+ end
126
+ end
127
+
115
128
  opts.on("-c", "--config", "Configure login info (set delicious username/password)") do |opt|
116
129
  options.config = true
117
130
  end
@@ -158,6 +171,8 @@ def main
158
171
  redownload
159
172
  elsif options.sync
160
173
  sync
174
+ elsif options.list
175
+ last(options.list)
161
176
  else
162
177
  exit 1 unless ARGV.size > 0
163
178
  search(ARGV)
@@ -93,6 +93,10 @@ class Database
93
93
  @@posts << params
94
94
  end
95
95
 
96
+ def self.last(n)
97
+ @@posts[-n..-1]
98
+ end
99
+
96
100
  def self.find(words)
97
101
 
98
102
  finders = words.map{|word| /#{word}/i }
@@ -1,12 +1,18 @@
1
1
  require 'date'
2
+ require 'open3'
2
3
 
3
4
  #################################################################
4
5
  ## Load the colorize gem, and define the "hilite" function
5
6
  begin
7
+
6
8
  require 'delicious-cli/colorize'
9
+
7
10
  # Colourized hilite...
8
11
  class String
12
+
9
13
  def hilite(words, color=:white)
14
+ return self.send(color) if words.nil?
15
+
10
16
  escaped_words = words.map { |word| Regexp.escape(word) }
11
17
  matcher = /(#{escaped_words.join('|')})/io
12
18
 
@@ -19,15 +25,68 @@ begin
19
25
  end
20
26
  end.join('')
21
27
  end
28
+
22
29
  end
30
+
23
31
  rescue LoadError
32
+
24
33
  STDERR.puts "Note: You should install the 'colorize' gem for extra prettiness.\n"
25
34
  # Monochrome hilite does nothing...
26
35
  class String
27
36
  def hilite(words); self; end
28
37
  end
38
+
39
+ end
40
+
41
+
42
+ #################################################################
43
+
44
+ def term_width
45
+ if `stty size` =~ /(\d+) (\d+)/
46
+ return $2.to_i
47
+ else
48
+ return 80
49
+ end
29
50
  end
30
51
 
52
+ SCREEN_WIDTH = term_width
53
+
54
+ #################################################################
55
+
56
+ class String
57
+
58
+ def wrap(width=80, chop=true)
59
+
60
+ if (lines = self.split("\n")).size > 1
61
+ return lines.map { |line| line.wrap(width, chop) }.flatten
62
+ end
63
+
64
+ lines = []
65
+ left = 0
66
+
67
+ loop do
68
+ edge = left + width
69
+ right = self.rindex(/\s+|$/, edge)
70
+
71
+ if right.nil? or right < left
72
+ if chop
73
+ right = edge
74
+ else
75
+ right = self.index(/\s+|$/, edge)
76
+ end
77
+ end
78
+
79
+ line = self[left...right]
80
+ lines << line.strip
81
+ left = self.index(/[^\s]|$/, right)
82
+ break if right >= (self.size-1)
83
+ end
84
+
85
+ lines
86
+ end
87
+
88
+ end
89
+
31
90
  #################################################################
32
91
 
33
92
  def formatdate(dt, width=11)
@@ -40,18 +99,43 @@ def formatdate(dt, width=11)
40
99
  end
41
100
 
42
101
 
43
- def display(post, query, indent_size=11)
44
- indent = " " * indent_size
102
+ def display(post, query=nil, indent_size=11)
103
+ indent = " " * indent_size
104
+ wrap_size = SCREEN_WIDTH - indent_size
45
105
 
46
- date = formatdate(post["time"], indent_size)
47
- desc = post["description"].hilite(query, :light_white)
48
- ext = post["extended"].hilite(query, :white)
49
- url = post["href"].hilite(query, :light_blue)
50
- tags = post["tag"].hilite(query, :light_cyan)
106
+ date = formatdate(post["time"], indent_size)
107
+ desc_lines = post["description"].wrap(wrap_size).map { |line| line.hilite(query, :light_white) }
108
+ url = post["href"].hilite(query, :light_blue)
109
+ tag_lines = post["tag"].wrap(wrap_size-2).map { |line| line.hilite(query, :light_cyan) }
51
110
 
52
- puts date + desc
53
- puts indent + ext if post["extended"].any?
111
+ if post["extended"].any?
112
+ ext_lines = post["extended"].wrap(wrap_size).map { |line| line.hilite(query, :white) }
113
+ else
114
+ ext_lines = []
115
+ end
116
+
117
+ # date / description
118
+ puts date + desc_lines.first
119
+ if desc_lines.size > 1
120
+ desc_lines[1..-1].each { |line| puts indent + line }
121
+ end
122
+
123
+ # extended description
124
+ ext_lines.each do |line|
125
+ puts indent + line
126
+ end
127
+
128
+ # url
54
129
  puts indent + url
55
- puts indent + "(".cyan + tags + ")".cyan
130
+
131
+ # tags
132
+ print indent + "(".cyan + tag_lines.first
133
+ if tag_lines.size > 1
134
+ tag_lines[1..-1].each do |line|
135
+ print "\n" + indent + " " + line
136
+ end
137
+ end
138
+ puts ")".cyan
139
+
56
140
  puts
57
141
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epitron-delicious-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
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-18 00:00:00 -07:00
12
+ date: 2009-09-22 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency