Class: Bookmarks

Inherits:
Object
  • Object
show all
Defined in:
lib/bookmarks.rb

Constant Summary collapse

BOOKMARKS_PATH =
File.expand_path "~/.jumprc"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBookmarks

Returns a new instance of Bookmarks.



28
29
30
31
32
33
34
35
36
37
# File 'lib/bookmarks.rb', line 28

def initialize
  # Loads the bookmarks list from the bookmarks file.
  begin
    @bookmarks = YAML::load_file(BOOKMARKS_PATH)
  rescue Errno::ENOENT
    @bookmarks = {}
  rescue
    raise "Can't load configuration file"
  end
end

Class Method Details

.is_valid_name?(bookmark) ⇒ Boolean

Checks if bookmark name is valid

Returns:

  • (Boolean)


40
41
42
# File 'lib/bookmarks.rb', line 40

def self.is_valid_name? bookmark
  return (bookmark =~/\A\W/).nil?
end

Instance Method Details

#add(path, bookmark) ⇒ Object

Adds bookmark pointing to path



56
57
58
# File 'lib/bookmarks.rb', line 56

def add path, bookmark
  @bookmarks[bookmark] = path
end

#complete(prefix) ⇒ Object

Provide a list of completion options, starting with given prefix



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
# File 'lib/bookmarks.rb', line 90

def complete prefix
  # Special cases:
  #   - nothing is provided: return all the bookmarks
  #   - absolute path: don't complete
  return sorted_bookmarks if prefix.nil? || prefix.empty?
  return prefix if prefix.start_with? File::SEPARATOR

  bookmark, path = prefix.split File::SEPARATOR, 2 # File.split only does path/basename

  completions = [  ]
  if path.nil?
    # still in 1st element, could match several bookmarks
    completions += @bookmarks.keys.find_all { |b| b.start_with? prefix }
  elsif @bookmarks.has_key?(bookmark)
    # bookmark known, complete further
    completions += Dir.chdir(@bookmarks[bookmark]) do
      Dir.glob(["#{path}*"]) \
        .select { |f| File.directory? f } \
        .collect { |f| File.join bookmark, f }
    end
  end
  completions.map! { |d| d + File::SEPARATOR }
  completions << prefix if completions.empty?
  sorted_list completions
end

#del(bookmark) ⇒ Object

Deletes bookmark



61
62
63
64
65
66
67
68
# File 'lib/bookmarks.rb', line 61

def del bookmark
  if @bookmarks.has_key? bookmark
    @bookmarks.delete bookmark
    true
  else
    false
  end
end

#expand_path(path_with_bookmark) ⇒ Object

Expands paths that could start with a bookmark (e.g. [bookmark]/sub/path)



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/bookmarks.rb', line 122

def expand_path(path_with_bookmark)
  if path_with_bookmark.index("/").nil?
    # the path is just a bookmark
    return @bookmarks[path_with_bookmark]
  elsif path_with_bookmark.index("/") == 0
    # the path is an absolute path (no bookmark, e.g. /absolute/path)
    return path_with_bookmark
  else
    # the path is composed of a bookmark and a subpath
    name = path_with_bookmark[0, path_with_bookmark.index('/')]
    path = path_with_bookmark[path_with_bookmark.index('/')+1,
                              path_with_bookmark.size]
    return @bookmarks[name] + "/" + path;
  end
end

#saveObject

Saves the bookmarks list in the bookmarks file.



45
46
47
48
49
50
51
52
53
# File 'lib/bookmarks.rb', line 45

def save
  begin
    File.open(BOOKMARKS_PATH, 'w') do |file|
      file << YAML::dump(@bookmarks)
    end
  rescue
    raise "Can't save configuration file"
  end
end

#simplify_path(path) ⇒ Object

Simplifies given path by replacing the user’s homedir with ~



117
118
119
# File 'lib/bookmarks.rb', line 117

def simplify_path(path)
  path.gsub /^#{File.expand_path '~'}/, '~'
end

#sorted_bookmarksObject



86
# File 'lib/bookmarks.rb', line 86

def sorted_bookmarks()  sorted_list @bookmarks.keys  end

#sorted_list(terms) ⇒ Object



87
# File 'lib/bookmarks.rb', line 87

def sorted_list(terms)  terms.sort.join ' '  end

#to_sObject

Prints the bookmarks list.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bookmarks.rb', line 71

def to_s
  if @bookmarks.empty?
    "No bookmarks saved"
  else
    bookmarks_table = table do |t|
      t.style = { :border_y => '', :border_i => '' }
      t.headings = "Bookmark", "Path"
      @bookmarks.keys.sort.each do |bookmark|
        t << [bookmark, simplify_path(@bookmarks[bookmark])]
      end
    end
    bookmarks_table.to_s
  end
end