Module: Aka

Defined in:
lib/aka/printing.rb,
lib/aka.rb,
lib/aka/core.rb,
lib/aka/config.rb,
lib/aka/string.rb,
lib/aka/helpers.rb,
lib/aka/version.rb,
lib/aka/constants.rb

Overview

Bryan Lim (@ytbryan) MIT License github.com/ytbryan twitter.com/ytbryan [email protected] || [email protected]

Defined Under Namespace

Classes: Base

Constant Summary collapse

VERSION =
"0.1.10"
AKA_PATH =
"#{Dir.home}/.aka"
CONFIG_PATH =
"#{Dir.home}/.aka/.config"
BASH_PROFILE_PATH =
"#{Dir.home}/.bash_profile"
PROFILE_PATH =
"#{Dir.home}/.profile"
BASHRC_PATH =
"#{Dir.home}/.bashrc"
ZSHRC_PATH =
"#{Dir.home}/.zshrc"

Class Method Summary collapse

Class Method Details

.add(input) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/aka/helpers.rb', line 155

def self.add input
  if input && search_alias_return_alias_tokens(input).first == false && not_empty_alias(input) == false
    array = input.split("=")
    full_command = "alias #{array.first}='#{array[1]}'".gsub("\n","") #remove new line in command
    print_out_command = "aka g #{array.first}='#{array[1]}'"
    str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
    File.open(str, 'a') { |file| file.write("\n" +full_command) }
    create_statement "#{print_out_command}"
    return true
  else
    puts "The alias is already present. Use 'aka -h' to see all the useful commands."
    return false
  end
end

.add_a_function(input, name_of_group) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/aka/helpers.rb', line 120

def self.add_a_function input, name_of_group
  if input && search_alias_return_alias_tokens(input).first == false && not_empty_alias(input) == false
    array = input.split("=")
    group_name = "# => #{name_of_group}"

    full_command = "function #{array.first}(){ #{array[1]} } #{group_name}".gsub("\n","") #remove new line in command

    # full_command = "alias #{array.first}='#{array[1]}' #{group_name}".gsub("\n","") #remove new line in command
    print_out_command = "aka g #{array.first}='#{array[1]}'"
    str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
    File.open(str, 'a') { |file| file.write("\n" +full_command) }
    create_statement "#{print_out_command} " + "in #{name_of_group} group."
    return true
  else
    puts "The alias is already present. Use 'aka -h' to see all the useful commands."
    return false
  end
end

.add_last_command(name) ⇒ Object



694
695
696
697
698
699
700
701
702
703
704
# File 'lib/aka/helpers.rb', line 694

def self.add_last_command name
  command = ""
  str = is_config_file_present?(readYML("#{CONFIG_PATH}")["history"])
  #i think if you do history -w, you can retrieve the latest command
  if content = File.open(str).read
    count=0
    content_array = product_content_array(content)
    command =  content_array[content_array.count - 1]
  end
  return str = name + "=" + "#{command}"
end

.add_with_group(input, name_of_group) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/aka/helpers.rb', line 139

def self.add_with_group input, name_of_group
  if input && search_alias_return_alias_tokens(input).first == false && not_empty_alias(input) == false
    array = input.split("=")
    group_name = "# => #{name_of_group}"
    full_command = "alias #{array.first}='#{array[1]}' #{group_name}".gsub("\n","") #remove new line in command
    print_out_command = "aka g #{array.first}='#{array[1]}'"
    str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
    File.open(str, 'a') { |file| file.write("\n" +full_command) }
    create_statement "#{print_out_command} " + "in #{name_of_group} group."
    return true
  else
    puts "The alias is already present. Use 'aka -h' to see all the useful commands."
    return false
  end
end

.append(str, path) ⇒ Object



109
110
111
# File 'lib/aka/helpers.rb', line 109

def self.append str, path
  File.open(path, 'a') { |file| file.write(str) }
end

.change_alias_group_name_with(input, new_group_name) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/aka/helpers.rb', line 207

def self.change_alias_group_name_with input, new_group_name
  str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
  if content = File.open(str).read
    content_array = product_content_array(content)
    content_array.each_with_index { |line, index|
      value = line.split(" ")
      if value.length > 1 && value.first == "alias"
        aliasWithoutGroup = line.split("# =>").first.strip
        answer = value[1].split("=") #contains the alias
        if input == answer.first
          alias_n_command = aliasWithoutGroup.split(" ").drop(1).join(" ")
          containsCommand = alias_n_command.split('=') #containsCommand[1]
          containsCommand[1].slice!(0) && containsCommand[1].slice!(containsCommand[1].length-1) if containsCommand[1] != nil && containsCommand[1][0] == "'" && containsCommand[1][containsCommand[1].length-1] == "'"
          alias_n_command = answer.first+"="+containsCommand[1]
          remove(answer.first)
          result = add_with_group(alias_n_command,new_group_name)
          reload_dot_file if result
        end
      end
    }
  else
    puts "#{@pwd} cannot be found.".red
  end
end

.cleanupObject



623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
# File 'lib/aka/helpers.rb', line 623

def self.cleanup
  str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
  if content = File.open(str).read
    content_array = product_content_array(content)
    check = false
    while check == false
      check = true
      content_array.each_with_index { |line, index|
        if line == "" || line == "\n"
          content_array.delete_at(index)
          check = false
        end
      }
    end
    write_with_newline(content_array)
  end
end

.countObject



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/aka/helpers.rb', line 461

def self.count
  alias_count = 0
  str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
  if content=File.open(str).read
    content_array = product_content_array(content)
    content_array.each_with_index { |line, index|
      value = line.split(" ")
      if value.length > 1 && value.first == "alias"
        answer = value[1].split("=")
        alias_count += 1
      end
    }
    return alias_count
  end
end

.count_aliases(array, howmany, least = false) ⇒ Object



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# File 'lib/aka/helpers.rb', line 549

def self.count_aliases array, howmany, least=false
  name_array,count_array = [], []
  array.each_with_index { |value, index|
    if name_array.include?(value) == false
      name_array.push(value)
    end
  }
  #count the value
  name_array.each { |unique_value|
    count = 0
    array.each { |value|
      if (unique_value == value)
        count+=1
      end
    }
    count_array.push(count)
  }

  sorted_count_array, sorted_name_array = sort(count_array, name_array)

  #display the least used aliases
  if least == true
    if sorted_count_array.count == sorted_name_array.count
      puts ""
      sorted_name_array.last(howmany).each_with_index { |value, index|
        percent = ((sorted_count_array[sorted_name_array.last(howmany).size + index]).round(2)/array.size.round(2))*100
        str = "#{sorted_name_array.size-sorted_name_array.last(howmany).size+index+1}. #{value}"
        puts "#{str} #{showSpace(str)} #{showBar(percent)}"
      }
      puts ""
    else
      puts "Something went wrong: count_array.count = #{sorted_count_array.count}\n
      name_array.count = #{sorted_name_array.count}. Please check your .bash_history.".pretty
    end
  else
    # #print out
    if sorted_count_array.count == sorted_name_array.count
      puts ""
      sorted_name_array.first(howmany).each_with_index { |value, index|
        percent = ((sorted_count_array[index]).round(2)/array.size.round(2))*100
        str = "#{index+1}. #{value}"
        puts "#{str} #{showSpace(str)} #{showBar(percent)}"
      }
      puts ""
    else
      puts "Something went wrong: count_array.count = #{sorted_count_array.count}\n
            name_array.count = #{sorted_name_array.count}. Please check your .bash_history.".pretty
    end
  end
  puts "There's a total of #{array.size} lines in #{readYML("#{CONFIG_PATH}")["history"]}."
end

.count_exportObject



445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/aka/helpers.rb', line 445

def self.count_export
  export_count = 0
  str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
  if content=File.open(str).read
    content_array = product_content_array(content)
    content_array.each_with_index { |line, index|
      value = line.split(" ")
      if value.length > 1 && value.first == "export"
        answer = value[1].split("=")
        export_count += 1
      end
    }
    return export_count
  end
end

.count_functionObject



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/aka/helpers.rb', line 429

def self.count_function
  function_count = 0
  str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
  if content=File.open(str).read
    content_array = product_content_array(content)
    content_array.each_with_index { |line, index|
      value = line.split(" ")
      if value.length > 1 && value.first == "function"
        answer = value[1].split("=")
        function_count += 1
      end
    }
    return function_count
  end
end

.count_groupsObject



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/aka/helpers.rb', line 411

def self.count_groups
  group_counts = 0
  group_array = []
  str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
  if content=File.open(str).read
    content_array = product_content_array(content)
    content_array.each_with_index { |line, index|
      value = line.split(" ")
      if value.length > 1 && value.first == 'alias'
        answer = value[1].split("=") #contains the alias
        group_name = line.scan(/# => ([a-zA-z]*)/).first if line.scan(/# => ([a-zA-z]*)/)
        group_array.push(group_name) if group_name != nil
      end
    }
    return group_array.uniq.count
  end
end

.create_statement(statement) ⇒ Object



38
39
40
# File 'lib/aka/printing.rb', line 38

def self.create_statement(statement)
  puts "Created: ".green +  statement
end

.edit_alias_command(newcommand, this_alias) ⇒ Object



378
379
380
381
382
383
# File 'lib/aka/helpers.rb', line 378

def self.edit_alias_command newcommand, this_alias
  Aka.remove(this_alias) #remove that alias

  edit_statement "aka g #{this_alias}='#{newcommand}'"
  return append("alias " + this_alias + "='" + newcommand + "'", readYML("#{CONFIG_PATH}")["dotfile"] )
end

.edit_alias_command_with_group(newcommand, this_alias, group) ⇒ Object



385
386
387
388
389
390
391
392
393
# File 'lib/aka/helpers.rb', line 385

def self.edit_alias_command_with_group newcommand, this_alias, group
  if !group.nil? || !group.empty?
    edit_statement("aka g #{this_alias}='#{newcommand}' -g #{group}")
    return append("alias " + this_alias + "='" + newcommand + "' # => " + group, readYML("#{CONFIG_PATH}")["dotfile"] )
  else
    edit_statement("aka g #{this_alias}='#{newcommand}'")
    return append("alias " + this_alias + "='" + newcommand + "'", readYML("#{CONFIG_PATH}")["dotfile"] )
  end
end

.edit_alias_name(newalias, thiscommand) ⇒ Object



395
396
397
398
399
# File 'lib/aka/helpers.rb', line 395

def self.edit_alias_name newalias, thiscommand
  Aka.remove(thiscommand) #remove that alias
  edit_statement("aka g #{newalias}='#{thiscommand}'")
  return append("alias " + newalias + "='" + thiscommand + "'", readYML("#{CONFIG_PATH}")["dotfile"] )
end

.edit_alias_name_with_group(newalias, thiscommand, group) ⇒ Object



401
402
403
404
405
406
407
408
409
# File 'lib/aka/helpers.rb', line 401

def self.edit_alias_name_with_group newalias, thiscommand, group
  if !group.nil? || !group.empty?
    edit_statement "aka g #{newalias}='#{thiscommand}' -g #{group}"
    return append("alias " + newalias + "='" + thiscommand + "' # => " + group, readYML("#{CONFIG_PATH}")["dotfile"] )
  else
    edit_statement "aka g #{newalias}='#{thiscommand}'"
    return append("alias " + newalias + "=" + thiscommand, readYML("#{CONFIG_PATH}")["dotfile"] )
  end
end

.edit_statement(statement) ⇒ Object



34
35
36
# File 'lib/aka/printing.rb', line 34

def self.edit_statement(statement)
  puts "Edited:  ".yellow + statement
end

.error_statement(statement) ⇒ Object



19
20
21
# File 'lib/aka/printing.rb', line 19

def self.error_statement(statement)
  puts "Error: ".red + statement
end

.exist_statement(statement) ⇒ Object



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

def self.exist_statement(statement)
  puts "Exist: ".green + statement
end

.export(arg, the_name, force) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/aka/helpers.rb', line 17

def self.export(arg, the_name, force)
  array = export_group_aliases(arg)
  if the_name != "load"
    new_proj_aka = "#{the_name}"+".aka"
    FileUtils.touch(new_proj_aka)
    write_with_array_into_withprint(new_proj_aka, array)
  else
    if File.exist?('proj.aka')
      if force
        write_with_array_into_withprint('proj.aka', array)
      else
        exist_statement("proj.aka already exists. Use -f to recreate a proj.aka")
      end
    else
      FileUtils.touch('proj.aka')
      write_with_array_into_withprint('proj.aka', array)
    end
  end
end

.export_group_aliases(name) ⇒ Object



244
245
246
247
248
249
250
251
252
# File 'lib/aka/helpers.rb', line 244

def self.export_group_aliases name
  str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
  results = []
  if content = File.open(str).read
    content_array = product_content_array(content)
    results = print_the_aliases_return_array2(content_array, name)
  end
  return results
end

.found?(answer, argument, line) ⇒ Boolean

Returns:

  • (Boolean)


369
370
371
372
373
374
375
376
# File 'lib/aka/helpers.rb', line 369

def self.found? answer, argument, line
  if answer == argument
    exist_statement(" aka g #{argument}=#{line.split('=')[1]}")
    return true
  else
    return false
  end
end

.get_all_aliases_from_proj_aka(str = "proj.aka") ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/aka/helpers.rb', line 37

def self.get_all_aliases_from_proj_aka str="proj.aka"
  array = []
  if content = File.open(str).read
    content_array = product_content_array(content)
    array = print_the_aliases_return_array(content_array)
  end
  return array
end

.get_group_name(input) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/aka/helpers.rb', line 232

def self.get_group_name input
  if input
    if input.include? "# =>"
      return input.split("# =>").last.strip
    else
      return nil
    end
  else
    error_statement "There is no input"
  end
end

.historyObject



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/aka/helpers.rb', line 350

def self.history
  str = is_config_file_present?(readYML("#{CONFIG_PATH}")["history"])
  if content = File.open(str).read
    puts ".bash_history is available"
    count=0
    content_array = product_content_array(content)
    content_array.each_with_index { |line, index|
      array = line.split(" ")
      if array.first == "alias"
        count += 1
      end
      puts "#{index+1} #{line}"
    }
    puts "There are #{count} lines of history."
  else
    error_statement(".bash_history is not available")
  end
end

.import(the_name) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/aka/helpers.rb', line 7

def self.import(the_name)
  if the_name == ""
    array = get_all_aliases_from_proj_aka
    repeated_system_call(array)
  else
    array = get_all_aliases_from_proj_aka(the_name)
    repeated_system_call(array)
  end
end

.is_config_file_present?(str) ⇒ Boolean

Returns:

  • (Boolean)


482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/aka/helpers.rb', line 482

def self.is_config_file_present? str
  path =  "#{BASH_PROFILE_PATH}"
  if str == ""
    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.")
    exit
  end

  if !File.exists?(str)
    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.")
    exit
  end
  return str
end

.isOhMyZshObject



166
167
168
# File 'lib/aka/config.rb', line 166

def self.isOhMyZsh
  readYML("#{CONFIG_PATH}")["dotfile"] == "#{ZSHRC_PATH}" ? true : false
end

.list_all_groupsObject



667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'lib/aka/helpers.rb', line 667

def self.list_all_groups
  Aka.print_title("System Groups")
  str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
  group_array = []
  if content=File.open(str).read
    content_array = product_content_array(content)
    content_array.each_with_index { |line, index|
      value = line.split(" ")
      if value.length > 1 && value.first == 'alias'

        answer = value[1].split("=") #contains the alias
        group_name = line.scan(/# => ([a-zA-z]*)/).first if line.scan(/# => ([a-zA-z]*)/)
        if group_name != nil
          group_array.push(group_name)
        end
      end
    }

    puts group_array.uniq

    puts ""
    puts "A total of #{group_array.uniq.count} groups from #{readYML("#{CONFIG_PATH}")["dotfile"]}"
    puts ""

  end
end

.list_all_groups_in_proj_akaObject



641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
# File 'lib/aka/helpers.rb', line 641

def self.list_all_groups_in_proj_aka
  Aka.print_title("Project Groups")
  str = 'proj.aka'
  group_array = []
  if content=File.open(str).read
    content_array = product_content_array(content)
    content_array.each_with_index { |line, index|
      value = line.split(" ")
      if value.length > 1 && value.first == 'alias'
        answer = value[1].split("=") #contains the alias
        group_name = line.scan(/# => ([a-zA-z]*)/).first if line.scan(/# => ([a-zA-z]*)/)
        if group_name != nil
          group_array.push(group_name)
        end
      end
    }

    puts group_array.uniq

    puts ""
    puts "A total of #{group_array.uniq.count} groups from #{Dir.pwd}/proj.aka"
    puts ""

  end
end

.not_empty_alias(input) ⇒ Object



170
171
172
173
174
# File 'lib/aka/helpers.rb', line 170

def self.not_empty_alias input
  array = input.split("=")
  return true if array.count < 2
  return array[1].strip == ""
end

.parseARGS(str) ⇒ Object



706
707
708
709
710
711
712
# File 'lib/aka/helpers.rb', line 706

def self.parseARGS str
  array =  str.split(" ")
  array.each_with_index do |line, value|
    array[value] = line.gsub('#{pwd}', Shellwords.escape(Dir.pwd))
  end
  return array.join(" ")
end


14
15
16
17
# File 'lib/aka/printing.rb', line 14

def self.print_all_helpful_statement
  puts "A total of #{count()} aliases, #{count_groups} groups, #{count_export} exports and #{count_function} functions from #{readYML("#{CONFIG_PATH}")["dotfile"]}"
  puts "\nUse 'aka -h' to see all the useful commands.\n\n"
end


23
24
25
26
# File 'lib/aka/printing.rb', line 23

def self.print_helpful_statement total_aliases
  puts "\nA total of  #{total_aliases} aliases in this project #{Dir.pwd}"
  puts "\nUse 'aka -h' to see all the useful commands."
end


778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
# File 'lib/aka/helpers.rb', line 778

def self.print_the_aliases content_array
  answer_count= 0
  content_array.each_with_index { |line, index|
    line = line.gsub("# =>", "-g")
    value = line.split(" ")
    containsCommand = line.split('=') #containsCommand[1]
    answer = value[1].split("=") #contains the alias
    group_name = line.scan(/# => ([a-zA-z]*)/).first if line.scan(/# => ([a-zA-z]*)/)
    if value.length > 1 && value.first == 'alias'
      containsCommand[1].slice!(0) &&  containsCommand[1].slice!(containsCommand[1].length-1) if containsCommand[1] != nil && containsCommand[1][0] == "'" && containsCommand[1][containsCommand[1].length-1] == "'"
      puts "aka g " + "#{answer.first}".red + "=#{containsCommand[1]}"
      answer_count+= 1
    end
  }
  return answer_count
end


758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
# File 'lib/aka/helpers.rb', line 758

def self.print_the_aliases2 content_array, name
  answer_count= 0
  content_array.each_with_index { |line, index|
    testline = line
    line = line.gsub("# =>", "-g")
    value = testline.split(" ")
    containsCommand = line.split('=') #containsCommand[1]
    if value.length > 1 && value.first == "alias"
      answer = value[1].split("=") #contains the alias
      group_name = testline.scan(/# => ([a-zA-z]*)/).first if testline.scan(/# => ([a-zA-z]*)/)
      if group_name != nil && group_name.first == name
        containsCommand[1].slice!(0) &&  containsCommand[1].slice!(containsCommand[1].length-1) if containsCommand[1] != nil && containsCommand[1][0] == "'" && containsCommand[1][containsCommand[1].length-1] == "'"
        puts "aka g " + "#{answer.first}".red + "=#{containsCommand[1]}"
        answer_count += 1
      end
    end
  }
  return answer_count
end


741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
# File 'lib/aka/helpers.rb', line 741

def self.print_the_aliases_return_array content_array
  array = []
  content_array.each_with_index { |line, index|
    testline = line
    line = line.gsub("# =>", "-g")
    value = testline.split(" ")
    containsCommand = line.split('=') #containsCommand[1]
    if value.length > 1 && value.first == "alias"
      answer = value[1].split("=") #contains the alias
      group_name = testline.scan(/# => ([a-zA-z]*)/).first if testline.scan(/# => ([a-zA-z]*)/)
      containsCommand[1].slice!(0) &&  containsCommand[1].slice!(containsCommand[1].length-1) if containsCommand[1] != nil && containsCommand[1][0] == "'" && containsCommand[1][containsCommand[1].length-1] == "'"
      array.push("aka g " + "#{answer.first}" + "=#{containsCommand[1]}")
    end
  }
  return array
end


723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
# File 'lib/aka/helpers.rb', line 723

def self.print_the_aliases_return_array2 content_array, name
  array = []
  content_array.each_with_index { |line, index|
    testline = line
    line = line.gsub("# =>", "-g")
    value = testline.split(" ")
    containsCommand = line.split('=') #containsCommand[1]
    if value.length > 1 && value.first == "alias"
      answer = value[1].split("=") #contains the alias
      group_name = testline.scan(/# => ([a-zA-z]*)/).first if testline.scan(/# => ([a-zA-z]*)/)
      if group_name != nil && group_name.first == name
        array.push(testline)
      end
    end
  }
  return array
end


28
29
30
31
32
# File 'lib/aka/printing.rb', line 28

def self.print_title(with_these)
  puts ""
  puts "*** #{with_these} ***"
  puts ""
end

.product_content_array(content) ⇒ Object



2
3
4
5
# File 'lib/aka/helpers.rb', line 2

def self.product_content_array(content)
  content.gsub!(/\r\n?/, "\n")
  return content_array = content.split("\n")
end

.read(location) ⇒ Object



477
478
479
480
# File 'lib/aka/helpers.rb', line 477

def self.read location
  answer = File.exist?(location)
  answer == true && content = File.open(location).read ? content : ""
end

.readYML(path) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/aka/helpers.rb', line 62

def self.readYML path
  if File.exists? path
    return YAML.load_file(path)
  else
    error_statement("#{CONFIG_PATH} does not exist. Type `aka setup` to setup the config file")
  end
end

.reload_dot_fileObject



2
3
4
# File 'lib/aka/core.rb', line 2

def self.reload_dot_file
  isOhMyZsh == true ? system("exec zsh") : system("kill -SIGUSR1 #{Process.ppid}")
end

.reload_with_sourceObject



58
59
60
# File 'lib/aka/helpers.rb', line 58

def self.reload_with_source
  system "source #{readYML("#{CONFIG_PATH}")["dotfile"]}"
end

.remove(input) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/aka/helpers.rb', line 308

def self.remove input
  str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
  if content=File.open(str).read
    content_array = product_content_array(content)
    content_array.each_with_index { |line, index|
      line = line.gsub("# =>", "-g")
      value = line.split(" ")
      if value.length > 1 && value.first == "alias"
        answer = value[1].split("=")
        if answer.first == input
          content_array.delete_at(index) && write_with_newline(content_array)
          print_out_command = "aka g #{input}=#{line.split("=")[1]}"
          puts "Removed: ".red  + "#{print_out_command}"
          return true
        end
      end
    }

    puts "#{input} cannot be found.".red
  else
    puts "#{@pwd} cannot be found.".red
    return false
  end
end

.remove_autosourceObject



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/aka/helpers.rb', line 333

def self.remove_autosource
  str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
  if content=File.open(str).read
    content_array = product_content_array(content)
    content_array.each_with_index { |line, index|
      if line == "source \"/home/ryan/.aka/autosource\""
        content_array.delete_at(index) && write_with_newline(content_array)
        puts "Removed: ".red + "source \"/home/ryan/.aka/autosource\""
        return true
      end
    }
  else
    error_statement("autosource cannot be found in dotfile.")
    return false
  end
end

.repeated_system_call(array) ⇒ Object



715
716
717
718
719
720
721
# File 'lib/aka/helpers.rb', line 715

def self.repeated_system_call array
  array.each do |line|
    line.gsub!("\'", "\"") #need to replace ' with "
    line = line + " -n" #do not reload :)
    system(line)
  end
end

.search_alias_return_alias_tokens(argument) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/aka/helpers.rb', line 254

def self.search_alias_return_alias_tokens argument
  str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
  if content = File.open(str).read
    content_array = product_content_array(content)
    content_array.each_with_index { |line, index|
      line = line.gsub("# =>", "-g")
      value = line.split(" ")
      containsCommand = line.split('=') #containsCommand[1]
      if value.length > 1 && value.first == "alias"
        answer = value[1].split("=") #contains the alias
        if found?(answer.first, argument.split("=").first, line) == true
          this_alias = answer.first
          answer.slice!(0) #rmove the first
          containsCommand[1].slice!(0) &&  containsCommand[1].slice!(containsCommand[1].length-1) if containsCommand[1] != nil && containsCommand[1][0] == "'" && containsCommand[1][containsCommand[1].length-1] == "'"
          return [true, this_alias, containsCommand[1]]
        end
      end
    }
  else
    puts "#{@pwd} cannot be found.".red
    return [false, nil, nil]
  end
  return [false, nil, nil]

end

.search_alias_return_alias_tokens_with_group(argument) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/aka/helpers.rb', line 280

def self.search_alias_return_alias_tokens_with_group argument
  str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
  if content = File.open(str).read
    content_array = product_content_array(content)
    content_array.each_with_index { |line, index|
      value = line.split(" ")
      if value.length > 1 && value.first == "alias"
        # templine = line.gsub("# =>", "-g")
        templine = line.split("# =>").first
        containsCommand = templine.split('=') #containsCommand[1]
        group_name = get_group_name(line)
        answer = value[1].split("=") #contains the alias
        if found?(answer.first, argument.split("=").first, templine) == true
          this_alias = answer.first
          answer.slice!(0) #rmove the first
          containsCommand[1].strip!
          containsCommand[1].slice!(0) &&  containsCommand[1].slice!(containsCommand[1].length-1) if containsCommand[1] != nil && containsCommand[1][0] == "'" && containsCommand[1][containsCommand[1].length-1] == "'"
          return [true, this_alias, containsCommand[1], group_name]
        end
      end
    }
  else
    puts "#{@pwd} cannot be found.".red
    return [false, nil, nil, nil]
  end
  return [false, nil, nil, nil]
end

.search_alias_with_group_name(name) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/aka/helpers.rb', line 176

def self.search_alias_with_group_name name
  print_title("System Alias")
  group_count = 0
  if name == "group"
    name = "default"
    str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
    if content = File.open(str).read
      content_array = product_content_array(content)
      group_count = print_the_aliases(content_array)
      if group_count == 0
        puts "No alias found in default group"
      else
        exist_statement("A total of #{group_count} aliases in default group.")
      end
    end
  else

    str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
    if content = File.open(str).read
      content_array = product_content_array(content)
      group_count = print_the_aliases2(content_array, name)
    end

    if group_count == 0
      puts "No alias found in #{name} group"
    else
      exist_statement("A total of #{group_count} aliases in #{name} group.")
    end
  end
end

.set_to_dotfile(filename) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/aka/config.rb', line 63

def self.set_to_dotfile(filename)
  if filename == ".zshrc"
    setZSHRC2
  elsif filename == ".bashrc"
    setBASHRC2
  elsif filename == ".bash_profile"
    setBASH2
  elsif filename == ".profile"
    setPROFILE
  end
end

.setBASHObject



132
133
134
135
136
# File 'lib/aka/config.rb', line 132

def self.setBASH
  setPath("#{BASH_PROFILE_PATH}","dotfile")
  setPath("#{Dir.home}/.bash_history","history")
  setPath("/etc/profile","profile")
end

.setBASH2Object

ryan - set the right dotfile and profile



152
153
154
155
156
157
# File 'lib/aka/config.rb', line 152

def self.setBASH2 #ryan - set the right dotfile and profile
  setPath("#{BASH_PROFILE_PATH}","dotfile")
  setPath("#{Dir.home}/.bash_history","history")
  setPath("/etc/profile","profile")
  setPath("#{AKA_PATH}","home")
end

.setBASHRCObject



126
127
128
129
130
# File 'lib/aka/config.rb', line 126

def self.setBASHRC
  setPath("#{BASHRC_PATH}","dotfile")
  setPath("#{Dir.home}/.bash_history","history")
  setPath("/etc/profile","profile")
end

.setBASHRC2Object

ryan - set the right dotfile and profile



145
146
147
148
149
150
# File 'lib/aka/config.rb', line 145

def self.setBASHRC2 #ryan - set the right dotfile and profile
  setPath("#{BASHRC_PATH}","dotfile")
  setPath("#{Dir.home}/.bash_history","history")
  setPath("#{BASHRC_PATH}","profile")
  setPath("#{AKA_PATH}","home")
end

.setPath(path, value) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/aka/helpers.rb', line 46

def self.setPath(path, value)
  data = readYML("#{CONFIG_PATH}")
  if data.has_key?(value) == true
    old_path = data[value]
    data[value] = path
    writeYML("#{CONFIG_PATH}", data)
    puts "#{value} -> #{path}"
  else
    error_statement("--#{value} does not exist in #{CONFIG_PATH} ")
  end
end

.setPROFILEObject



159
160
161
162
163
164
# File 'lib/aka/config.rb', line 159

def self.setPROFILE
  setPath("#{PROFILE_PATH}","dotfile")
  setPath("#{Dir.home}/.bash_history","history")
  setPath("/etc/profile","profile")
  setPath("#{AKA_PATH}","home")
end

.setup_akaObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
55
56
57
58
59
60
# File 'lib/aka/config.rb', line 4

def self.setup_aka
  userBash = []
  # 1. check for each type of file without setting anything.
  if File.exist?("#{ZSHRC_PATH}") #if zshrc exist
    userBash.push(".zshrc")
  end
  if File.exist?("#{BASHRC_PATH}") #if bashrc exist
    userBash.push(".bashrc")
  end
  if File.exist?("#{BASH_PROFILE_PATH}") #if bash_profile exist
    userBash.push(".bash_profile")
  end
  if File.exist?("#{PROFILE_PATH}") #if .profile exist
    userBash.push(".profile")
  end

  #2. count the number of types

  #3 if number of types is 1, proceed to set it
  if userBash.count == 1
    set_to_dotfile(userBash.first)

  elsif userBash.count > 1
    #4 if the number of types is more than 1, proceed to ask which one does the users want to uses.
    userBash.each_with_index do |choice,i|
      puts "#{i+1}. Setup at #{Dir.home}/#{choice}"
    end

    puts ("Where do you wish to setup aka? (Pick a number and enter)\n")
    choice = STDIN.gets.chomp
  
    case choice
      when "1"
        set_to_dotfile(userBash[0]) if userBash[0]
      when "2"
        if userBash[1] then set_to_dotfile(userBash[1]) else abort "No file choosen" end
      when "3"
        if userBash[2] then set_to_dotfile(userBash[2]) else abort "No file choosen" end
      when "4"
        if userBash[3] then set_to_dotfile(userBash[3]) else abort "No file choosen" end
      else
        puts "Invalid input, Please enter the number between 1 and #{userBash.count}. Please try again"
        abort "No file choosen"
    end
  end #if userBash > 1

    # if File.exist?("#{ZSHRC_PATH}") #if zshec exist
    #   setZSHRC2
    # elsif File.exist?("#{BASHRC_PATH}") #if bashrc exist
    #   setBASHRC2
    # elsif File.exist?("#{BASH_PROFILE_PATH}") #if bash_profile exist
    #   setBASH2
    # else
    #   puts "Aka2 only supports zshrc, bashrc and bash_profile"
    #   puts "Please contact http://github.com/ytbryan for more info."
    # end
end

.setup_autosourceObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/aka/config.rb', line 75

def self.setup_autosource
  if File.exist?("#{AKA_PATH}")
    FileUtils.touch("#{AKA_PATH}/autosource")
    out_file = File.new("#{AKA_PATH}/autosource", "w")
    out_file.puts("export HISTSIZE=10000")
    out_file.puts("sigusr2() { unalias $1;}")
    out_file.puts("sigusr1() { source #{readYML("#{CONFIG_PATH}")["dotfile"]}; history -a; echo 'reloaded dot file'; }")
    out_file.puts("trap sigusr1 SIGUSR1")
    out_file.puts("trap 'sigusr2 $(cat ~/sigusr1-args)' SIGUSR2")
    out_file.close
    autosource = "\nsource \"#{AKA_PATH}/autosource\""
    append(autosource, readYML("#{CONFIG_PATH}")['profile'])
    puts "Done. Please restart this shell.".red
  else
    puts "Directory #{CONFIG_PATH} doesn't exist"
  end
end

.setup_configObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/aka/config.rb', line 92

def self.setup_config
  if File.exist?("#{CONFIG_PATH}")
    puts "Directory #{CONFIG_PATH} exist"
  else
    FileUtils.mkdir_p("#{AKA_PATH}")
    FileUtils.touch("#{CONFIG_PATH}")
    out_file = File.new("#{CONFIG_PATH}", "w")
    out_file.puts("---")
    out_file.puts("dotfile: \"/home/user/.bashrc\"")
    out_file.puts("history: \"/home/user/.bash_history\"")
    out_file.puts("home: \"/home/user/.aka\"")
    out_file.puts("install: \"/usr/local/bin\"")
    out_file.puts("profile: \"/home/user/.bashrc\"")
    out_file.puts("list: 20")
    out_file.puts("usage: 20")
    out_file.puts("remote: 12.12.12.21")
    out_file.close
  end
end

.setZSHRCObject



120
121
122
123
124
# File 'lib/aka/config.rb', line 120

def self.setZSHRC
  setPath("#{ZSHRC_PATH}","dotfile")
  setPath("#{Dir.home}/.zsh_history","history")
  setPath("/etc/zprofile","profile")
end

.setZSHRC2Object

ryan - set the right dotfile and profile



138
139
140
141
142
143
# File 'lib/aka/config.rb', line 138

def self.setZSHRC2 #ryan - set the right dotfile and profile
  setPath("#{ZSHRC_PATH}","dotfile")
  setPath("#{Dir.home}/.zsh_history","history")
  setPath("#{ZSHRC_PATH}","profile")
  setPath("#{AKA_PATH}","home")
end

.showBar(percent) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aka/string.rb', line 16

def self.showBar percent
  result = ""
  val = percent/100 * 50
  val = 2 if val > 1 && val < 2
  val = 1 if val.round <= 1 #for visibiity, show two bars if it's just one
  val.round.times do
    result += "+"
  end

  remaining = 50 - val.round
  remaining.times do
    result += "-".red
  end

  return result + " #{percent.round(2)}%"
end

.showConfigObject



112
113
114
115
116
117
118
# File 'lib/aka/config.rb', line 112

def self.showConfig
  thing = YAML.load_file("#{CONFIG_PATH}")
  puts ""
  thing.each do |company,details|
    puts "#{company} -> " + "#{details}".red
  end
end

.showlast(list_number = false, howmany = 10, showGroup) ⇒ Object



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/aka/helpers.rb', line 496

def self.showlast(list_number=false,howmany=10, showGroup)
  str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
  if content = File.open(str).read
    content_array = product_content_array(content)
    #why not just call the last five lines? Because i can't be sure that they are aliases
    total_aliases = []
    content_array.each_with_index { |line, index|
      value = line.split(" ")
      if value.length > 1 && value.first == "alias"
        total_aliases.push(line)
      end
    }
    puts ""
    if total_aliases.count > howmany #if there is enogh alias
      total_aliases.last(howmany).each_with_index do |line, index|
        line = line.gsub("# =>", "-g")
        splitted= line.split('=')
        if list_number
          puts "#{total_aliases.count - howmany + index+1}. aka g " + splitted[0].split(" ")[1].red + "=" + splitted[1]
        else
          puts "aka g " + splitted[0].split(" ")[1].red + "=" + splitted[1]
        end
      end
    else #if there is not enough alias
      total_aliases.last(howmany).each_with_index do |line, index|
        line = line.gsub("# =>", "-g")
        splitted= line.split('=')
        if list_number
          puts "#{index+1}. aka g " + splitted[0].split(" ")[1].red + "=" + splitted[1]
        else
          puts "aka g " + splitted[0].split(" ")[1].red + "=" + splitted[1]
        end
      end
    end
    puts ""
  end
end

.showSpace(word) ⇒ Object

Getting these babies ready for beauty contest



6
7
8
9
10
11
12
13
14
# File 'lib/aka/string.rb', line 6

def self.showSpace word
  space = ""
  val = 20 - word.size
  val = 20 if val < 0
  val.times do
    space += " "
  end
  return space
end

.showUsage(howmany = 10, least = false) ⇒ Object



534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'lib/aka/helpers.rb', line 534

def self.showUsage howmany=10, least=false
  str = is_config_file_present?(readYML("#{CONFIG_PATH}")["history"])
  value = reload_dot_file
  #get all aliases
  if content = File.open(str).read
    content_array = product_content_array(content)
    total_aliases = []
    content_array.each_with_index { |line, index|
      value = line.split(" ")
      total_aliases.push(value.first)
    }
    count_aliases(total_aliases, howmany, least)
  end
end

.sort(countarray, namearray) ⇒ Object

highest first. decscending.



601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'lib/aka/helpers.rb', line 601

def self.sort(countarray, namearray) #highest first. decscending.
  temp = 0, temp2 = ""
  countarray.each_with_index { |count, index|
    countarray[0..countarray.size-index].each_with_index { |x, thisindex|  #always one less than total

      if index < countarray.size-1 && thisindex < countarray.size-1
        if countarray[thisindex] < countarray[thisindex+1] #if this count is less than next count
          temp = countarray[thisindex]
          countarray[thisindex] = countarray[thisindex+1]
          countarray[thisindex+1] = temp

          temp2 = namearray[thisindex]
          namearray[thisindex] = namearray[thisindex+1]
          namearray[thisindex+1] = temp2
        end
      end

    }
  }#outer loop
  return countarray, namearray
end

.split(fulldomain) ⇒ Object



113
114
115
116
117
118
# File 'lib/aka/helpers.rb', line 113

def self.split fulldomain
  username = fulldomain.split("@").first
  domain = fulldomain.split("@")[1].split(":").first
  port = fulldomain.split("@")[1].split(":")[1]
  return [username, domain, port]
end

.unalias_the(value) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/aka/core.rb', line 6

def self.unalias_the value
  if isOhMyZsh == true
    system("exec zsh")
  else
    system "echo '#{value}' > ~/sigusr1-args;"
    system "kill -SIGUSR2 #{Process.ppid}"
  end
end

.write(str, path) ⇒ Object



105
106
107
# File 'lib/aka/helpers.rb', line 105

def self.write str, path
  File.open(path, 'w') { |file| file.write(str) }
end

.write_with_array_into(path, array) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/aka/helpers.rb', line 87

def self.write_with_array_into path, array
  File.open(path, 'w') { |file|
    array.each do |line|
      file.write(line)
      file.write("\n")
    end
  }
end

.write_with_array_into_withprint(path, array) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/aka/helpers.rb', line 74

def self.write_with_array_into_withprint path, array
  File.open(path, 'w') { |file|
    file.write("#generated with https://rubygems.org/gems/aka2")
    file.write("\n\n")

    array.each do |line|
      file.write(line)
      file.write("\n")
    end
  }
end

.write_with_newline(array) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/aka/helpers.rb', line 96

def self.write_with_newline array
  str = is_config_file_present?(readYML("#{CONFIG_PATH}")["dotfile"])
  File.open(str, 'w') { |file|
    array.each do |line|
      file.write(line + "\n")
    end
  }
end

.writeYML(path, theyml) ⇒ Object



70
71
72
# File 'lib/aka/helpers.rb', line 70

def self.writeYML path, theyml
  File.open(path, 'w') {|f| f.write theyml.to_yaml } #Store
end