Class: Rcd

Inherits:
Object
  • Object
show all
Defined in:
lib/rcd.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Rcd

:nodoc:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rcd.rb', line 7

def initialize(argv) # :nodoc:
  # profile path
  @profile = Pathname.new(Dir.home).join('.rcd_profile')
  
  @options = OpenStruct.new
  (OptionParser.new do |opts|
    opts.banner = 'Usage: rcd path/key [options]'
    opts.separator ''
    opts.separator 'SPECIFIC OPTIONS:'

    opts.on('-a [key,new_path]', :REQUIRED, Array, 'Add a new target file or direcotry') do |key, path|
      options.key = key
      options.path = path
      options.add_new_path = true
      argv.delete('-a')
      argv.delete("#{key},#{path}")
    end
    
    opts.on('-p', '--add-pwd [key]', :REQUIRED, String, 'Add $(pwd) and a key in rcd_profile') do |key|
      options.key = key
      options.path = Dir.pwd
      options.add_new_path = true
      argv.delete('-p')
      argv.delete('--add-pwd')
      argv.delete(key)
    end
    
    opts.on('-l', '--list', 'List all the saved paths') do
      options.list_all = true
      argv.delete('-l')
    end
    
    opts.on('-h', '--help', 'Display help message') do
      p opts
      exit
    end
    
    # opts.on('-v', '--version', 'Display help message') do
    #   p opts
    #   exit
    # end
  end).parse(argv)
  
  list_all_saved_paths if options.list_all
  add_new_target_path if options.add_new_path
  puts(get_path(argv[0])) unless argv.length.zero?
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/rcd.rb', line 6

def options
  @options
end

#profileObject

Returns the value of attribute profile.



6
7
8
# File 'lib/rcd.rb', line 6

def profile
  @profile
end

Instance Method Details

#add_new_target_pathObject



66
67
68
69
70
71
72
# File 'lib/rcd.rb', line 66

def add_new_target_path
  mode = profile.exist? ? 'a' : 'w'
  profile.open(mode) do |f|
    f << "#{options.key},#{options.path}\n"
  end
  puts "\e[32m Add Success"
end

#get_path(key) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rcd.rb', line 74

def get_path(key)
  path = ''
  profile.each_line do |line|
    path_pair = line.split(',')
    if path_pair[0].match(Regexp.new(key))
      path = path_pair[1]
      break
    end
  end
  path
end

#list_all_saved_pathsObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/rcd.rb', line 55

def list_all_saved_paths
  unless profile.exist?
    puts "\e[031m Not Paths Saved"
    exit
  end
  puts 'All the saved paths:'
  profile.each_line do |l|
    puts(sprintf("\e[032m%10s\e[0m    %s", *l.split(','))) unless l.match(/\^n/)
  end
end