jump 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/COPYING.txt +674 -0
- data/POST_INSTALL.txt +60 -0
- data/README.rdoc +108 -0
- data/Rakefile +88 -0
- data/VERSION +1 -0
- data/bash_integration/bash_completion/jump +55 -0
- data/bash_integration/shell_driver +28 -0
- data/bin/jump-bin +90 -0
- data/lib/bookmarks.rb +146 -0
- data/test/bookmarks_test.rb +139 -0
- data/test/test_helper.rb +24 -0
- metadata +175 -0
data/POST_INSTALL.txt
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
It's strongly recommended to install bash_completion. In this way you will have
|
2
|
+
the best user experience.
|
3
|
+
Linux users can install it using their package manager (apt, zypper, yum, emerge...),
|
4
|
+
while OSX users can install it via ports, fink or brew.
|
5
|
+
|
6
|
+
Now that jump is installed you have to create a file called 'jump' containing the
|
7
|
+
following text:
|
8
|
+
|
9
|
+
_jump()
|
10
|
+
{
|
11
|
+
local cur prev opts
|
12
|
+
COMPREPLY=()
|
13
|
+
cur="${COMP_WORDS[COMP_CWORD]}"
|
14
|
+
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
15
|
+
opts="--help -h --add -a --del -d --list -l"
|
16
|
+
|
17
|
+
if [[ ${prev} == -d || ${prev} == --d* ]] ; then
|
18
|
+
# complete the del command with a list of the available bookmarks
|
19
|
+
local bookmarks=$(jump --bc)
|
20
|
+
COMPREPLY=( $(compgen -W "${bookmarks}" -- ${cur}) )
|
21
|
+
return 0
|
22
|
+
fi
|
23
|
+
|
24
|
+
if [[ ${cur:0:1} == "-" ]]; then
|
25
|
+
COMPREPLY="$(compgen -W "${opts}" -- ${cur}) "
|
26
|
+
return 0
|
27
|
+
else
|
28
|
+
local bookmarks=$(jump --bc ${cur})
|
29
|
+
COMPREPLY=( $(compgen -W "${bookmarks}" -- ${cur}) )
|
30
|
+
return 0
|
31
|
+
fi
|
32
|
+
}
|
33
|
+
|
34
|
+
function jump {
|
35
|
+
#echo "jump called with $*"
|
36
|
+
if [ ${1:0:1} == "-" ]; then
|
37
|
+
jump-bin $*
|
38
|
+
else
|
39
|
+
cd $(jump-bin $*)
|
40
|
+
fi
|
41
|
+
}
|
42
|
+
|
43
|
+
complete -o nospace -F _jump jump
|
44
|
+
|
45
|
+
|
46
|
+
The file must be located inside of the 'bash_completion.d' directory, which is
|
47
|
+
usually located under /etc/.
|
48
|
+
|
49
|
+
If you do not want to install 'bash_completion' then add the following code
|
50
|
+
to your bash configuration file (e.g. '~/.bash_profile' or '~/.bashrc'):
|
51
|
+
|
52
|
+
function jump {
|
53
|
+
if [ ${1:0:1} == "-" ]; then
|
54
|
+
jump-bin $*
|
55
|
+
else
|
56
|
+
cd $(jump-bin $*)
|
57
|
+
fi
|
58
|
+
}
|
59
|
+
|
60
|
+
Beware: without bash_completion you won't be able use jump's advanced completion features.
|
data/README.rdoc
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
= Jump, a bookmarking system for the bash shell.
|
2
|
+
|
3
|
+
== Introduction
|
4
|
+
|
5
|
+
Jump is a tool that allows you to quickly change directories in the bash
|
6
|
+
shell using bookmarks. Thanks to Jump, you won't have to type those long
|
7
|
+
paths anymore.
|
8
|
+
Jump was inspired by {go-tool}[http://code.google.com/p/go-tool/] by ActiveState.
|
9
|
+
|
10
|
+
== Usage
|
11
|
+
|
12
|
+
Say you often work in a directory with a path like
|
13
|
+
`/Users/giuseppe/Work/Projects/MyProject`. With Jump you can add a bookmark
|
14
|
+
to it:
|
15
|
+
|
16
|
+
$ jump --add myproject
|
17
|
+
|
18
|
+
From now on you can jump to your project just by typing:
|
19
|
+
|
20
|
+
$ jump myproject
|
21
|
+
|
22
|
+
You can even append subfolders to the bookmark name!
|
23
|
+
|
24
|
+
$ jump myproject/src/
|
25
|
+
|
26
|
+
You can take a look at all your bookmarks with the `list` command:
|
27
|
+
|
28
|
+
$ jump --list
|
29
|
+
|
30
|
+
To delete a bookmark you don't need anymore, use the `del` command:
|
31
|
+
|
32
|
+
$ jump --del myproject
|
33
|
+
|
34
|
+
Don't remember a command? Just type:
|
35
|
+
|
36
|
+
$ jump --help
|
37
|
+
|
38
|
+
== Installation
|
39
|
+
|
40
|
+
Jump is packaged as a gem:
|
41
|
+
|
42
|
+
$ gem install jump
|
43
|
+
|
44
|
+
== Bash integration
|
45
|
+
|
46
|
+
It's strongly recommended to install <i>bash_completion</i>. In this way you will have
|
47
|
+
the best user experience.
|
48
|
+
|
49
|
+
Linux users can install it using their package manager (apt, zypper, yum, emerge...)
|
50
|
+
while OSX users can install it via ports, fink or brew.
|
51
|
+
|
52
|
+
When jump is installed you have to create a file called <i>jump</i> containing the
|
53
|
+
following text:
|
54
|
+
|
55
|
+
_jump()
|
56
|
+
{
|
57
|
+
local cur prev opts
|
58
|
+
COMPREPLY=()
|
59
|
+
cur="${COMP_WORDS[COMP_CWORD]}"
|
60
|
+
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
61
|
+
opts="--help -h --add -a --del -d --list -l"
|
62
|
+
|
63
|
+
if [[ ${prev} == -d || ${prev} == --d* ]] ; then
|
64
|
+
# complete the del command with a list of the available bookmarks
|
65
|
+
local bookmarks=$(jump --bc)
|
66
|
+
COMPREPLY=( $(compgen -W "${bookmarks}" -- ${cur}) )
|
67
|
+
return 0
|
68
|
+
fi
|
69
|
+
|
70
|
+
if [[ ${cur:0:1} == "-" ]]; then
|
71
|
+
COMPREPLY="$(compgen -W "${opts}" -- ${cur}) "
|
72
|
+
return 0
|
73
|
+
else
|
74
|
+
local bookmarks=$(jump --bc ${cur})
|
75
|
+
COMPREPLY=( $(compgen -W "${bookmarks}" -- ${cur}) )
|
76
|
+
return 0
|
77
|
+
fi
|
78
|
+
}
|
79
|
+
|
80
|
+
function jump {
|
81
|
+
#echo "jump called with $*"
|
82
|
+
if [ ${1:0:1} == "-" ]; then
|
83
|
+
jump-bin $*
|
84
|
+
else
|
85
|
+
cd $(jump-bin $*)
|
86
|
+
fi
|
87
|
+
}
|
88
|
+
|
89
|
+
complete -o nospace -F _jump jump
|
90
|
+
|
91
|
+
|
92
|
+
The file must be located inside of the <i>bash_completion.d</i> directory, which
|
93
|
+
is usually located under <i>/etc/</i>.
|
94
|
+
|
95
|
+
If you do not want to install <i>bash_completion</i> then add the following code
|
96
|
+
to your bash configuration file (e.g. <i>~/.bash_profile</i> or <i>~/.bashrc</i>):
|
97
|
+
|
98
|
+
function jump {
|
99
|
+
if [ ${1:0:1} == "-" ]; then
|
100
|
+
jump-bin $*
|
101
|
+
else
|
102
|
+
cd $(jump-bin $*)
|
103
|
+
fi
|
104
|
+
}
|
105
|
+
|
106
|
+
<b>Beware:</b> without bash_completion you won't be able use jump's advanced completion features.
|
107
|
+
|
108
|
+
(c) 2010 Flavio Castelli and Giuseppe Capizzi
|
data/Rakefile
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# This file is part of the jump project
|
2
|
+
#
|
3
|
+
# Copyright (C) 2010 Flavio Castelli <flavio@castelli.name>
|
4
|
+
# Copyright (C) 2010 Giuseppe Capizzi <gcapizzi@gmail.com>
|
5
|
+
#
|
6
|
+
# kaveau is free software; you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU General Public License as published by
|
8
|
+
# the Free Software Foundation; either version 2 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
#
|
11
|
+
# kaveau is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU General Public License
|
17
|
+
# along with Keep; if not, write to the
|
18
|
+
# Free Software Foundation, Inc.,
|
19
|
+
# 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.
|
20
|
+
|
21
|
+
require 'rake'
|
22
|
+
require 'rake/rdoctask'
|
23
|
+
require 'rake/testtask'
|
24
|
+
|
25
|
+
task :default => "test"
|
26
|
+
|
27
|
+
namespace :test do
|
28
|
+
desc "Test all classes"
|
29
|
+
Rake::TestTask.new(:all) do |t|
|
30
|
+
t.libs << "test"
|
31
|
+
t.pattern = 'test/*_test.rb'
|
32
|
+
t.verbose = true
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "Run all the unit tests"
|
37
|
+
task :test do
|
38
|
+
Rake::Task["test:all"].invoke
|
39
|
+
end
|
40
|
+
|
41
|
+
desc 'Generate documentation.'
|
42
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
43
|
+
rdoc.rdoc_dir = 'rdoc'
|
44
|
+
rdoc.title = 'jump'
|
45
|
+
rdoc.options << '--line-numbers' << "--main" << "README.rdoc"
|
46
|
+
rdoc.rdoc_files.include('README.rdoc')
|
47
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
48
|
+
end
|
49
|
+
|
50
|
+
begin
|
51
|
+
require 'jeweler'
|
52
|
+
Jeweler::Tasks.new do |gem|
|
53
|
+
gem.name = %q{jump}
|
54
|
+
gem.summary = %q{A bookmarking system for the bash shell}
|
55
|
+
gem.description = %q{Jump is a tool that allows you to quickly change
|
56
|
+
directories in the bash shell using bookmarks.
|
57
|
+
Thanks to Jump, you won't have to type those long paths anymore.
|
58
|
+
|
59
|
+
Jump was inspired by go-tool by ActiveState
|
60
|
+
(http://code.google.com/p/go-tool/).}
|
61
|
+
|
62
|
+
gem.files = FileList['[A-Z]*', 'bash_integration/**/*', 'lib/**/*.rb', 'test/**/*.rb']
|
63
|
+
gem.require_path = 'lib'
|
64
|
+
gem.bindir = 'bin'
|
65
|
+
gem.executables = ['jump-bin']
|
66
|
+
gem.test_files = Dir[*['test/**/*_test.rb']]
|
67
|
+
|
68
|
+
gem.has_rdoc = true
|
69
|
+
gem.extra_rdoc_files = ["README.rdoc"]
|
70
|
+
gem.rdoc_options = ['--line-numbers', "--main", "README.rdoc"]
|
71
|
+
|
72
|
+
gem.authors = ["Flavio Castelli", "Giuseppe Capizzi"]
|
73
|
+
gem.email = %w(flavio@castelli.name gcapizzi@gmail.com)
|
74
|
+
gem.homepage = "http://github.com/flavio/jump"
|
75
|
+
|
76
|
+
gem.add_dependency "terminal-table"
|
77
|
+
gem.add_development_dependency "fakefs"
|
78
|
+
|
79
|
+
gem.platform = Gem::Platform::RUBY
|
80
|
+
gem.post_install_message = File.read('POST_INSTALL.txt')
|
81
|
+
end
|
82
|
+
Jeweler::GemcutterTasks.new
|
83
|
+
rescue LoadError
|
84
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
85
|
+
end
|
86
|
+
|
87
|
+
desc "Clean files generated by rake tasks"
|
88
|
+
task :clobber => [:clobber_rdoc]
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# This file is part of the jump project
|
2
|
+
#
|
3
|
+
# Copyright (C) 2010 Flavio Castelli <flavio@castelli.name>
|
4
|
+
# Copyright (C) 2010 Giuseppe Capizzi <gcapizzi@gmail.com>
|
5
|
+
#
|
6
|
+
# kaveau is free software; you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU General Public License as published by
|
8
|
+
# the Free Software Foundation; either version 2 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
#
|
11
|
+
# kaveau is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU General Public License
|
17
|
+
# along with Keep; if not, write to the
|
18
|
+
# Free Software Foundation, Inc.,
|
19
|
+
# 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.
|
20
|
+
|
21
|
+
_jump()
|
22
|
+
{
|
23
|
+
local cur prev opts
|
24
|
+
COMPREPLY=()
|
25
|
+
cur="${COMP_WORDS[COMP_CWORD]}"
|
26
|
+
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
27
|
+
opts="--help -h --add -a --del -d --list -l"
|
28
|
+
|
29
|
+
if [[ ${prev} == -d || ${prev} == --d* ]] ; then
|
30
|
+
# complete the del command with a list of the available bookmarks
|
31
|
+
local bookmarks=$(jump --bc)
|
32
|
+
COMPREPLY=( $(compgen -W "${bookmarks}" -- ${cur}) )
|
33
|
+
return 0
|
34
|
+
fi
|
35
|
+
|
36
|
+
if [[ ${cur:0:1} == "-" ]]; then
|
37
|
+
COMPREPLY="$(compgen -W "${opts}" -- ${cur}) "
|
38
|
+
return 0
|
39
|
+
else
|
40
|
+
local bookmarks=$(jump --bc ${cur})
|
41
|
+
COMPREPLY=( $(compgen -W "${bookmarks}" -- ${cur}) )
|
42
|
+
return 0
|
43
|
+
fi
|
44
|
+
}
|
45
|
+
|
46
|
+
function jump {
|
47
|
+
#echo "jump called with $*"
|
48
|
+
if [ ${1:0:1} == "-" ]; then
|
49
|
+
jump-bin $*
|
50
|
+
else
|
51
|
+
cd $(jump-bin $*)
|
52
|
+
fi
|
53
|
+
}
|
54
|
+
|
55
|
+
complete -o nospace -F _jump jump
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# This file is part of the jump project
|
2
|
+
#
|
3
|
+
# Copyright (C) 2010 Flavio Castelli <flavio@castelli.name>
|
4
|
+
# Copyright (C) 2010 Giuseppe Capizzi <gcapizzi@gmail.com>
|
5
|
+
#
|
6
|
+
# kaveau is free software; you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU General Public License as published by
|
8
|
+
# the Free Software Foundation; either version 2 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
#
|
11
|
+
# kaveau is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU General Public License
|
17
|
+
# along with Keep; if not, write to the
|
18
|
+
# Free Software Foundation, Inc.,
|
19
|
+
# 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.
|
20
|
+
|
21
|
+
function jump {
|
22
|
+
#echo "jump called with $*"
|
23
|
+
if [ ${1:0:1} == "-" ]; then
|
24
|
+
jump-bin $*
|
25
|
+
else
|
26
|
+
cd $(jump-bin $*)
|
27
|
+
fi
|
28
|
+
}
|
data/bin/jump-bin
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# This file is part of the jump project
|
4
|
+
#
|
5
|
+
# Copyright (C) 2010 Flavio Castelli <flavio@castelli.name>
|
6
|
+
# Copyright (C) 2010 Giuseppe Capizzi <gcapizzi@gmail.com>
|
7
|
+
#
|
8
|
+
# kaveau is free software; you can redistribute it and/or modify
|
9
|
+
# it under the terms of the GNU General Public License as published by
|
10
|
+
# the Free Software Foundation; either version 2 of the License, or
|
11
|
+
# (at your option) any later version.
|
12
|
+
#
|
13
|
+
# kaveau is distributed in the hope that it will be useful,
|
14
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
+
# GNU General Public License for more details.
|
17
|
+
#
|
18
|
+
# You should have received a copy of the GNU General Public License
|
19
|
+
# along with Keep; if not, write to the
|
20
|
+
# Free Software Foundation, Inc.,
|
21
|
+
# 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.
|
22
|
+
|
23
|
+
THIS_FILE = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__
|
24
|
+
|
25
|
+
require "#{File.dirname(THIS_FILE)}/../lib/bookmarks"
|
26
|
+
|
27
|
+
options = {}
|
28
|
+
begin
|
29
|
+
OptionParser.new do |opts|
|
30
|
+
opts.banner = "Usage: example.rb [options]"
|
31
|
+
|
32
|
+
opts.on("-a", "--add BOOKMARK", "Saves the current directory in BOOKMARK") do |v|
|
33
|
+
options[:add] = v
|
34
|
+
end
|
35
|
+
|
36
|
+
opts.on("-d", "--del BOOKMARK", "Deletes BOOKMARK") do |v|
|
37
|
+
options[:del] = v
|
38
|
+
end
|
39
|
+
|
40
|
+
opts.on("-l", "--list", "Prints the list of all saved bookmarks") do |v|
|
41
|
+
options[:list] = true
|
42
|
+
end
|
43
|
+
|
44
|
+
opts.on_tail("-h", "--help", "Show this message") do
|
45
|
+
puts opts
|
46
|
+
exit
|
47
|
+
end
|
48
|
+
end.parse!
|
49
|
+
rescue OptionParser::InvalidOption
|
50
|
+
if $!.args.first == "--bc"
|
51
|
+
# This is an hidden method used by bash_completion
|
52
|
+
bookmarks = Bookmarks.new
|
53
|
+
puts bookmarks.bash_completion(ARGV[0])
|
54
|
+
exit 0
|
55
|
+
else
|
56
|
+
STDERR.puts $!
|
57
|
+
exit 1
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
bookmarks = Bookmarks.new
|
62
|
+
|
63
|
+
if options.has_key?(:add)
|
64
|
+
bookmarks.add(Dir.pwd, options[:add])
|
65
|
+
bookmarks.save
|
66
|
+
puts "Bookmark added."
|
67
|
+
end
|
68
|
+
|
69
|
+
if options.has_key?(:del)
|
70
|
+
if bookmarks.del(options[:del])
|
71
|
+
bookmarks.save
|
72
|
+
puts "Bookmark deleted."
|
73
|
+
else
|
74
|
+
STDERR.puts "Bookmark '#{options[:del]}' has not been deleted, it's unknown."
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
puts bookmarks if options.has_key?(:list)
|
79
|
+
if ARGV.size == 1
|
80
|
+
expanded_path = bookmarks.expand_path(ARGV[0])
|
81
|
+
if expanded_path.nil?
|
82
|
+
STDERR.puts "Unknown bookmark: '#{ARGV[0]}'."
|
83
|
+
puts Dir.pwd
|
84
|
+
else
|
85
|
+
puts expanded_path
|
86
|
+
end
|
87
|
+
elsif ARGV.size > 1
|
88
|
+
STDERR.puts "Wrong arguments."
|
89
|
+
puts Dir.pwd
|
90
|
+
end
|
data/lib/bookmarks.rb
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
# This file is part of the jump project
|
2
|
+
#
|
3
|
+
# Copyright (C) 2010 Flavio Castelli <flavio@castelli.name>
|
4
|
+
# Copyright (C) 2010 Giuseppe Capizzi <gcapizzi@gmail.com>
|
5
|
+
#
|
6
|
+
# kaveau is free software; you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU General Public License as published by
|
8
|
+
# the Free Software Foundation; either version 2 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
#
|
11
|
+
# kaveau is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU General Public License
|
17
|
+
# along with Keep; if not, write to the
|
18
|
+
# Free Software Foundation, Inc.,
|
19
|
+
# 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.
|
20
|
+
|
21
|
+
require 'rubygems'
|
22
|
+
require 'yaml'
|
23
|
+
require 'terminal-table/import'
|
24
|
+
|
25
|
+
class Bookmarks
|
26
|
+
BOOKMARKS_PATH = File.expand_path "~/.jumprc"
|
27
|
+
|
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 save configuration file"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Checks if +bookmark+ name is valid
|
40
|
+
def self.is_valid_name? bookmark
|
41
|
+
return (bookmark =~/\A\W/).nil?
|
42
|
+
end
|
43
|
+
|
44
|
+
# Saves the bookmarks list in the bookmarks file.
|
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
|
54
|
+
|
55
|
+
# Adds +bookmark+ pointing to +path+
|
56
|
+
def add path, bookmark
|
57
|
+
@bookmarks[bookmark] = path
|
58
|
+
end
|
59
|
+
|
60
|
+
# Deletes +bookmark+
|
61
|
+
def del bookmark
|
62
|
+
if @bookmarks.has_key? bookmark
|
63
|
+
@bookmarks.delete bookmark
|
64
|
+
true
|
65
|
+
else
|
66
|
+
false
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Prints the bookmarks list.
|
71
|
+
def to_s
|
72
|
+
if @bookmarks.empty?
|
73
|
+
"No bookmarks saved"
|
74
|
+
else
|
75
|
+
bookmarks_table = table do |t|
|
76
|
+
t.headings = "Bookmark", "Path"
|
77
|
+
@bookmarks.keys.sort.each do |bookmark|
|
78
|
+
t << [bookmark, @bookmarks[bookmark]]
|
79
|
+
end
|
80
|
+
end
|
81
|
+
bookmarks_table.to_s
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def bash_completion text
|
86
|
+
if text.nil? || text.empty?
|
87
|
+
# nothing is provided -> return all the bookmarks
|
88
|
+
@bookmarks.keys.sort.join(' ')
|
89
|
+
elsif text.include? '/'
|
90
|
+
if text.index('/') == 0
|
91
|
+
# this is an absolute path (eg: /foo)
|
92
|
+
return text
|
93
|
+
end
|
94
|
+
|
95
|
+
# [bookmark]/path
|
96
|
+
bookmark = text[0, text.index('/')]
|
97
|
+
path = text[text.index('/')+1, text.size]
|
98
|
+
if @bookmarks.has_key?(bookmark)
|
99
|
+
# this is a known bookmark
|
100
|
+
entries = []
|
101
|
+
Dir.foreach(@bookmarks[bookmark]) do |filename|
|
102
|
+
next if !path.empty? && (filename =~ /\A#{path}.*/).nil?
|
103
|
+
if File.directory?(File.join(@bookmarks[bookmark], filename))
|
104
|
+
next if filename == "." || filename == ".."
|
105
|
+
entries << File.join(bookmark, filename)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
if entries.empty?
|
110
|
+
text
|
111
|
+
else
|
112
|
+
entries << "#{bookmark}/"
|
113
|
+
entries.sort.join(' ')
|
114
|
+
end
|
115
|
+
else
|
116
|
+
# this is an unknown bookmark
|
117
|
+
text
|
118
|
+
end
|
119
|
+
else
|
120
|
+
# text could match one of the bookmarks
|
121
|
+
matches = @bookmarks.keys.find_all { |b| b =~ /\A#{text}/ }
|
122
|
+
if matches.empty?
|
123
|
+
text
|
124
|
+
else
|
125
|
+
matches.sort.join(' ')
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
# Expands paths that could start with a bookmark (e.g. [bookmark]/sub/path)
|
131
|
+
def expand_path(path_with_bookmark)
|
132
|
+
if path_with_bookmark.index("/").nil?
|
133
|
+
# the path is just a bookmark
|
134
|
+
return @bookmarks[path_with_bookmark]
|
135
|
+
elsif path_with_bookmark.index("/") == 0
|
136
|
+
# the path is an absolute path (no bookmark, e.g. /absolute/path)
|
137
|
+
return path_with_bookmark
|
138
|
+
else
|
139
|
+
# the path is composed of a bookmark and a subpath
|
140
|
+
name = path_with_bookmark[0, path_with_bookmark.index('/')]
|
141
|
+
path = path_with_bookmark[path_with_bookmark.index('/')+1,
|
142
|
+
path_with_bookmark.size]
|
143
|
+
return @bookmarks[name] + "/" + path;
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|