Class: HikiUtils::Command

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ Command

Returns a new instance of Command.



22
23
24
25
26
27
28
# File 'lib/hikiutils.rb', line 22

def initialize(argv=[])
  @argv = argv
  @data_name=['nick_name','local_dir','local_uri','global_dir','global_uri']
  data_path = File.join(ENV['HOME'], '.hikirc')
  DataFiles.prepare(data_path)
  read_sources
end

Class Method Details

.run(argv = []) ⇒ Object



17
18
19
20
# File 'lib/hikiutils.rb', line 17

def self.run(argv=[])
  print "hiki says 'Hello world'.\n"
  new(argv).execute
end

Instance Method Details

#add_sourcesObject



230
231
232
233
234
235
236
237
238
239
# File 'lib/hikiutils.rb', line 230

def add_sources
  cont = {}
  @data_name.each{|name|
    printf("%s ? ", name)
    tmp = gets.chomp
    cont[name.to_sym] = tmp
  }
  @src[:srcs] << cont
  show_sources
end

#check_dbObject



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

def check_db
  result= InfoDB.new(@l_dir).show_inconsist
  print (result=='') ? "db agrees with text dir.\n" : result
end

#db_file(file_name) ⇒ Object



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

def db_file(file_name)
  info=InfoDB.new(@l_dir)
  p info.show(file_name)
end

#display_format(id, name, local, global) ⇒ Object



220
221
222
223
224
# File 'lib/hikiutils.rb', line 220

def display_format(id, name, local, global)
  name_length  = 10-full_width_count(name)
  local_length = 40-full_width_count(local)
  [id.to_s.rjust(3), name.ljust(name_length),local.ljust(local_length), global.center(10)].join(' | ')
end

#dump_sourcesObject



190
191
192
193
194
# File 'lib/hikiutils.rb', line 190

def dump_sources
  file = File.open(DATA_FILE,'w')
  YAML.dump(@src, file)
  file.close
end

#edit_file(file) ⇒ Object



179
180
181
182
183
184
185
186
187
188
# File 'lib/hikiutils.rb', line 179

def edit_file(file)
  t_file=File.join(@l_dir,'text',file)
  if !File.exist?(t_file) then
    file=File.open(t_file,'w')
    file.close
    File.chmod(0777,t_file)
  end
  p command="open -a mi #{t_file}"
  system command
end

#executeObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hikiutils.rb', line 30

def execute
  @argv << '--help' if @argv.size==0
  command_parser = OptionParser.new do |opt|
    opt.on('-v', '--version','show program Version.') { |v|
      opt.version = HikiUtils::VERSION
      puts opt.ver
    }
    opt.on('-s', '--show','show sources') {show_sources}
    opt.on('-a', '--add','add sources info') {add_sources }
    opt.on('-t', '--target VAL','set target id') {|val| set_target(val) }
    opt.on('-e', '--edit FILE','open file') {|file| edit_file(file) }
    opt.on('-l', '--list [FILE]','list files') {|file| list_files(file) }
    opt.on('-u', '--update FILE','update file') {|file| update_file(file) }
    opt.on('-r', '--rsync','rsync files') {rsync_files}
    opt.on('-d', '--database FILE','read database file') {|file| db_file(file)}
    opt.on('-c', '--checkdb','check database file') {check_db}
    opt.on('--remove FILE','remove file') {|file| remove_file(file)}
    opt.on('--move FILES','move file1,file2',Array) {|files| move_file(files)}
  end
  command_parser.parse!(@argv)
  dump_sources
  exit
end

#full_width_count(string) ⇒ Object



226
227
228
# File 'lib/hikiutils.rb', line 226

def full_width_count(string)
  string.each_char.select{|char| !(/[ -~。-゚]/.match(char))}.count
end

#list_files(file) ⇒ Object



172
173
174
175
176
177
# File 'lib/hikiutils.rb', line 172

def list_files(file)
  file ='' if file==nil
  t_file=File.join(@l_dir,'text')
  print "target_dir : "+t_file+"\n"
  print `cd #{t_file} ; ls -lt #{file}*`
end

#move_file(files) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/hikiutils.rb', line 54

def move_file(files)
  begin
    p file1_path = File.join(@l_dir,'text',files[0])
    p file2_path = File.join(@l_dir,'text',files[1])
  rescue => evar
    puts evar.to_s
    puts "error on move_files, check the input format, especially comma separation."
    exit
  end
  return if file1_path==file2_path
  if File.exist?(file2_path) then
    print ("moving target #{files[1]} exists.\n")
    print ("first remove #{files[1]}.\n")
    return
  else
    File.rename(file1_path,file2_path)
  end

  info=InfoDB.new(@l_dir)

  db = info.db

#      pp file0=db[files[0]]
#      db.delete(files[0])
#      db[files[1]]=file0
#      db[files[1]][:title]=files[1] if db[files[1]][:title]==files[0]
#      pp db[files[1]]

  db.each{|ele|
    ref = ele[1][:references]
    if ref.include?(files[0]) then
      p link_file=ele[0]
      link_path = File.join(@l_dir,'text',link_file)

      target=File.open(link_path,'r')
      cont = target.read
      target.close
      if NKF.guess(cont)==NKF::EUC then
        print "Don\'t support EUC-JP, change link manually.\n"
      else

        cont.gsub!(/\[\[#{files[0]}\]\]/,"\[\[#{files[1]}\]\]")
        target=File.open(link_path,'w')
        target.print cont
        target.close

        ref.delete(files[0])
        ref << files[1]

        p cache_path = File.join(@l_dir,'cache/parser',link_file)
        begin
          File.delete(cache_path)
        rescue => evar
          puts evar.to_s
        end
      end
    end
  }

  info.dump
end

#read_sourcesObject



241
242
243
244
245
246
247
# File 'lib/hikiutils.rb', line 241

def read_sources
  file = File.open(DATA_FILE,'r')
  @src = YAML.load(file.read)
  file.close
  @target = @src[:target]
  @l_dir=@src[:srcs][@target][:local_dir]
end

#remove_file(file_name) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/hikiutils.rb', line 116

def remove_file(file_name)
  p text_path = File.join(@l_dir,'text',file_name)
  p attach_path = File.join(@l_dir,'cache/attach',file_name)
  begin
    File.delete(text_path)
  rescue => evar
    puts evar.to_s
  end
  begin
    Dir.rmdir(attach_path)
  rescue => evar
    puts evar.to_s
  end

  info=InfoDB.new(@l_dir)
  p "delete "
  del_file=info.delete(file_name)

  info.dump
end

#rsync_filesObject



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

def rsync_files
  p local = @l_dir
  p global = @src[:srcs][@target][:global_dir]
  p command="rsync -auvz --delete -e ssh #{local}/ #{global}"
  system command
end

#set_target(val) ⇒ Object



196
197
198
199
# File 'lib/hikiutils.rb', line 196

def set_target(val)
  @src[:target] = val.to_i
  show_sources
end

#show_sourcesObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/hikiutils.rb', line 201

def show_sources()
  printf("target_no:%i\n",@src[:target])
  printf("editor_command:%s\n",@src[:editor_command])
  header = display_format('id','nick name','local directory','global uri')

  puts header
  puts '-' * header.size

  @src[:srcs].each_with_index{|src,i|
    target = i==@src[:target] ? '*':' '
    id = target+i.to_s
    name=src[:nick_name]
    local=src[:local_dir]
    global=src[:global_uri]
    puts display_format(id,name,local,global)
  }

end

#update_file(file0) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/hikiutils.rb', line 154

def update_file(file0)
  file = (file0==nil) ? 'FrontPage' : file0
  #rm cache file
  t_file=File.join(@l_dir,'cache/parser',file)
  p command="rm #{t_file}"
  system command

  #update info file
  info=InfoDB.new(@l_dir)
  info.update(file0)

  #open file on browser
  l_path = @src[:srcs][@target][:local_uri]
  l_file=l_path+"/?"+file
  p command="open \'#{l_file}\'"
  system command
end