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.



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

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



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

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

Instance Method Details

#add_sourcesObject



227
228
229
230
231
232
233
234
235
236
# File 'lib/hikiutils.rb', line 227

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



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

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

#db_file(file_name) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/hikiutils.rb', line 143

def db_file(file_name)
  db = Hash.new
  p file_path = File.join(@l_dir,'info.db')
  cont = File.read(file_path)
  db = TMarshal::load(cont)
  p db[file_name]
end

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



217
218
219
220
221
# File 'lib/hikiutils.rb', line 217

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



187
188
189
190
191
# File 'lib/hikiutils.rb', line 187

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

#edit_file(file) ⇒ Object



176
177
178
179
180
181
182
183
184
185
# File 'lib/hikiutils.rb', line 176

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



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

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



223
224
225
# File 'lib/hikiutils.rb', line 223

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

#list_files(file) ⇒ Object



169
170
171
172
173
174
# File 'lib/hikiutils.rb', line 169

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



52
53
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
# File 'lib/hikiutils.rb', line 52

def move_file(files)
  p file1_path = File.join(@l_dir,'text',files[0])
  p file2_path = File.join(@l_dir,'text',files[1])
  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

  db = Hash.new
  p db_path = File.join(@l_dir,'info.db')
  file = File.open(db_path,'r')
  cont = file.read
  db = TMarshal::load(cont)
  file.close

  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
      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
  }

  file = File.open(db_path,'w')
  TMarshal::dump(db,file)
  file.close
end

#read_sourcesObject



238
239
240
241
242
243
244
# File 'lib/hikiutils.rb', line 238

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/hikiutils.rb', line 109

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

  db = Hash.new
  p db_path = File.join(@l_dir,'info.db')
  file = File.open(db_path,'r')
  cont = file.read
  db = TMarshal::load(cont)
  file.close

  db.delete(file_name)

  file = File.open(db_path,'w')
  TMarshal::dump(db,file)
  file.close

end

#rsync_filesObject



151
152
153
154
155
156
# File 'lib/hikiutils.rb', line 151

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



193
194
195
196
# File 'lib/hikiutils.rb', line 193

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

#show_sourcesObject



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

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



158
159
160
161
162
163
164
165
166
167
# File 'lib/hikiutils.rb', line 158

def update_file(file0)
  file = (file0==nil) ? 'FrontPage' : file0
  t_file=File.join(@l_dir,'cache/parser',file)
  p command="rm #{t_file}"
  system command
  l_path = @src[:srcs][@target][:local_uri]
  l_file=l_path+"/?"+file
  p command="open \'#{l_file}\'"
  system command
end