Class: SpecificHelp::Command

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, argv = []) ⇒ Command

Returns a new instance of Command.



14
15
16
17
18
19
20
# File 'lib/specific_help.rb', line 14

def initialize(file,argv=[])
  @source_file = file
  @help_cont = YAML.load(File.read(file))
  @help_cont[:head].each{|line| print line.chomp+"\n" } if @help_cont[:head] != nil
  @help_cont[:license].each{|line| print "#{line.chomp}\n" } if @help_cont[:license] != nil
  @argv = argv
end

Class Method Details

.run(file, argv = []) ⇒ Object



10
11
12
# File 'lib/specific_help.rb', line 10

def self.run(file,argv=[])
  new(file, argv).execute
end

Instance Method Details

#add(item = 'new_item') ⇒ Object



98
99
100
101
102
103
104
# File 'lib/specific_help.rb', line 98

def add(item='new_item')
  print "Trying to add #{item}\n"
  new_item={:opts=>{:short=>'-'+item[0], :long=>'--'+item, :desc=>item},
      :title=>item, :cont=> [item]}
  @help_cont[item.to_sym]=new_item
  File.open(@source_file,'w'){|file| file.print YAML.dump(@help_cont)}
end

#all_helpObject



127
128
129
130
131
132
133
134
135
# File 'lib/specific_help.rb', line 127

def all_help
  @help_cont.each_pair{|key,val|
    if key==:head or key==:license
      disp(val)
    else
      disp_help(key)
    end
  }
end

#backup_list(val) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/specific_help.rb', line 56

def backup_list(val)
  val = val || 10
  print "\n ...showing last #{val} stored items in backup.\n"
  backup=mk_backup_file(@source_file)
  File.open(backup,'r'){|file|
    backup_cont = YAML.load(File.read(backup))
    backup_size = backup_cont.size
    backup_size = backup_size>val.to_i ? val.to_i : backup_size
    backup_keys = backup_cont.keys
    backup_keys.reverse.each_with_index{|item,i|
      break if i>=backup_size
      line = item.to_s.split('_')
      printf("%10s : %8s%8s\n",line[0],line[1],line[2])
    }
  }
end

#disp(lines) ⇒ Object



155
156
157
# File 'lib/specific_help.rb', line 155

def disp(lines)
  lines.each{|line| puts "  *#{line}"} if lines != nil
end

#disp_help(key_word) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/specific_help.rb', line 147

def disp_help(key_word)
  print_separater
  items =@help_cont[key_word]
  puts items[:title]
  disp(items[:cont])
  print_separater
end

#edit_helpObject



113
114
115
# File 'lib/specific_help.rb', line 113

def edit_help
  system("emacs #{@source_file}")
end

#executeObject



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
54
# File 'lib/specific_help.rb', line 22

def execute
  if @argv.size==0
    if @source_file.include?('todo')
      @argv << '--all'
    else
      @argv << '--help'
    end
  end
  command_parser = OptionParser.new do |opt|
    opt.on('-v', '--version','show program Version.') { |v|
      opt.version = MyHelp::VERSION
      puts opt.ver
    }
    @help_cont.each_pair{|key,val|
      next if key==:head or key==:license
      opts = val[:opts]
      opt.on(opts[:short],opts[:long],opts[:desc]) {disp_help(key)}
    }
    opt.on('--edit','edit help contents'){edit_help}
    opt.on('--to_hiki','convert to hikidoc format'){to_hiki}
    opt.on('--all','display all helps'){all_help}
    opt.on('--store [item]','store [item] in backfile'){|item| store(item)}
    opt.on('--remove [item]','remove [item] and store in backfile'){|item| remove(item) }
    opt.on('--add [item]','add new [item]'){|item| add(item) }
    opt.on('--backup_list [val]','show last [val] backup list'){|val| backup_list(val)}
  end
  begin
    command_parser.parse!(@argv)
  rescue=> eval
    p eval
  end
  exit
end

#hiki_disp(lines) ⇒ Object



143
144
145
# File 'lib/specific_help.rb', line 143

def hiki_disp(lines)
  lines.each{|line| puts "*#{line}"}  if lines != nil
end

#hiki_help(key_word) ⇒ Object



137
138
139
140
141
# File 'lib/specific_help.rb', line 137

def hiki_help(key_word)
  items =@help_cont[key_word]
  puts "\n!!"+items[:title]+"\n"
  hiki_disp(items[:cont])
end

#mk_backup_file(file) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/specific_help.rb', line 73

def mk_backup_file(file)
  path = File.dirname(file)
  base = File.basename(file)
  backup= File.join(path,"."+base)
  FileUtils.touch(backup) unless File.exists?(backup)
  return backup
end


159
160
161
# File 'lib/specific_help.rb', line 159

def print_separater
  print "---\n"
end

#remove(item) ⇒ Object



106
107
108
109
110
111
# File 'lib/specific_help.rb', line 106

def remove(item)
  print "Trying to remove #{item}\n"
  store(item)
  @help_cont.delete(item.to_sym)
  File.open(@source_file,'w'){|file| file.print YAML.dump(@help_cont)}
end

#store(item) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/specific_help.rb', line 81

def store(item)
  print "Trying to store #{item}\n"
  backup=mk_backup_file(@source_file)
  unless store_item = @help_cont[item.to_sym] then
    print "No #{item} in this help.  The items are following...\n"
    keys = @help_cont.keys
    keys.each{|key|
      p key
    }
    exit
  end
  p store_name = item+"_"+Time.now.strftime("%Y%m%d_%H%M%S")
  backup_cont=YAML.load(File.read(backup)) || {}
  backup_cont[store_name.to_sym]=store_item
  File.open(backup,'w'){|file| file.print(YAML.dump(backup_cont))}
end

#to_hikiObject



117
118
119
120
121
122
123
124
125
# File 'lib/specific_help.rb', line 117

def to_hiki
  @help_cont.each_pair{|key,val|
    if key==:head or key==:license
      hiki_disp(val)
    else
      hiki_help(key)
    end
  }
end