class Bookmarks
Constants
- BOOKMARKS_PATH
Public Class Methods
is_valid_name?(bookmark)
click to toggle source
Checks if bookmark
name is valid
# File lib/bookmarks.rb 40 def self.is_valid_name? bookmark 41 return (bookmark =~/\A\W/).nil? 42 end
new()
click to toggle source
# File lib/bookmarks.rb 28 def initialize 29 # Loads the bookmarks list from the bookmarks file. 30 begin 31 @bookmarks = YAML::load_file(BOOKMARKS_PATH) 32 rescue Errno::ENOENT 33 @bookmarks = {} 34 rescue 35 raise "Can't load configuration file" 36 end 37 end
Public Instance Methods
add(path, bookmark)
click to toggle source
Adds bookmark
pointing to path
# File lib/bookmarks.rb 56 def add path, bookmark 57 @bookmarks[bookmark] = path 58 end
complete(prefix)
click to toggle source
Provide a list of completion options, starting with given prefix
# File lib/bookmarks.rb 90 def complete prefix 91 # Special cases: 92 # - nothing is provided: return all the bookmarks 93 # - absolute path: don't complete 94 return sorted_bookmarks if prefix.nil? || prefix.empty? 95 return prefix if prefix.start_with? File::SEPARATOR 96 97 bookmark, path = prefix.split File::SEPARATOR, 2 # File.split only does path/basename 98 99 completions = [ ] 100 if path.nil? 101 # still in 1st element, could match several bookmarks 102 completions += @bookmarks.keys.find_all { |b| b.start_with? prefix } 103 elsif @bookmarks.has_key?(bookmark) 104 # bookmark known, complete further 105 completions += Dir.chdir(@bookmarks[bookmark]) do 106 Dir.glob(["#{path}*"]) \ 107 .select { |f| File.directory? f } \ 108 .collect { |f| File.join bookmark, f } 109 end 110 end 111 completions.map! { |d| d + File::SEPARATOR } 112 completions << prefix if completions.empty? 113 sorted_list completions 114 end
del(bookmark)
click to toggle source
Deletes bookmark
# File lib/bookmarks.rb 61 def del bookmark 62 if @bookmarks.has_key? bookmark 63 @bookmarks.delete bookmark 64 true 65 else 66 false 67 end 68 end
expand_path(path_with_bookmark)
click to toggle source
Expands paths that could start with a bookmark (e.g. [bookmark]/sub/path)
# File lib/bookmarks.rb 122 def expand_path(path_with_bookmark) 123 if path_with_bookmark.index("/").nil? 124 # the path is just a bookmark 125 return @bookmarks[path_with_bookmark] 126 elsif path_with_bookmark.index("/") == 0 127 # the path is an absolute path (no bookmark, e.g. /absolute/path) 128 return path_with_bookmark 129 else 130 # the path is composed of a bookmark and a subpath 131 name = path_with_bookmark[0, path_with_bookmark.index('/')] 132 path = path_with_bookmark[path_with_bookmark.index('/')+1, 133 path_with_bookmark.size] 134 return @bookmarks[name] + "/" + path; 135 end 136 end
save()
click to toggle source
Saves the bookmarks list in the bookmarks file.
# File lib/bookmarks.rb 45 def save 46 begin 47 File.open(BOOKMARKS_PATH, 'w') do |file| 48 file << YAML::dump(@bookmarks) 49 end 50 rescue 51 raise "Can't save configuration file" 52 end 53 end
simplify_path(path)
click to toggle source
Simplifies given path by replacing the user's homedir with ~
# File lib/bookmarks.rb 117 def simplify_path(path) 118 path.gsub /^#{File.expand_path '~'}/, '~' 119 end
sorted_bookmarks()
click to toggle source
# File lib/bookmarks.rb 86 def sorted_bookmarks() sorted_list @bookmarks.keys end
sorted_list(terms)
click to toggle source
# File lib/bookmarks.rb 87 def sorted_list(terms) terms.sort.join ' ' end
to_s()
click to toggle source
Prints the bookmarks list.
# File lib/bookmarks.rb 71 def to_s 72 if @bookmarks.empty? 73 "No bookmarks saved" 74 else 75 bookmarks_table = table do |t| 76 t.style = { :border_y => '', :border_i => '' } 77 t.headings = "Bookmark", "Path" 78 @bookmarks.keys.sort.each do |bookmark| 79 t << [bookmark, simplify_path(@bookmarks[bookmark])] 80 end 81 end 82 bookmarks_table.to_s 83 end 84 end