Paths 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/paths +4 -0
  2. data/lib/paths.rb +111 -0
  3. metadata +48 -0
data/bin/paths ADDED
@@ -0,0 +1,4 @@
1
+ #! /usr/bin/env ruby
2
+ require_relative '../lib/paths'
3
+
4
+ Paths.new ARGV
data/lib/paths.rb ADDED
@@ -0,0 +1,111 @@
1
+ require 'optparse'
2
+ require 'ostruct'
3
+ require 'pathname'
4
+ require 'tempfile'
5
+
6
+ class Paths # :nodoc:
7
+ Profile_Name = '.paths_profile'
8
+ attr_accessor :options, :profile
9
+ def initialize(argv) # :nodoc:
10
+ argv.push('-h') if argv.length.zero?
11
+ @options = OpenStruct.new
12
+ (OptionParser.new do |opts|
13
+ opts.banner = 'Usage: paths key [options]'
14
+ opts.separator ''
15
+ opts.separator 'SPECIFIC OPTIONS:'
16
+
17
+ opts.on('-a', '--add key,new_path', :REQUIRED, Array, 'Add a new target file or direcotry, the default new paths is $(pwd)') do |key, path|
18
+ options.key = key
19
+ options.path = path.nil? ? Dir.pwd : path
20
+ options.add_new_path = true
21
+ end
22
+
23
+ opts.on('-r', '--remove key', String, 'Remove Key') do |key|
24
+ options.remove_path = true
25
+ options.key = key
26
+ end
27
+
28
+ opts.on('--rename old_key,new_key', Array, 'Rename key') do |old_key, new_key|
29
+ options.rename_key = true
30
+ options.old_key = old_key
31
+ options.new_key = new_key
32
+ end
33
+
34
+ opts.on('-l', '--list', 'List all the saved paths') do
35
+ options.list_all = true
36
+ end
37
+
38
+ opts.on('-h', '--help', 'Display help message') do
39
+ p opts
40
+ exit
41
+ end
42
+
43
+ # opts.on('-v', '--version', 'Display help message') do
44
+ # p opts
45
+ # exit
46
+ # end
47
+ end).parse(argv)
48
+
49
+ # profile path
50
+ @profile = Pathname.new(Dir.home).join(Profile_Name)
51
+ list_all_saved_paths if options.list_all
52
+ add_new_target_path if options.add_new_path
53
+ remove_path if options.remove_path
54
+ rename_key if options.rename_key
55
+ puts(get_path(argv[0])) if argv.length == 1 && !argv[0].include?('-')
56
+ end
57
+
58
+ def list_all_saved_paths
59
+ unless profile.exist?
60
+ puts "\e[031m Not Paths Saved"
61
+ exit
62
+ end
63
+ puts 'All the saved paths:'
64
+ profile.each_line do |l|
65
+ puts(sprintf("\e[032m%10s\e[0m %s", *l.split(','))) unless l.match(/\^n/)
66
+ end
67
+ end
68
+
69
+ def add_new_target_path
70
+ mode = profile.exist? ? 'a' : 'w'
71
+ profile.open(mode) do |f|
72
+ f << "#{options.key},#{options.path}\n"
73
+ end
74
+ puts "\e[32m Add Success"
75
+ end
76
+
77
+ def remove_path
78
+ tmp_paths_profile = Tempfile.new(Profile_Name)
79
+ profile.each_line do |line|
80
+ next if line.split(',').first.match(Regexp.new(options.key))
81
+ tmp_paths_profile << line
82
+ end
83
+ FileUtils.mv(tmp_paths_profile.to_path, profile.to_path)
84
+ puts "\e[31m Remove Success"
85
+ end
86
+
87
+ def rename_key
88
+ tmp_paths_profile = Tempfile.new(Profile_Name)
89
+ profile.each_line do |line|
90
+ if line.split(',').first.match(Regexp.new(options.old_key))
91
+ tmp_paths_profile << "#{options.new_key},#{line.split(',')[1]}"
92
+ else
93
+ tmp_paths_profile << line
94
+ end
95
+ end
96
+ FileUtils.mv(tmp_paths_profile.to_path, profile.to_path)
97
+ puts "\e[32m Rename Success"
98
+ end
99
+
100
+ def get_path(key)
101
+ path = ''
102
+ profile.each_line do |line|
103
+ path_pair = line.split(',')
104
+ if path_pair[0].match(Regexp.new(key))
105
+ path = path_pair[1]
106
+ break
107
+ end
108
+ end
109
+ path
110
+ end
111
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Paths
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Van Hu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-22 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: A gem saving frequent used paths and keeping them at hand, in a very
15
+ ease and comfortable way.
16
+ email: bom.d.van@gmail.com
17
+ executables:
18
+ - paths
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/paths.rb
23
+ - bin/paths
24
+ homepage: http://rubygems.org/gems/paths
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.17
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: ! '*nix system file system helper'
48
+ test_files: []