Top Level Namespace

Defined Under Namespace

Modules: Colored, Enumerable Classes: Database, Delicious, FileNotFound, Float, Integer, NilClass, Settings, String, Symbol

Constant Summary collapse

SCREEN_WIDTH =
term_width
HOMEDIR =
File.expand_path("~")
CONFIGDIR =
File.join(HOMEDIR, ".delicious")

Instance Method Summary collapse

Instance Method Details

#config(first_time = false) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/delicious-cli.rb', line 39

def config(first_time = false)
  # prompt user and stuff
  puts "Delicious login info"
  puts "---------------------------"
  puts
  print "Username: "
  username = gets.strip
  print "Password: "
  password = gets.strip
  
  puts
  puts "User: #{username.inspect} | Pass: #{password.inspect}"
  print "Is this correct? [y/N] "
  answer = gets.strip
  if answer =~ /^[Yy]$/
    puts 
    
    puts "* Checking that login/password works..."
    Delicious.basic_auth(username, password)
    if Delicious.valid_auth?    
      puts "  |_ Login successful! Saving config..."
      Settings["username"] = username
      Settings["password"] = password
      Settings.save!

      sync if first_time
      
      puts "* Everything is ready. Search away!" 
    else
      puts "  |_ Login failed! (Please check your credentials or network.)"
    end
    puts
  else
    puts "Aborting..."
  end
end

#configfile(filename, check = false) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/delicious-cli/settings.rb', line 16

def configfile(filename, check=false)
  path = File.join(CONFIGDIR, filename)
  
  if check and not File.exists? path
    raise FileNotFound, path
  end
  
  path
end

#display(post, query = nil, indent_size = 11) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/delicious-cli/display.rb', line 94

def display(post, query=nil, indent_size=11)
  indent    = " " * indent_size
  wrap_size = SCREEN_WIDTH - indent_size

  date         = formatdate(post["time"], indent_size)
  desc_lines   = post["description"].wrap(wrap_size).map { |line| line.hilite(query, :light_white) }
  url          = post["href"].hilite(query, :light_blue)
  tag_lines    = post["tag"].wrap(wrap_size-2).map { |line| line.hilite(query, :light_cyan) }

  if post["extended"].blank?
    ext_lines = []
  else
    ext_lines = post["extended"].wrap(wrap_size).map { |line| line.hilite(query, :white) }
  end
  
  # date / description
  puts date + desc_lines.first
  if desc_lines.size > 1
    desc_lines[1..-1].each { |line| puts indent + line }
  end
  
  # extended description
  ext_lines.each do |line|
    puts indent + line
  end  
  
  # url
  puts indent + url
  
  # tags
  print indent + "(".cyan + tag_lines.first
  if tag_lines.size > 1
    tag_lines[1..-1].each do |line|
      print "\n" + indent + " " + line
    end
  end
  puts ")".cyan
  
  puts
end

#formatdate(dt, width = 11) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/delicious-cli/display.rb', line 84

def formatdate(dt, width=11)
  time = "%l:%M%P"
  date = "%d %b %g"
  #dt.strftime("#{date} #{time}")
  result = dt.strftime(date)
  
  result.ljust(width).light_yellow
end

#mainObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/delicious-cli.rb', line 79

def main

  # In it!
  
  Database.init!
  begin
    Settings.load!
  rescue Errno::ENOENT
    config(true)
    retry
  end
  
  Delicious.basic_auth(Settings["username"], Settings["password"])

  # Parse de opts
  
  ARGV.push("--help") if ARGV.empty?
  
  options = OpenStruct.new
  OptionParser.new do |opts|
    opts.banner = "Usage: delicious [options] <search query>"
    opts.separator " "
    opts.separator "Specific options:"
    
    opts.on("-c", "--config", "(Re)configure login info (set your delicious username/password)") do |opt|
      options.config = true
    end

    opts.on("-t", "--test-auth", "Test that authentication info works") do |opt|
      options.test_auth = true 
    end

    opts.on("-s", "--sync", "Synchronize links") do |opt|
      options.sync = true
    end
  
    opts.on("-r", "--redownload", "Erase database and redownload all links") do |opt|
      options.redownload = true
    end

    opts.on("-a", "--all", "Show all links.") do |opt|
      options.list = 0
    end

    opts.on("-l", "--list [num]", "List the last [num] links (default 5, 0 for all)") do |opt|
      if opt
        options.list = opt.to_i
      else
        options.list = 5
      end
    end

    opts.on("-j", "--json", "Output results as (pretty) JSON.") do |opt|
      options.json = true
    end
  
    opts.on("-d", "--debug", "Debug info") do |opt|
      options.debug = true
    end
  

    opts.separator " "
    opts.separator "Common options:"

    # No argument, shows at tail.  This will print an options summary.
    # Try it and see!
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      puts
      exit
    end

    opts.on("-v", "--version", "Version information") do |opt|
      versionfile = File.join(File.dirname(File.expand_path(__FILE__)), "..", "VERSION")
      versionstring = open(versionfile).read
      puts "delicious-cli v#{versionstring}"
      puts
    end
    
  end.parse!
  
  
  # Handle options

  $log.level = Logger::DEBUG if options.debug
   
  if options.test_auth
    puts "Logging in as '#{Settings["username"]}': #{Delicious.valid_auth? ? "success!" : "fail!"}"
  elsif options.config 
    config
  elsif options.redownload
    redownload
  elsif options.sync
    sync
  else
    
    # get posts
    if options.list
      posts = Database.last(options.list)
    else
      words = ARGV
      exit 1 unless words.size > 0
      posts = Database.find(words)
    end
    
    
    if options.json
      require 'json'
      puts JSON.pretty_generate(posts)
    else
      if words
        posts.each { |match| display(match, words) }
      else
        posts.each { |post| display(post) }
      end
    end
    
  end
  
end

#printflush(string) ⇒ Object



22
23
24
25
# File 'lib/delicious-cli/db.rb', line 22

def printflush(string)
  print string
  STDOUT.flush
end

#redownloadObject



33
34
35
36
37
# File 'lib/delicious-cli.rb', line 33

def redownload
  puts "* Redownloading database..."
  Database.clear!
  Database.sync
end

#syncObject



28
29
30
31
# File 'lib/delicious-cli.rb', line 28

def sync
  puts "* Synchronizing database..."
  Database.sync
end

#term_widthObject



36
37
38
39
40
41
42
# File 'lib/delicious-cli/display.rb', line 36

def term_width
  if `stty size` =~ /(\d+) (\d+)/
    return $2.to_i
  else
    return 80
  end
end