aka2 0.1.9 → 0.1.10.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,166 @@
1
+ module Aka
2
+
3
+ def self.setup_aka
4
+ userBash = []
5
+ # 1. check for each type of file without setting anything.
6
+ if File.exist?("#{ZSHRC_PATH}") #if zshrc exist
7
+ userBash.push(".zshrc")
8
+ end
9
+ if File.exist?("#{BASHRC_PATH}") #if bashrc exist
10
+ userBash.push(".bashrc")
11
+ end
12
+ if File.exist?("#{BASH_PROFILE_PATH}") #if bash_profile exist
13
+ userBash.push(".bash_profile")
14
+ end
15
+ if File.exist?("#{PROFILE_PATH}") #if .profile exist
16
+ userBash.push(".profile")
17
+ end
18
+
19
+ #2. count the number of types
20
+
21
+ #3 if number of types is 1, proceed to set it
22
+ if userBash.count == 1
23
+ set_to_dotfile(userBash.first)
24
+
25
+ elsif userBash.count > 1
26
+ #4 if the number of types is more than 1, proceed to ask which one does the users want to uses.
27
+ userBash.each_with_index do |choice,i|
28
+ puts "#{i+1}. Setup in #{Dir.home}/#{choice}"
29
+ end
30
+ choice = ask "Please choose which location you wish to setup? (Choose a number and enter)\n"
31
+
32
+ #5 once you receive input, then you set it according to input
33
+ case choice
34
+ when "1"
35
+ set_to_dotfile(userBash[0]) if userBash[0]
36
+ when "2"
37
+ if userBash[1] then set_to_dotfile(userBash[1]) else abort "No file choosen" end
38
+ when "3"
39
+ if userBash[2] then set_to_dotfile(userBash[2]) else abort "No file choosen" end
40
+ when "4"
41
+ if userBash[3] then set_to_dotfile(userBash[3]) else abort "No file choosen" end
42
+ else
43
+ puts "Invalid input, Please enter the number between 1 and #{userBash.count}. Please try again"
44
+ abort "No file choosen"
45
+ end
46
+ end #if userBash > 1
47
+
48
+ # if File.exist?("#{ZSHRC_PATH}") #if zshec exist
49
+ # setZSHRC2
50
+ # elsif File.exist?("#{BASHRC_PATH}") #if bashrc exist
51
+ # setBASHRC2
52
+ # elsif File.exist?("#{BASH_PROFILE_PATH}") #if bash_profile exist
53
+ # setBASH2
54
+ # else
55
+ # puts "Aka2 only supports zshrc, bashrc and bash_profile"
56
+ # puts "Please contact http://github.com/ytbryan for more info."
57
+ # end
58
+ end
59
+
60
+
61
+ def self.set_to_dotfile(filename)
62
+ if filename == ".zshrc"
63
+ setZSHRC2
64
+ elsif filename == ".bashrc"
65
+ setBASHRC2
66
+ elsif filename == ".bash_profile"
67
+ setBASH2
68
+ elsif filename == ".profile"
69
+ setPROFILE
70
+ end
71
+ end
72
+
73
+ def self.setup_autosource
74
+ if File.exist?("#{CONFIG_PATH}")
75
+ out_file = File.new("#{CONFIG_PATH}/autosource", "w")
76
+ out_file.puts("export HISTSIZE=10000")
77
+ out_file.puts("sigusr2() { unalias $1;}")
78
+ out_file.puts("sigusr1() { source #{readYML("#{CONFIG_PATH}")["dotfile"]}; history -a; echo 'reloaded dot file'; }")
79
+ out_file.puts("trap sigusr1 SIGUSR1")
80
+ out_file.puts("trap 'sigusr2 $(cat ~/sigusr1-args)' SIGUSR2")
81
+ out_file.close
82
+ autosource = "\nsource \"#{CONFIG_PATH}/autosource\""
83
+ append(autosource, readYML("#{CONFIG_PATH}")['profile'])
84
+ puts "Done. Please restart this shell.".red
85
+ else
86
+ puts "Directory #{CONFIG_PATH} doesn't exist"
87
+ end
88
+ end
89
+ def self.setup_config
90
+ if File.exist?("#{CONFIG_PATH}")
91
+ puts "Directory #{CONFIG_PATH} exist"
92
+ else
93
+ FileUtils::mkdir_p("#{CONFIG_PATH}")
94
+ out_file = File.new("#{CONFIG_PATH}", "w")
95
+ out_file.puts("---")
96
+ out_file.puts("dotfile: \"/home/user/.bashrc\"")
97
+ out_file.puts("history: \"/home/user/.bash_history\"")
98
+ out_file.puts("home: \"/home/user/.aka\"")
99
+ out_file.puts("install: \"/usr/local/bin\"")
100
+ out_file.puts("profile: \"/home/user/.bashrc\"")
101
+ out_file.puts("list: 20")
102
+ out_file.puts("usage: 20")
103
+ out_file.puts("remote: 12.12.12.21")
104
+ out_file.close
105
+ end
106
+ end
107
+
108
+ def self.showConfig
109
+ thing = YAML.load_file("#{CONFIG_PATH}")
110
+ puts ""
111
+ thing.each do |company,details|
112
+ puts "#{company} -> " + "#{details}".red
113
+ end
114
+ end
115
+
116
+ def self.setZSHRC
117
+ setPath("#{ZSHRC_PATH}","dotfile")
118
+ setPath("#{Dir.home}/.zsh_history","history")
119
+ setPath("/etc/zprofile","profile")
120
+ end
121
+
122
+ def self.setBASHRC
123
+ setPath("#{BASHRC_PATH}","dotfile")
124
+ setPath("#{Dir.home}/.bash_history","history")
125
+ setPath("/etc/profile","profile")
126
+ end
127
+
128
+ def self.setBASH
129
+ setPath("#{BASH_PROFILE_PATH}","dotfile")
130
+ setPath("#{Dir.home}/.bash_history","history")
131
+ setPath("/etc/profile","profile")
132
+ end
133
+
134
+ def self.setZSHRC2 #ryan - set the right dotfile and profile
135
+ setPath("#{ZSHRC_PATH}","dotfile")
136
+ setPath("#{Dir.home}/.zsh_history","history")
137
+ setPath("#{ZSHRC_PATH}","profile")
138
+ setPath("#{AKA_PATH}","home")
139
+ end
140
+
141
+ def self.setBASHRC2 #ryan - set the right dotfile and profile
142
+ setPath("#{BASHRC_PATH}","dotfile")
143
+ setPath("#{Dir.home}/.bash_history","history")
144
+ setPath("#{BASHRC_PATH}","profile")
145
+ setPath("#{AKA_PATH}","home")
146
+ end
147
+
148
+ def self.setBASH2 #ryan - set the right dotfile and profile
149
+ setPath("#{BASH_PROFILE_PATH}","dotfile")
150
+ setPath("#{Dir.home}/.bash_history","history")
151
+ setPath("/etc/profile","profile")
152
+ setPath("#{AKA_PATH}","home")
153
+ end
154
+
155
+ def self.setPROFILE
156
+ setPath("#{PROFILE_PATH}","dotfile")
157
+ setPath("#{Dir.home}/.bash_history","history")
158
+ setPath("/etc/profile","profile")
159
+ setPath("#{AKA_PATH}","home")
160
+ end
161
+
162
+ def self.isOhMyZsh
163
+ readYML("#{CONFIG_PATH}")["dotfile"] == "#{ZSHRC_PATH}" ? TRUE : FALSE
164
+ end
165
+
166
+ end
@@ -0,0 +1,8 @@
1
+ module Aka
2
+ AKA_PATH="#{Dir.home}/.aka"
3
+ CONFIG_PATH="#{Dir.home}/.aka/.config"
4
+ BASH_PROFILE_PATH="#{Dir.home}/.bash_profile"
5
+ PROFILE_PATH="#{Dir.home}/.profile"
6
+ BASHRC_PATH="#{Dir.home}/.bashrc"
7
+ ZSHRC_PATH="#{Dir.home}/.zshrc"
8
+ end
@@ -0,0 +1,14 @@
1
+ module Aka
2
+ def self.reload_dot_file
3
+ isOhMyZsh == TRUE ? system("exec zsh") : system("kill -SIGUSR1 #{Process.ppid}")
4
+ end
5
+
6
+ def self.unalias_the value
7
+ if isOhMyZsh == TRUE
8
+ system("exec zsh")
9
+ else
10
+ system "echo '#{value}' > ~/sigusr1-args;"
11
+ system "kill -SIGUSR2 #{Process.ppid}"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,775 @@
1
+ module Aka
2
+ def self.product_content_array(content)
3
+ content.gsub!(/\r\n?/, "\n")
4
+ return content_array = content.split("\n")
5
+ end
6
+
7
+ def self.import(the_name)
8
+ if the_name == ""
9
+ array = get_all_aliases_from_proj_aka
10
+ repeated_system_call(array)
11
+ else
12
+ array = get_all_aliases_from_proj_aka(the_name)
13
+ repeated_system_call(array)
14
+ end
15
+ end
16
+
17
+ def self.export(arg, the_name, force)
18
+ array = export_group_aliases(arg)
19
+ if the_name != "load"
20
+ new_proj_aka = "#{the_name}"+".aka"
21
+ FileUtils.touch(new_proj_aka)
22
+ write_with_array_into_withprint(new_proj_aka, array)
23
+ else
24
+ if File.exist?('proj.aka')
25
+ if force
26
+ write_with_array_into_withprint('proj.aka', array)
27
+ else
28
+ exist_statement("proj.aka already exists. Use -f to recreate a proj.aka")
29
+ end
30
+ else
31
+ FileUtils.touch('proj.aka')
32
+ write_with_array_into_withprint('proj.aka', array)
33
+ end
34
+ end
35
+ end
36
+
37
+ def self.get_all_aliases_from_proj_aka str="proj.aka"
38
+ array = []
39
+ if content = File.open(str).read
40
+ content_array = product_content_array(content)
41
+ array = print_the_aliases_return_array(content_array)
42
+ end
43
+ return array
44
+ end
45
+
46
+ def self.setPath(path, value)
47
+ data = readYML("#{CONFIG_PATH}")
48
+ if data.has_key?(value) == TRUE
49
+ old_path = data[value]
50
+ data[value] = path
51
+ writeYML("#{CONFIG_PATH}", data)
52
+ puts "#{value} -> #{path}"
53
+ else
54
+ error_statement("--#{value} does not exist in #{CONFIG_PATH} ")
55
+ end
56
+ end
57
+
58
+ def self.reload_with_source
59
+ system "source #{readYML("#{CONFIG_PATH}")["dotfile"]}"
60
+ end
61
+
62
+ def self.readYML path
63
+ if File.exists? path
64
+ return YAML.load_file(path)
65
+ else
66
+ error_statement("#{CONFIG_PATH} does not exist. Type `aka setup` to setup the config file")
67
+ end
68
+ end
69
+
70
+ def self.writeYML path, theyml
71
+ File.open(path, 'w') {|f| f.write theyml.to_yaml } #Store
72
+ end
73
+
74
+ def self.write_with_array_into_withprint path, array
75
+ File.open(path, 'w') { |file|
76
+ file.write("#generated with https://rubygems.org/gems/aka2")
77
+ file.write("\n\n")
78
+
79
+ array.each do |line|
80
+ file.write(line)
81
+ file.write("\n")
82
+ end
83
+ }
84
+ end
85
+
86
+
87
+ def self.write_with_array_into path, array
88
+ File.open(path, 'w') { |file|
89
+ array.each do |line|
90
+ file.write(line)
91
+ file.write("\n")
92
+ end
93
+ }
94
+ end
95
+
96
+ def self.write_with_newline array
97
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
98
+ File.open(str, 'w') { |file|
99
+ array.each do |line|
100
+ file.write(line + "\n")
101
+ end
102
+ }
103
+ end
104
+
105
+ def self.write str, path
106
+ File.open(path, 'w') { |file| file.write(str) }
107
+ end
108
+
109
+ def self.append str, path
110
+ File.open(path, 'a') { |file| file.write(str) }
111
+ end
112
+
113
+ def self.split fulldomain
114
+ username = fulldomain.split("@").first
115
+ domain = fulldomain.split("@")[1].split(":").first
116
+ port = fulldomain.split("@")[1].split(":")[1]
117
+ return [username, domain, port]
118
+ end
119
+
120
+ def self.add_with_group input, name_of_group
121
+ if input && search_alias_return_alias_tokens(input).first == FALSE && not_empty_alias(input) == FALSE
122
+ array = input.split("=")
123
+ group_name = "# => #{name_of_group}"
124
+ full_command = "alias #{array.first}='#{array[1]}' #{group_name}".gsub("\n","") #remove new line in command
125
+ print_out_command = "aka g #{array.first}='#{array[1]}'"
126
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
127
+ File.open(str, 'a') { |file| file.write("\n" +full_command) }
128
+ create_statement "#{print_out_command} " + "in #{name_of_group} group."
129
+ return TRUE
130
+ else
131
+ puts "The alias is already present. Use 'aka -h' to see all the useful commands."
132
+ return FALSE
133
+ end
134
+ end
135
+
136
+ def self.add input
137
+ if input && search_alias_return_alias_tokens(input).first == FALSE && not_empty_alias(input) == FALSE
138
+ array = input.split("=")
139
+ full_command = "alias #{array.first}='#{array[1]}'".gsub("\n","") #remove new line in command
140
+ print_out_command = "aka g #{array.first}='#{array[1]}'"
141
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
142
+ File.open(str, 'a') { |file| file.write("\n" +full_command) }
143
+ create_statement "#{print_out_command}"
144
+ return TRUE
145
+ else
146
+ puts "The alias is already present. Use 'aka -h' to see all the useful commands."
147
+ return FALSE
148
+ end
149
+ end
150
+
151
+ def self.not_empty_alias input
152
+ array = input.split("=")
153
+ return TRUE if array.count < 2
154
+ return array[1].strip == ""
155
+ end
156
+
157
+ def self.search_alias_with_group_name name
158
+ print_title("System Alias")
159
+ group_count = 0
160
+ if name == "group"
161
+ name = "default"
162
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
163
+ if content = File.open(str).read
164
+ content_array = product_content_array(content)
165
+ group_count = print_the_aliases(content_array)
166
+ if group_count == 0
167
+ puts "No alias found in default group"
168
+ else
169
+ exist_statement("A total of #{group_count} aliases in default group.")
170
+ end
171
+ end
172
+ else
173
+
174
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
175
+ if content = File.open(str).read
176
+ content_array = product_content_array(content)
177
+ group_count = print_the_aliases2(content_array, name)
178
+ end
179
+
180
+ if group_count == 0
181
+ puts "No alias found in #{name} group"
182
+ else
183
+ exist_statement("A total of #{group_count} aliases in #{name} group.")
184
+ end
185
+ end
186
+ end
187
+
188
+ def self.change_alias_group_name_with input, new_group_name
189
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
190
+ if content = File.open(str).read
191
+ content_array = product_content_array(content)
192
+ content_array.each_with_index { |line, index|
193
+ value = line.split(" ")
194
+ if value.length > 1 && value.first == "alias"
195
+ aliasWithoutGroup = line.split("# =>").first.strip
196
+ answer = value[1].split("=") #contains the alias
197
+ if input == answer.first
198
+ alias_n_command = aliasWithoutGroup.split(" ").drop(1).join(" ")
199
+ containsCommand = alias_n_command.split('=') #containsCommand[1]
200
+ containsCommand[1].slice!(0) && containsCommand[1].slice!(containsCommand[1].length-1) if containsCommand[1] != nil && containsCommand[1][0] == "'" && containsCommand[1][containsCommand[1].length-1] == "'"
201
+ alias_n_command = answer.first+"="+containsCommand[1]
202
+ remove(answer.first)
203
+ result = add_with_group(alias_n_command,new_group_name)
204
+ reload_dot_file if result
205
+ end
206
+ end
207
+ }
208
+ else
209
+ puts "#{@pwd} cannot be found.".red
210
+ end
211
+ end
212
+
213
+ def self.get_group_name input
214
+ if input
215
+ if input.include? "# =>"
216
+ return input.split("# =>").last.strip
217
+ else
218
+ return nil
219
+ end
220
+ else
221
+ error_statement "There is no input"
222
+ end
223
+ end
224
+
225
+ def self.export_group_aliases name
226
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
227
+ results = []
228
+ if content = File.open(str).read
229
+ content_array = product_content_array(content)
230
+ results = print_the_aliases_return_array2(content_array, name)
231
+ end
232
+ return results
233
+ end
234
+
235
+ def self.search_alias_return_alias_tokens argument
236
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
237
+ if content = File.open(str).read
238
+ content_array = product_content_array(content)
239
+ content_array.each_with_index { |line, index|
240
+ line = line.gsub("# =>", "-g")
241
+ value = line.split(" ")
242
+ containsCommand = line.split('=') #containsCommand[1]
243
+ if value.length > 1 && value.first == "alias"
244
+ answer = value[1].split("=") #contains the alias
245
+ if found?(answer.first, argument.split("=").first, line) == TRUE
246
+ this_alias = answer.first
247
+ answer.slice!(0) #rmove the first
248
+ containsCommand[1].slice!(0) && containsCommand[1].slice!(containsCommand[1].length-1) if containsCommand[1] != nil && containsCommand[1][0] == "'" && containsCommand[1][containsCommand[1].length-1] == "'"
249
+ return [TRUE, this_alias, containsCommand[1]]
250
+ end
251
+ end
252
+ }
253
+ else
254
+ puts "#{@pwd} cannot be found.".red
255
+ return [FALSE, nil, nil]
256
+ end
257
+ return [FALSE, nil, nil]
258
+
259
+ end
260
+
261
+ def self.search_alias_return_alias_tokens_with_group argument
262
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
263
+ if content = File.open(str).read
264
+ content_array = product_content_array(content)
265
+ content_array.each_with_index { |line, index|
266
+ value = line.split(" ")
267
+ if value.length > 1 && value.first == "alias"
268
+ # templine = line.gsub("# =>", "-g")
269
+ templine = line.split("# =>").first
270
+ containsCommand = templine.split('=') #containsCommand[1]
271
+ group_name = get_group_name(line)
272
+ answer = value[1].split("=") #contains the alias
273
+ if found?(answer.first, argument.split("=").first, templine) == TRUE
274
+ this_alias = answer.first
275
+ answer.slice!(0) #rmove the first
276
+ containsCommand[1].strip!
277
+ containsCommand[1].slice!(0) && containsCommand[1].slice!(containsCommand[1].length-1) if containsCommand[1] != nil && containsCommand[1][0] == "'" && containsCommand[1][containsCommand[1].length-1] == "'"
278
+ return [TRUE, this_alias, containsCommand[1], group_name]
279
+ end
280
+ end
281
+ }
282
+ else
283
+ puts "#{@pwd} cannot be found.".red
284
+ return [FALSE, nil, nil, nil]
285
+ end
286
+ return [FALSE, nil, nil, nil]
287
+ end
288
+
289
+ def self.remove input
290
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
291
+ if content=File.open(str).read
292
+ content_array = product_content_array(content)
293
+ content_array.each_with_index { |line, index|
294
+ line = line.gsub("# =>", "-g")
295
+ value = line.split(" ")
296
+ if value.length > 1 && value.first == "alias"
297
+ answer = value[1].split("=")
298
+ if answer.first == input
299
+ content_array.delete_at(index) && write_with_newline(content_array)
300
+ print_out_command = "aka g #{input}=#{line.split("=")[1]}"
301
+ puts "Removed: ".red + "#{print_out_command}"
302
+ return TRUE
303
+ end
304
+ end
305
+ }
306
+
307
+ puts "#{input} cannot be found.".red
308
+ else
309
+ puts "#{@pwd} cannot be found.".red
310
+ return FALSE
311
+ end
312
+ end
313
+
314
+ def self.remove_autosource
315
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
316
+ if content=File.open(str).read
317
+ content_array = product_content_array(content)
318
+ content_array.each_with_index { |line, index|
319
+ if line == "source \"/home/ryan/.aka/autosource\""
320
+ content_array.delete_at(index) && write_with_newline(content_array)
321
+ puts "Removed: ".red + "source \"/home/ryan/.aka/autosource\""
322
+ return TRUE
323
+ end
324
+ }
325
+ else
326
+ error_statement("autosource cannot be found in dotfile.")
327
+ return FALSE
328
+ end
329
+ end
330
+
331
+ def self.history
332
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["history"])
333
+ if content = File.open(str).read
334
+ puts ".bash_history is available"
335
+ count=0
336
+ content_array = product_content_array(content)
337
+ content_array.each_with_index { |line, index|
338
+ array = line.split(" ")
339
+ if array.first == "alias"
340
+ count += 1
341
+ end
342
+ puts "#{index+1} #{line}"
343
+ }
344
+ puts "There are #{count} lines of history."
345
+ else
346
+ error_statement(".bash_history is not available")
347
+ end
348
+ end
349
+
350
+ def self.found? answer, argument, line
351
+ if answer == argument
352
+ exist_statement(" aka g #{argument}=#{line.split('=')[1]}")
353
+ return TRUE
354
+ else
355
+ return FALSE
356
+ end
357
+ end
358
+
359
+ def self.edit_alias_command newcommand, this_alias
360
+ Aka.remove(this_alias) #remove that alias
361
+
362
+ edit_statement "aka g #{this_alias}='#{newcommand}'"
363
+ return append("alias " + this_alias + "='" + newcommand + "'", readYML("#{CONFIG_PATH}")["dotfile"] )
364
+ end
365
+
366
+ def self.edit_alias_command_with_group newcommand, this_alias, group
367
+ if !group.nil? || !group.empty?
368
+ edit_statement("aka g #{this_alias}='#{newcommand}' -g #{group}")
369
+ return append("alias " + this_alias + "='" + newcommand + "' # => " + group, readYML("#{CONFIG_PATH}")["dotfile"] )
370
+ else
371
+ edit_statement("aka g #{this_alias}='#{newcommand}'")
372
+ return append("alias " + this_alias + "='" + newcommand + "'", readYML("#{CONFIG_PATH}")["dotfile"] )
373
+ end
374
+ end
375
+
376
+ def self.edit_alias_name newalias, thiscommand
377
+ Aka.remove(thiscommand) #remove that alias
378
+ edit_statement("aka g #{newalias}='#{thiscommand}'")
379
+ return append("alias " + newalias + "='" + thiscommand + "'", readYML("#{CONFIG_PATH}")["dotfile"] )
380
+ end
381
+
382
+ def self.edit_alias_name_with_group newalias, thiscommand, group
383
+ if !group.nil? || !group.empty?
384
+ edit_statement "aka g #{newalias}='#{thiscommand}' -g #{group}"
385
+ return append("alias " + newalias + "='" + thiscommand + "' # => " + group, readYML("#{CONFIG_PATH}")["dotfile"] )
386
+ else
387
+ edit_statement "aka g #{newalias}='#{thiscommand}'"
388
+ return append("alias " + newalias + "=" + thiscommand, readYML("#{CONFIG_PATH}")["dotfile"] )
389
+ end
390
+ end
391
+
392
+ def self.count_groups
393
+ group_counts = 0
394
+ group_array = []
395
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
396
+ if content=File.open(str).read
397
+ content_array = product_content_array(content)
398
+ content_array.each_with_index { |line, index|
399
+ value = line.split(" ")
400
+ if value.length > 1 && value.first == 'alias'
401
+ answer = value[1].split("=") #contains the alias
402
+ group_name = line.scan(/# => ([a-zA-z]*)/).first if line.scan(/# => ([a-zA-z]*)/)
403
+ group_array.push(group_name) if group_name != nil
404
+ end
405
+ }
406
+ return group_array.uniq.count
407
+ end
408
+ end
409
+
410
+ def self.count_function
411
+ function_count = 0
412
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
413
+ if content=File.open(str).read
414
+ content_array = product_content_array(content)
415
+ content_array.each_with_index { |line, index|
416
+ value = line.split(" ")
417
+ if value.length > 1 && value.first == "function"
418
+ answer = value[1].split("=")
419
+ function_count += 1
420
+ end
421
+ }
422
+ return function_count
423
+ end
424
+ end
425
+
426
+ def self.count_export
427
+ export_count = 0
428
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
429
+ if content=File.open(str).read
430
+ content_array = product_content_array(content)
431
+ content_array.each_with_index { |line, index|
432
+ value = line.split(" ")
433
+ if value.length > 1 && value.first == "export"
434
+ answer = value[1].split("=")
435
+ export_count += 1
436
+ end
437
+ }
438
+ return export_count
439
+ end
440
+ end
441
+
442
+ def self.count
443
+ alias_count = 0
444
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
445
+ if content=File.open(str).read
446
+ content_array = product_content_array(content)
447
+ content_array.each_with_index { |line, index|
448
+ value = line.split(" ")
449
+ if value.length > 1 && value.first == "alias"
450
+ answer = value[1].split("=")
451
+ alias_count += 1
452
+ end
453
+ }
454
+ return alias_count
455
+ end
456
+ end
457
+
458
+ def self.read location
459
+ answer = File.exist?(location)
460
+ answer == TRUE && content = File.open(location).read ? content : ""
461
+ end
462
+
463
+ def self.is_config_file_present? str
464
+ path = "#{BASH_PROFILE_PATH}"
465
+ if str == ""
466
+ error_statement("Type `aka init --dotfile #{path}` to set the path to your dotfile. \nReplace .bash_profile with .bashrc or .zshrc if you are not using bash.")
467
+ exit
468
+ end
469
+
470
+ if !File.exists?(str)
471
+ error_statement("Type `aka init --dotfile #{path}` to set the path of your dotfile. \nReplace .bash_profile with .bashrc or .zshrc if you are not using bash.")
472
+ exit
473
+ end
474
+ return str
475
+ end
476
+
477
+ def self.showlast(list_number=FALSE,howmany=10, showGroup)
478
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
479
+ if content = File.open(str).read
480
+ content_array = product_content_array(content)
481
+ #why not just call the last five lines? Because i can't be sure that they are aliases
482
+ total_aliases = []
483
+ content_array.each_with_index { |line, index|
484
+ value = line.split(" ")
485
+ if value.length > 1 && value.first == "alias"
486
+ total_aliases.push(line)
487
+ end
488
+ }
489
+ puts ""
490
+ if total_aliases.count > howmany #if there is enogh alias
491
+ total_aliases.last(howmany).each_with_index do |line, index|
492
+ line = line.gsub("# =>", "-g")
493
+ splitted= line.split('=')
494
+ if list_number
495
+ puts "#{total_aliases.count - howmany + index+1}. aka g " + splitted[0].split(" ")[1].red + "=" + splitted[1]
496
+ else
497
+ puts "aka g " + splitted[0].split(" ")[1].red + "=" + splitted[1]
498
+ end
499
+ end
500
+ else #if there is not enough alias
501
+ total_aliases.last(howmany).each_with_index do |line, index|
502
+ line = line.gsub("# =>", "-g")
503
+ splitted= line.split('=')
504
+ if list_number
505
+ puts "#{index+1}. aka g " + splitted[0].split(" ")[1].red + "=" + splitted[1]
506
+ else
507
+ puts "aka g " + splitted[0].split(" ")[1].red + "=" + splitted[1]
508
+ end
509
+ end
510
+ end
511
+ puts ""
512
+ end
513
+ end
514
+
515
+ def self.showUsage howmany=10, least=FALSE
516
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["history"])
517
+ value = reload_dot_file
518
+ #get all aliases
519
+ if content = File.open(str).read
520
+ content_array = product_content_array(content)
521
+ total_aliases = []
522
+ content_array.each_with_index { |line, index|
523
+ value = line.split(" ")
524
+ total_aliases.push(value.first)
525
+ }
526
+ count_aliases(total_aliases, howmany, least)
527
+ end
528
+ end
529
+
530
+ def self.count_aliases array, howmany, least=FALSE
531
+ name_array,count_array = [], []
532
+ array.each_with_index { |value, index|
533
+ if name_array.include?(value) == FALSE
534
+ name_array.push(value)
535
+ end
536
+ }
537
+ #count the value
538
+ name_array.each { |unique_value|
539
+ count = 0
540
+ array.each { |value|
541
+ if (unique_value == value)
542
+ count+=1
543
+ end
544
+ }
545
+ count_array.push(count)
546
+ }
547
+
548
+ sorted_count_array, sorted_name_array = sort(count_array, name_array)
549
+
550
+ #display the least used aliases
551
+ if least == TRUE
552
+ if sorted_count_array.count == sorted_name_array.count
553
+ puts ""
554
+ sorted_name_array.last(howmany).each_with_index { |value, index|
555
+ percent = ((sorted_count_array[sorted_name_array.last(howmany).size + index]).round(2)/array.size.round(2))*100
556
+ str = "#{sorted_name_array.size-sorted_name_array.last(howmany).size+index+1}. #{value}"
557
+ puts "#{str} #{showSpace(str)} #{showBar(percent)}"
558
+ }
559
+ puts ""
560
+ else
561
+ puts "Something went wrong: count_array.count = #{sorted_count_array.count}\n
562
+ name_array.count = #{sorted_name_array.count}. Please check your .bash_history.".pretty
563
+ end
564
+ else
565
+ # #print out
566
+ if sorted_count_array.count == sorted_name_array.count
567
+ puts ""
568
+ sorted_name_array.first(howmany).each_with_index { |value, index|
569
+ percent = ((sorted_count_array[index]).round(2)/array.size.round(2))*100
570
+ str = "#{index+1}. #{value}"
571
+ puts "#{str} #{showSpace(str)} #{showBar(percent)}"
572
+ }
573
+ puts ""
574
+ else
575
+ puts "Something went wrong: count_array.count = #{sorted_count_array.count}\n
576
+ name_array.count = #{sorted_name_array.count}. Please check your .bash_history.".pretty
577
+ end
578
+ end
579
+ puts "There's a total of #{array.size} lines in #{readYML("#{CONFIG_PATH}")["history"]}."
580
+ end
581
+
582
+ def self.sort(countarray, namearray) #highest first. decscending.
583
+ temp = 0, temp2 = ""
584
+ countarray.each_with_index { |count, index|
585
+ countarray[0..countarray.size-index].each_with_index { |x, thisindex| #always one less than total
586
+
587
+ if index < countarray.size-1 && thisindex < countarray.size-1
588
+ if countarray[thisindex] < countarray[thisindex+1] #if this count is less than next count
589
+ temp = countarray[thisindex]
590
+ countarray[thisindex] = countarray[thisindex+1]
591
+ countarray[thisindex+1] = temp
592
+
593
+ temp2 = namearray[thisindex]
594
+ namearray[thisindex] = namearray[thisindex+1]
595
+ namearray[thisindex+1] = temp2
596
+ end
597
+ end
598
+
599
+ }
600
+ }#outer loop
601
+ return countarray, namearray
602
+ end
603
+
604
+ def self.cleanup
605
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
606
+ if content = File.open(str).read
607
+ content_array = product_content_array(content)
608
+ check = FALSE
609
+ while check == FALSE
610
+ check = TRUE
611
+ content_array.each_with_index { |line, index|
612
+ if line == "" || line == "\n"
613
+ content_array.delete_at(index)
614
+ check = FALSE
615
+ end
616
+ }
617
+ end
618
+ write_with_newline(content_array)
619
+ end
620
+ end
621
+
622
+ def self.list_all_groups_in_proj_aka
623
+ Aka.print_title("Project Groups")
624
+ str = 'proj.aka'
625
+ group_array = []
626
+ if content=File.open(str).read
627
+ content_array = product_content_array(content)
628
+ content_array.each_with_index { |line, index|
629
+ value = line.split(" ")
630
+ if value.length > 1 && value.first == 'alias'
631
+ answer = value[1].split("=") #contains the alias
632
+ group_name = line.scan(/# => ([a-zA-z]*)/).first if line.scan(/# => ([a-zA-z]*)/)
633
+ if group_name != nil
634
+ group_array.push(group_name)
635
+ end
636
+ end
637
+ }
638
+
639
+ puts group_array.uniq
640
+
641
+ puts ""
642
+ puts "A total of #{group_array.uniq.count} groups from #{Dir.pwd}/proj.aka"
643
+ puts ""
644
+
645
+ end
646
+ end
647
+
648
+ def self.list_all_groups
649
+ Aka.print_title("System Groups")
650
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
651
+ group_array = []
652
+ if content=File.open(str).read
653
+ content_array = product_content_array(content)
654
+ content_array.each_with_index { |line, index|
655
+ value = line.split(" ")
656
+ if value.length > 1 && value.first == 'alias'
657
+
658
+ answer = value[1].split("=") #contains the alias
659
+ group_name = line.scan(/# => ([a-zA-z]*)/).first if line.scan(/# => ([a-zA-z]*)/)
660
+ if group_name != nil
661
+ group_array.push(group_name)
662
+ end
663
+ end
664
+ }
665
+
666
+ puts group_array.uniq
667
+
668
+ puts ""
669
+ puts "A total of #{group_array.uniq.count} groups from #{readYML("#{CONFIG_PATH}")["dotfile"]}"
670
+ puts ""
671
+
672
+ end
673
+ end
674
+
675
+ def self.add_last_command name
676
+ command = ""
677
+ str = is_config_file_present?(readYML("#{CONFIG_PATH}")["history"])
678
+ #i think if you do history -w, you can retrieve the latest command
679
+ if content = File.open(str).read
680
+ count=0
681
+ content_array = product_content_array(content)
682
+ command = content_array[content_array.count - 1]
683
+ end
684
+ return str = name + "=" + "#{command}"
685
+ end
686
+
687
+ def self.parseARGS str
688
+ array = str.split(" ")
689
+ array.each_with_index do |line, value|
690
+ array[value] = line.gsub('#{pwd}', Shellwords.escape(Dir.pwd))
691
+ end
692
+ return array.join(" ")
693
+ end
694
+
695
+
696
+ def self.repeated_system_call array
697
+ array.each do |line|
698
+ line.gsub!("\'", "\"") #need to replace ' with "
699
+ line = line + " -n" #do not reload :)
700
+ system(line)
701
+ end
702
+ end
703
+
704
+ def self.print_the_aliases_return_array2 content_array, name
705
+ array = []
706
+ content_array.each_with_index { |line, index|
707
+ testline = line
708
+ line = line.gsub("# =>", "-g")
709
+ value = testline.split(" ")
710
+ containsCommand = line.split('=') #containsCommand[1]
711
+ if value.length > 1 && value.first == "alias"
712
+ answer = value[1].split("=") #contains the alias
713
+ group_name = testline.scan(/# => ([a-zA-z]*)/).first if testline.scan(/# => ([a-zA-z]*)/)
714
+ if group_name != nil && group_name.first == name
715
+ array.push(testline)
716
+ end
717
+ end
718
+ }
719
+ return array
720
+ end
721
+
722
+ def self.print_the_aliases_return_array content_array
723
+ array = []
724
+ content_array.each_with_index { |line, index|
725
+ testline = line
726
+ line = line.gsub("# =>", "-g")
727
+ value = testline.split(" ")
728
+ containsCommand = line.split('=') #containsCommand[1]
729
+ if value.length > 1 && value.first == "alias"
730
+ answer = value[1].split("=") #contains the alias
731
+ group_name = testline.scan(/# => ([a-zA-z]*)/).first if testline.scan(/# => ([a-zA-z]*)/)
732
+ containsCommand[1].slice!(0) && containsCommand[1].slice!(containsCommand[1].length-1) if containsCommand[1] != nil && containsCommand[1][0] == "'" && containsCommand[1][containsCommand[1].length-1] == "'"
733
+ array.push("aka g " + "#{answer.first}" + "=#{containsCommand[1]}")
734
+ end
735
+ }
736
+ return array
737
+ end
738
+
739
+ def self.print_the_aliases2 content_array, name
740
+ answer_count= 0
741
+ content_array.each_with_index { |line, index|
742
+ testline = line
743
+ line = line.gsub("# =>", "-g")
744
+ value = testline.split(" ")
745
+ containsCommand = line.split('=') #containsCommand[1]
746
+ if value.length > 1 && value.first == "alias"
747
+ answer = value[1].split("=") #contains the alias
748
+ group_name = testline.scan(/# => ([a-zA-z]*)/).first if testline.scan(/# => ([a-zA-z]*)/)
749
+ if group_name != nil && group_name.first == name
750
+ containsCommand[1].slice!(0) && containsCommand[1].slice!(containsCommand[1].length-1) if containsCommand[1] != nil && containsCommand[1][0] == "'" && containsCommand[1][containsCommand[1].length-1] == "'"
751
+ puts "aka g " + "#{answer.first}".red + "=#{containsCommand[1]}"
752
+ answer_count += 1
753
+ end
754
+ end
755
+ }
756
+ return answer_count
757
+ end
758
+
759
+ def self.print_the_aliases content_array
760
+ answer_count= 0
761
+ content_array.each_with_index { |line, index|
762
+ line = line.gsub("# =>", "-g")
763
+ value = line.split(" ")
764
+ containsCommand = line.split('=') #containsCommand[1]
765
+ answer = value[1].split("=") #contains the alias
766
+ group_name = line.scan(/# => ([a-zA-z]*)/).first if line.scan(/# => ([a-zA-z]*)/)
767
+ if value.length > 1 && value.first == 'alias'
768
+ containsCommand[1].slice!(0) && containsCommand[1].slice!(containsCommand[1].length-1) if containsCommand[1] != nil && containsCommand[1][0] == "'" && containsCommand[1][containsCommand[1].length-1] == "'"
769
+ puts "aka g " + "#{answer.first}".red + "=#{containsCommand[1]}"
770
+ answer_count+= 1
771
+ end
772
+ }
773
+ return answer_count
774
+ end
775
+ end