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.



16
17
18
19
20
21
22
# File 'lib/specific_help.rb', line 16

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



12
13
14
# File 'lib/specific_help.rb', line 12

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

Instance Method Details

#add(item = 'new_item') ⇒ Object



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

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



154
155
156
157
158
159
160
161
162
163
# File 'lib/specific_help.rb', line 154

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

#backup_list(val) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/specific_help.rb', line 68

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



184
185
186
187
188
# File 'lib/specific_help.rb', line 184

def disp(lines)
#      lines.each{|line| puts "  +#{line}"} if lines != nil
  lines.each{|line| puts "*#{line}".blue}
#      lines.each{|line| puts CodeRay.scan("+#{line}", :Taskpaper).term}
end

#disp_help(key_word) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/specific_help.rb', line 175

def disp_help(key_word)
  print_separater
  items =@help_cont[key_word]
  puts items[:title].magenta
#      puts CodeRay.scan("-#{items[:title]}:", :Taskpaper).term
  disp(items[:cont])
  print_separater
end

#edit_helpObject



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/specific_help.rb', line 131

def edit_help
  p help_file =@source_file
  begin
    p command= "emacs #{help_file}"
    exec command
  rescue => e
    print "\nOption edit is not executable on windows. \n"
    print "Type the following shell command;\n\n"
    print "emacs /home/#{ENV['USER']}/.my_help/#{File.basename(@source_file)}\n\n"
    print "M-x ruby-mode should be good for edit.\n"
  end
end

#executeObject



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
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/specific_help.rb', line 24

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','hikiのformatに変更する'){to_hiki}
    opt.on('--all','すべてのhelp画面を表示させる'){all_help}
    opt.on('--store [item]','store [item] でback upをとる'){|item| store(item)}
    opt.on('--remove [item]','remove [item] back upしてるlistを消去する'){|item| remove(item) } 
    opt.on('--add [item]','add new [item]で新しいhelpを作る'){|item| add(item) }
    opt.on('--backup_list [val]','back upしているlistを表示させる'){|val| backup_list(val)}

#        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



171
172
173
# File 'lib/specific_help.rb', line 171

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

#hiki_help(key_word) ⇒ Object



165
166
167
168
169
# File 'lib/specific_help.rb', line 165

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



85
86
87
88
89
90
91
# File 'lib/specific_help.rb', line 85

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


190
191
192
# File 'lib/specific_help.rb', line 190

def print_separater
  print "---\n"
end

#remove(item) ⇒ Object



124
125
126
127
128
129
# File 'lib/specific_help.rb', line 124

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/specific_help.rb', line 93

def store(item)
  if item==nil
    print "spcify --store [item].\n"
    exit
  else
    print "Trying to store #{item}\n"
  end
  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")
  cont = {store_name.to_sym => store_item}
  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



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

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