Module: Rfd::Bookmark

Defined in:
lib/rfd/bookmark.rb

Constant Summary collapse

CONFIG_DIR =
File.join(ENV.fetch('XDG_CONFIG_HOME') { File.expand_path('~/.config') }, 'rfd')
BOOKMARK_FILE =
File.join(CONFIG_DIR, 'bookmarks')

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.bookmarksObject

Returns the value of attribute bookmarks.



11
12
13
# File 'lib/rfd/bookmark.rb', line 11

def bookmarks
  @bookmarks
end

Class Method Details

.add(path) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/rfd/bookmark.rb', line 13

def add(path)
  path = File.expand_path(path)
  return if @bookmarks.include?(path)

  @bookmarks << path
  save
end

.include?(path) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/rfd/bookmark.rb', line 27

def include?(path)
  @bookmarks.include?(File.expand_path(path))
end

.loadObject



39
40
41
42
43
44
45
46
47
# File 'lib/rfd/bookmark.rb', line 39

def load
  return unless File.exist?(BOOKMARK_FILE)

  @bookmarks = File.readlines(BOOKMARK_FILE, chomp: true)
    .map { |line| File.expand_path(line) }
    .select { |path| File.directory?(path) }
rescue Errno::EACCES, Errno::ENOENT
  @bookmarks = []
end

.remove(path) ⇒ Object



21
22
23
24
25
# File 'lib/rfd/bookmark.rb', line 21

def remove(path)
  path = File.expand_path(path)
  @bookmarks.delete(path)
  save
end

.saveObject



49
50
51
52
53
54
55
# File 'lib/rfd/bookmark.rb', line 49

def save
  dir = File.dirname(BOOKMARK_FILE)
  FileUtils.mkdir_p(dir) unless File.directory?(dir)
  File.write(BOOKMARK_FILE, @bookmarks.join("\n") + "\n")
rescue Errno::EACCES, Errno::ENOENT
  # Silently fail if we can't write
end

.toggle(path) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/rfd/bookmark.rb', line 31

def toggle(path)
  if include?(path)
    remove(path)
  else
    add(path)
  end
end