overpath 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f8be652dae7a95b56a2a470dac2e794bcca7ede3
4
+ data.tar.gz: 075eeb68462839818003675154bf2462f717da24
5
+ SHA512:
6
+ metadata.gz: 70b53e13618d162fc0f97536b032ec5d37593ddd3419b83045de5085e48b58c09e1616ea5fcffa6b05e54340c391772de5b84717ac826e2f989c8fab32b0e972
7
+ data.tar.gz: 7afd3db406c31a07d49d2c401cb83a6f97a55b2276645509ce8ddcef6880bee57a82b6dd40fa9dff42ce425e5544db767eecb6c925e6f81a3ec83220a5a4f2c0
@@ -0,0 +1,195 @@
1
+ #!/usr/bin/env ruby
2
+ require 'overpath'
3
+ require 'gli'
4
+
5
+ include GLI::App
6
+ program_desc 'Valhalla awaits!'
7
+ program_long_desc 'Find your path before Ragnarök comes!'
8
+ default_command 'help'
9
+ hide_commands_without_desc true
10
+ version '0.0.1'
11
+ sort_help :manually
12
+ accept(File) do |string|
13
+ File.path(string)
14
+ end
15
+ flag %i[file], desc: 'Specify a file to store paths',
16
+ default_value: File.join(Dir.home, '.overpath/overpath'),
17
+ arg_name: 'filepath',
18
+ type: File
19
+
20
+ pre do |global_options, _command, _options, _args|
21
+ path = global_options[:file]
22
+ dir = File.dirname(path)
23
+ Dir.mkdir(dir) unless Dir.exist?(dir)
24
+ @table = PathTable.new(path)
25
+ @table.load
26
+ end
27
+
28
+ post do
29
+ @table.save
30
+ end
31
+
32
+ desc 'Set one path with one key'
33
+ arg 'key'
34
+ arg 'path', :optional
35
+ command :add do |c|
36
+ c.desc 'Set favorite path'
37
+ c.switch %i[f favorite]
38
+ c.action do |_global_options, options, args|
39
+ key = args[0]
40
+ if key
41
+ value = File.expand_path(args[1] ? args[1] : Dir.pwd)
42
+ @table.table[key] = value
43
+ puts PathPrinter.str([key, value])
44
+ @table.favorite = key if options[:f]
45
+ end
46
+ end
47
+ end
48
+
49
+ desc 'Get paths with keys'
50
+ arg 'key, ...'
51
+ command :get do |c|
52
+ c.desc 'Get the path of directory instead of file'
53
+ c.switch %i[d directory]
54
+ c.desc 'List all paths'
55
+ c.switch %i[a all]
56
+ c.desc 'Don\'t escape the path'
57
+ c.switch [:E]
58
+ c.action do |_global_options, options, args|
59
+ list = if options[:a]
60
+ @table.table.values
61
+ elsif args.empty? && @table.favorite
62
+ [@table.table[@table.favorite]]
63
+ else
64
+ @table.table.values_at(*args).select { |item| item }
65
+ end
66
+ if options[:d]
67
+ list.map! { |path| (File.directory?(path) ? path : File.dirname(path)) }
68
+ end
69
+ puts PathPrinter.string(list, true, !options[:E])
70
+ end
71
+ end
72
+
73
+ desc 'Choose your favorite path'
74
+ arg 'key', :optional
75
+ command :fav do |c|
76
+ c.desc 'Clean current favorite path'
77
+ c.switch %i[c clean]
78
+ c.action do |_global_options, options, args|
79
+ if options[:c]
80
+ if @table.favorite
81
+ key_value = [@table.favorite, @table.table[@table.favorite]]
82
+ puts PathPrinter.str_del(key_value)
83
+ @table.favorite = nil
84
+ end
85
+ elsif args[0]
86
+ value = @table.table[args[0]]
87
+ if value
88
+ @table.favorite = args[0]
89
+ puts PathPrinter.str_mark([@table.favorite, value], @table.favorite)
90
+ end
91
+ elsif @table.favorite
92
+ key_value = [@table.favorite, @table.table[@table.favorite]]
93
+ puts PathPrinter.str_mark(key_value, @table.favorite)
94
+ end
95
+ end
96
+ end
97
+
98
+ desc 'List paths and their keys'
99
+ command :ls do |c|
100
+ c.desc 'List keys only'
101
+ c.switch %i[k keys-only]
102
+ c.desc 'List paths only'
103
+ c.switch %i[p paths-only]
104
+ c.desc 'Don\'t escape the paths'
105
+ c.switch [:E]
106
+ c.action do |_global_options, options, _args|
107
+ list = nil
108
+ if options[:k] == options[:p]
109
+ list = PathPrinter.string_with_mark(@table.table.to_a,
110
+ true,
111
+ !options[:E],
112
+ @table.favorite)
113
+ elsif options[:k]
114
+ list = PathPrinter.string(@table.table.keys,
115
+ true,
116
+ false)
117
+ elsif options[:p]
118
+ list = PathPrinter.string(@table.table.values,
119
+ true,
120
+ !options[:E])
121
+ end
122
+ puts list
123
+ end
124
+ end
125
+
126
+ desc '(Ex)change key of path to another'
127
+ arg 'key'
128
+ arg 'another'
129
+ command :mv do |c|
130
+ c.desc 'Exchange keys'
131
+ c.switch %i[e exchange]
132
+ c.action do |_global_options, options, args|
133
+ if args[0] && args[1]
134
+ @table.favorite = nil if args[0] == @table.favorite && !options[:e]
135
+ value = @table.table.delete(args[0])
136
+ another_value = @table.table.delete(args[1])
137
+ if value
138
+ puts PathPrinter.str_del([args[0], value]) unless options[:e]
139
+ value = File.expand_path(value)
140
+ @table.table[args[1]] = value
141
+ puts PathPrinter.str([args[1], value])
142
+ end
143
+ if options[:e] && another_value
144
+ another_value = File.expand_path(another_value)
145
+ @table.table[args[0]] = another_value
146
+ puts PathPrinter.str([args[0], another_value])
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+ desc 'Sort paths by key'
153
+ command :so do |c|
154
+ c.action do |_global_options, _options, _args|
155
+ @table.table = Hash[@table.table.sort_by { |key, _value| key }]
156
+ puts PathPrinter.string_with_mark(@table.table.to_a,
157
+ true,
158
+ true,
159
+ @table.favorite)
160
+ end
161
+ end
162
+
163
+ desc 'Delete paths with keys'
164
+ arg 'key, ...'
165
+ command :rm do |c|
166
+ c.desc 'Delete all paths'
167
+ c.switch %i[a all]
168
+ c.action do |_global_options, options, args|
169
+ list = (options[:a] ? @table.table.keys : args)
170
+ list.each do |key|
171
+ @table.favorite = nil if key == @table.favorite
172
+ value = @table.table.delete(key)
173
+ puts PathPrinter.str_del([key, value]) if value
174
+ end
175
+ end
176
+ end
177
+
178
+ command :'.' do |c|
179
+ c.action do |global_options, _options, args|
180
+ key = @app.exe_name
181
+ path = (File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__)
182
+ path = File.expand_path(path)
183
+ if args[0] == 'path'
184
+ puts PathPrinter.str(path)
185
+ elsif args[0] == 'command'
186
+ puts @commands_declaration_order.map(&:name).drop(2).join(' ')
187
+ elsif args[0] == 'file'
188
+ puts global_options[:file]
189
+ else
190
+ puts PathPrinter.str([key, path])
191
+ end
192
+ end
193
+ end
194
+
195
+ exit run(ARGV)
@@ -0,0 +1,2 @@
1
+ require_relative 'overpath/path_table'
2
+ require_relative 'overpath/path_printer'
@@ -0,0 +1,61 @@
1
+ require 'shellwords'
2
+
3
+ # Generator for print key-value pairs
4
+ class PathPrinter
5
+ class << self
6
+ def str(data)
7
+ string(data, false, true)
8
+ end
9
+
10
+ def str_del(data)
11
+ string_with_mark_deletion(data, false, true, '', true)
12
+ end
13
+
14
+ def str_mark(data, mark)
15
+ string_with_mark(data, false, true, mark)
16
+ end
17
+
18
+ def string(data, is_list, escaped)
19
+ string_with_mark(data, is_list, escaped, '')
20
+ end
21
+
22
+ def string_with_mark(data, is_list, escaped, mark)
23
+ string_with_mark_deletion(data, is_list, escaped, mark, false)
24
+ end
25
+
26
+ def string_with_mark_deletion(data, is_list, escaped, mark, deletion)
27
+ output = []
28
+ if is_list
29
+ data.each do |o|
30
+ output << string_from_key_value(o, escaped, mark, deletion)
31
+ end
32
+ else
33
+ output << string_from_key_value(data, escaped, mark, deletion)
34
+ end
35
+ output
36
+ end
37
+
38
+ def string_from_key_value(o, escaped, mark, deletion)
39
+ line = nil
40
+ if o.is_a?(Array) && o.size == 2
41
+ line = string_from_key_and_value(o, escaped, mark, deletion)
42
+ elsif o.is_a?(String)
43
+ line = string_from_key_or_value(o, escaped, mark, deletion)
44
+ end
45
+ line
46
+ end
47
+
48
+ def string_from_key_and_value(key_value, escaped, mark, deletion)
49
+ value = (escaped ? Shellwords.escape(key_value[1]) : key_value[1])
50
+ mark = (mark == key_value[0] ? " \u{2764}" : '')
51
+ deletion_mark = (deletion ? " \u{2716}" : '')
52
+ "\"#{key_value[0]}\" => \"#{value}\"#{mark}#{deletion_mark}"
53
+ end
54
+
55
+ def string_from_key_or_value(string, escaped, mark, deletion)
56
+ string += (mark == string ? " \u{2764}" : '')
57
+ string += (deletion ? " \u{2716}" : '')
58
+ (escaped ? Shellwords.escape(string) : string)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,41 @@
1
+ require 'json'
2
+
3
+ # Controller for paths
4
+ class PathTable
5
+ def initialize(file_path)
6
+ @file_path = file_path
7
+ @table = {}
8
+ end
9
+ PATHTABLE_FAVORITE_KEY = 'PATHTABLE_FAVORITE_KEY'.freeze
10
+ def save
11
+ File.open(@file_path + '.new', 'w+') do |file|
12
+ save_to_file(file)
13
+ end
14
+ File.rename(@file_path, @file_path + '.old') if File.exist?(@file_path)
15
+ File.rename(@file_path + '.new', @file_path)
16
+ end
17
+
18
+ def save_to_file(file)
19
+ favorite = { PATHTABLE_FAVORITE_KEY => @favorite }
20
+ table = @favorite ? @table.merge(favorite) : @table
21
+ file.puts JSON.generate(table)
22
+ File.foreach(@file_path).with_index do |line, i|
23
+ break if i > 14
24
+ file.puts line
25
+ end
26
+ end
27
+
28
+ def load
29
+ if File.exist?(@file_path)
30
+ table = JSON.parse(File.open(@file_path, 'r').gets)
31
+ if table.is_a?(Hash)
32
+ @table = table.reject { |key, _value| key == PATHTABLE_FAVORITE_KEY }
33
+ @table.each_value { |value| value.chomp!('/') }
34
+ @favorite = table[PATHTABLE_FAVORITE_KEY]
35
+ end
36
+ end
37
+ @table
38
+ end
39
+ attr_accessor :table
40
+ attr_accessor :favorite
41
+ end
@@ -0,0 +1,120 @@
1
+ == overpath - Valhalla awaits!
2
+
3
+ Find your path before Ragnarök comes!
4
+
5
+ v0.0.1
6
+
7
+ === Global Options
8
+ === --file filepath
9
+
10
+ Specify a file to store paths
11
+
12
+ [Default Value] /Users/fangqiuming/.overpath/overpath
13
+
14
+
15
+ === --help
16
+ Show this message
17
+
18
+
19
+
20
+ === --version
21
+ Display the program version
22
+
23
+
24
+
25
+ === Commands
26
+ ==== Command: <tt>add </tt>
27
+ Set one path with one key
28
+
29
+
30
+ ===== Options
31
+ ===== -f|--[no-]favorite
32
+ Set favorite path
33
+
34
+
35
+
36
+ ==== Command: <tt>fav </tt>
37
+ Choose your favorite path
38
+
39
+
40
+ ===== Options
41
+ ===== -c|--[no-]clean
42
+ Clean current favorite path
43
+
44
+
45
+
46
+ ==== Command: <tt>get </tt>
47
+ Get paths with keys
48
+
49
+
50
+ ===== Options
51
+ ===== -E
52
+ Don't escape the path
53
+
54
+
55
+
56
+ ===== -a|--[no-]all
57
+ List all paths
58
+
59
+
60
+
61
+ ===== -d|--[no-]directory
62
+ Get the path of directory instead of file
63
+
64
+
65
+
66
+ ==== Command: <tt>help command</tt>
67
+ Shows a list of commands or help for one command
68
+
69
+ Gets help for the application or its commands. Can also list the commands in a way helpful to creating a bash-style completion function
70
+ ===== Options
71
+ ===== -c
72
+ List commands one per line, to assist with shell completion
73
+
74
+
75
+
76
+ ==== Command: <tt>ls </tt>
77
+ List paths and their keys
78
+
79
+
80
+ ===== Options
81
+ ===== -E
82
+ Don't escape the paths
83
+
84
+
85
+
86
+ ===== -k|--[no-]keys-only
87
+ List keys only
88
+
89
+
90
+
91
+ ===== -p|--[no-]paths-only
92
+ List paths only
93
+
94
+
95
+
96
+ ==== Command: <tt>mv </tt>
97
+ (Ex)change key of path to another
98
+
99
+
100
+ ===== Options
101
+ ===== -e|--[no-]exchange
102
+ Exchange keys
103
+
104
+
105
+
106
+ ==== Command: <tt>rm </tt>
107
+ Delete paths with keys
108
+
109
+
110
+ ===== Options
111
+ ===== -a|--[no-]all
112
+ Delete all paths
113
+
114
+
115
+
116
+ ==== Command: <tt>so </tt>
117
+ Sort paths by key
118
+
119
+
120
+ [Default Command] help
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: overpath
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fang Qiuming
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gli
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.16'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ description: A manager of your common paths
42
+ email: fangqiuming@outlook.com
43
+ executables:
44
+ - overpath
45
+ extensions: []
46
+ extra_rdoc_files:
47
+ - overpath.rdoc
48
+ files:
49
+ - bin/overpath
50
+ - lib/overpath.rb
51
+ - lib/overpath/path_printer.rb
52
+ - lib/overpath/path_table.rb
53
+ - overpath.rdoc
54
+ homepage: http://rubygems.org/gems/overpath
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.6.11
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Drive in Bash like a Pro
78
+ test_files: []