rcd 0.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/rcd +4 -0
  2. data/lib/rcd.rb +71 -0
  3. metadata +48 -0
data/bin/rcd ADDED
@@ -0,0 +1,4 @@
1
+ #! /usr/bin/env ruby
2
+ require_relative '../lib/rcd'
3
+
4
+ Rcd.new ARGV.dup
data/lib/rcd.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'optparse'
2
+ require 'ostruct'
3
+ require 'pathname'
4
+
5
+ class Rcd # :nodoc:
6
+ attr_accessor :options, :profile
7
+ def initialize(argv) # :nodoc:
8
+ # profile path
9
+ @profile = Pathname.new(Dir.home).join('.rcd_profile')
10
+
11
+ @options = OpenStruct.new
12
+ (OptionParser.new do |opts|
13
+ opts.banner = 'Usage: rcd path/key [options]'
14
+ opts.separator ''
15
+ opts.separator 'SPECIFIC OPTIONS:'
16
+
17
+ opts.on('-a [key,new_path]', :REQUIRED, Array, 'Add a new target file or direcotry') do |key, path|
18
+ options.key = key
19
+ options.path = path
20
+ options.add_new_path = true
21
+ argv.delete('-a')
22
+ argv.delete("#{key},#{path}")
23
+ end
24
+
25
+ opts.on('-l', 'List all the saved paths') do
26
+ options.list_all = true
27
+ argv.delete('-l')
28
+ end
29
+
30
+ opts.on('-h', 'Display help message') do
31
+ p opts
32
+ exit
33
+ end
34
+ end).parse(argv)
35
+
36
+ list_all_saved_paths if options.list_all
37
+ add_new_target_path if options.add_new_path
38
+ puts(get_path(argv[0])) unless argv.length.zero?
39
+ end
40
+
41
+ def list_all_saved_paths
42
+ unless profile.exist?
43
+ puts "\e[031m Not Paths Saved"
44
+ exit
45
+ end
46
+ puts ' All the saved paths:'
47
+ profile.each_line do |l|
48
+ puts(sprintf(" \e[032m%10s\e[0m %s", *l.split(','))) unless l.match(/\^n/)
49
+ end
50
+ end
51
+
52
+ def add_new_target_path
53
+ mode = profile.exist? ? 'a' : 'w'
54
+ profile.open(mode) do |f|
55
+ f << "#{options.key},#{options.path}\n"
56
+ end
57
+ puts "\e[32m Add Success"
58
+ end
59
+
60
+ def get_path(key)
61
+ path = ''
62
+ profile.each_line do |line|
63
+ path_pair = line.split(',')
64
+ if path_pair[0].match(Regexp.new(key))
65
+ path = path_pair[1]
66
+ break
67
+ end
68
+ end
69
+ path
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rcd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.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 some frequent used paths and keeping them at hand, in a
15
+ very ease and comfortable way.
16
+ email: bom.d.van@gmail.com
17
+ executables:
18
+ - rcd
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/rcd.rb
23
+ - bin/rcd
24
+ homepage: http://rubygems.org/gems/rcd
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: My First Ruby Gem!
48
+ test_files: []