am 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8df19bbe4cba337cd11c641f072abbfe372445c7
4
+ data.tar.gz: ec27b16762f43a976981a96e65d4ab141dd7453c
5
+ SHA512:
6
+ metadata.gz: 3eb3f376df1b8a3be7bfcb55d2a93c48bf70726fcc9074feb019a9261cf9996d404a30583178e1ccc9eebe0fa081f401acafba466274f7df4b0a1a0ea8090499
7
+ data.tar.gz: aa96470961e50186ce2151d9f415d486fba51ed6ba6473d289ecabc04928bd45707751d807540d3be67bf304c910d92e8da6cb36409689c1e0958f34b0c1e257
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in am.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 ka-yamashita
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Am(Alias Manager)
2
+
3
+ `alias` command wrapper
4
+
5
+ ## Installation
6
+
7
+ $ gem install am
8
+
9
+ ## Usage
10
+
11
+ * zsh user
12
+ run this command
13
+ ```
14
+ echo 'source ~/.am_config' >> ~/.zprofile
15
+ echo 'setopt share_history' >> ~/.zprofile
16
+ ```
17
+
18
+ * bash user
19
+
20
+ add this code to `~/.bashrc`
21
+ ```
22
+ function share_history {
23
+ history -a
24
+ history -c
25
+ history -r
26
+ }
27
+ PROMPT_COMMAND='share_history'
28
+ shopt -u histappend
29
+ export HISTSIZE=9999
30
+ ```
31
+
32
+ run this commnad
33
+
34
+ ```
35
+ echo 'source ~/.am_config' >> ~/.bash_profile
36
+ ```
37
+
38
+ ### command options
39
+
40
+ $ am [show]
41
+ print current config
42
+
43
+ $ am add
44
+ add alias from last history command
45
+
46
+ $ am add -l(list)
47
+ add alias select from five line last history command
48
+
49
+ $ am del [alias_name)
50
+ delete alias from alias_name
51
+
52
+ $ am del -l(list)
53
+ delete alias select from current config
54
+ ## License
55
+ * MIT
56
+
57
+
58
+ ## Author
59
+ * ka-yamashita
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/am.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'am/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "am"
8
+ spec.version = Am::VERSION
9
+ spec.authors = ["ka-yamashita"]
10
+ spec.email = ["www.kazu.com@gmail.com"]
11
+ spec.summary = %q{command note command.}
12
+ spec.description = %q{command note command.}
13
+ spec.homepage = "http://ten-snapon.com"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_dependency 'thor'
24
+ end
data/bin/am ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'am'
3
+
4
+ AM::CLI.start
data/lib/am.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "am/version"
2
+ require "am/cli"
3
+ module AM
4
+ # Your code goes here...
5
+ CONFIG_FILE=File.expand_path('~/.am_config')
6
+ ALIAS = 0
7
+ COMMAND = 1
8
+ def self.before_break(message)
9
+ puts "\n#{message}"
10
+ end
11
+ def self.after_break(message)
12
+ puts "#{message}\n\n"
13
+ end
14
+ end
data/lib/am/cli.rb ADDED
@@ -0,0 +1,102 @@
1
+ require 'am'
2
+ require 'tail'
3
+ require 'config'
4
+ require 'ui'
5
+ require 'thor'
6
+
7
+ module AM
8
+ class CLI < Thor
9
+ default_command :show
10
+ def initialize(*args)
11
+ super
12
+ config = Config.new
13
+ @config = config.current
14
+ @ui = Ui.new
15
+ end
16
+
17
+ desc "show", "show current alias"
18
+ def show
19
+ if @config.empty?
20
+ puts 'a blank config'
21
+ else
22
+ @ui.current_config(@config)
23
+ end
24
+ end
25
+
26
+ desc "add", "add alias"
27
+ option :list, :type => :boolean, :aliases => '-l'
28
+ def add
29
+ tail = Tail.new
30
+ # registeration from history choice
31
+ if options[:list]
32
+ commands = tail.print_last_commands
33
+ add_record = @ui.add_command_with_number(commands)
34
+ # registeration from last history
35
+ else
36
+ last_command = tail.get_last_command
37
+ add_record = @ui.add_command_with_last_history(last_command)
38
+ end
39
+
40
+ if uniq?(add_record)
41
+ @config << add_record
42
+ add_config(add_record)
43
+ else
44
+ AM.before_break("")
45
+ show
46
+ end
47
+ end
48
+
49
+ desc "del", "delete alias"
50
+ option :list, :type => :boolean, :aliases => '-l'
51
+
52
+ def del(delete_alias=nil)
53
+ if @config.empty?
54
+ puts 'a blank config'
55
+ exit
56
+ end
57
+
58
+ if options[:list] || delete_alias == nil
59
+ @ui.current_config(@config)
60
+ delete_alias = @ui.del_command_with_number(@config)
61
+ end
62
+ delete_config(delete_alias)
63
+ end
64
+
65
+ no_commands do
66
+
67
+ def add_config(add_record)
68
+ config = Config.new
69
+ if config.save_config(@config)
70
+ AM.before_break("[success] #{add_record[ALIAS]} / #{add_record[COMMAND]} added command")
71
+ AM.after_break("please run: [ source #{CONFIG_FILE} ]")
72
+ else
73
+ puts "[error] #{add_record[ALIAS]} / #{add_record[COMMAND]} couldn't add command"
74
+ end
75
+ end
76
+
77
+ def delete_config(exclude)
78
+ config = Config.new
79
+ if config.save_config(@config, exclude)
80
+ AM.before_break("[success] delete alias #{exclude}")
81
+ AM.after_break("please run: [ source #{CONFIG_FILE} ]")
82
+ else
83
+ AM.after_break("[error] failue delete alias #{exclude}}")
84
+ end
85
+ end
86
+
87
+ def uniq?(add_record)
88
+ @config.each do |al, command|
89
+ if add_record[ALIAS] == al
90
+ AM.before_break("[error] not written as duplecate alias is '#{add_record[ALIAS]}'")
91
+ return false
92
+ elsif add_record[COMMAND] == command
93
+ AM.before_break("[error] not written as duplecate command is #{add_record[COMMAND]}")
94
+ return false
95
+ end
96
+ end
97
+ return true
98
+ end
99
+ end
100
+
101
+ end
102
+ end
data/lib/am/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Am
2
+ VERSION = "0.0.1"
3
+ end
data/lib/config.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'am'
2
+
3
+ module AM
4
+ class Config
5
+
6
+ def current
7
+ config = []
8
+ File.open(CONFIG_FILE, 'r') do |file|
9
+ file.each_line do |line|
10
+ config << line.gsub(/^alias /, '').strip.split('=', 2)
11
+ end
12
+ end if File.exists?(CONFIG_FILE)
13
+ config
14
+ end
15
+
16
+ def save_config(cache, exclude=nil)
17
+ tmp_file = CONFIG_FILE + '.tmp'
18
+ file = File.open(tmp_file, "w")
19
+
20
+ cache.each do |al, command|
21
+ record = "alias #{al.to_s.strip}=#{command.to_s.strip}"
22
+ if al.to_s.strip != exclude
23
+ file.puts(record)
24
+ end
25
+ `#{record}`
26
+ end
27
+ file.close
28
+ File.rename(tmp_file, CONFIG_FILE)
29
+ end
30
+ end
31
+ end
data/lib/tail.rb ADDED
@@ -0,0 +1,41 @@
1
+ require 'am'
2
+
3
+ module AM
4
+ class Tail
5
+ def initialize(*args)
6
+ @sh_history_file = get_history_file
7
+ end
8
+
9
+ def get_history_file
10
+ shell = `echo $SHELL`
11
+ if shell =~ /zsh/
12
+ @history_pattern = '.*;(.*)\n'
13
+ return File.expand_path('~/.zsh_history')
14
+ elsif shell =~ /bash/
15
+ @history_pattern = '^\s+[0-9]+\s{2}'
16
+ return File.expand_path('~/.bash_history')
17
+ else
18
+ puts "does not support is #{shell}"
19
+ end
20
+ end
21
+
22
+ def print_last_commands
23
+ exit if @sh_history_file.nil?
24
+
25
+ commands = []
26
+ last_commands = `tail -6 #{@sh_history_file} | head -5`
27
+ last_commands.each_with_index do |c,i|
28
+ record = c.split(/#{@history_pattern}/)[COMMAND]
29
+ puts (i+1).to_s + ': ' + record
30
+ commands << record
31
+ end
32
+ commands
33
+ end
34
+
35
+ def get_last_command
36
+ exit if @sh_history_file.nil?
37
+ `tail -2 #{@sh_history_file} | head -1`.split(/#{@history_pattern}/)[COMMAND].strip
38
+ end
39
+
40
+ end
41
+ end
data/lib/ui.rb ADDED
@@ -0,0 +1,78 @@
1
+ require 'am'
2
+
3
+ module AM
4
+ class Ui
5
+ def current_config(config)
6
+ al_max_len = 0
7
+
8
+ AM::after_break("current commands of the config")
9
+
10
+ config.each do |al,command|
11
+ al_max_len = max(al.to_s.length, al_max_len)
12
+ end
13
+
14
+ index_max_len = config.length.to_s.length
15
+
16
+ config.each_with_index do|record,index|
17
+
18
+ as = ''
19
+ (al_max_len - record[ALIAS].length.to_i).times do as = as + ' ' end
20
+ is = ''
21
+ (index_max_len - (index+1).to_s.length).times do is = is + ' ' end
22
+
23
+ puts " #{is}#{(index+1).to_s} : #{record[ALIAS].to_s}#{as} = #{record[COMMAND].to_s}"
24
+ end
25
+ AM::before_break("")
26
+ end
27
+
28
+ def add_command_with_number(commands)
29
+ print 'please input add command number: '
30
+ number = please_input
31
+ valid?(number, '^[^1-5]', '[error] input using number!')
32
+ alias_name = get_alias
33
+ [alias_name, quot(commands[number.to_i-1])]
34
+ end
35
+
36
+ def add_command_with_last_history(command)
37
+ puts "add command is #{quot(command)}"
38
+ alias_name = get_alias
39
+ [alias_name, quot(command)]
40
+ end
41
+
42
+ def del_command_with_number(config)
43
+ print 'please input delete command number: '
44
+ number = please_input
45
+ valid?(number, '^[^0-9]', '[error] input using number!')
46
+ delete_alias = config[number.to_i-1][ALIAS] if config.length >= number.to_i
47
+ end
48
+
49
+ def get_alias
50
+ print "please input add command alias: "
51
+ name = please_input
52
+ valid?(name, '^[^\w-]', '[error] input using a-z or 0-9 or _ or -!')
53
+ name
54
+ end
55
+
56
+ def valid?(val, pattern, message)
57
+ if /#{pattern}/ =~ val
58
+ puts message
59
+ exit
60
+ end
61
+ end
62
+
63
+ def please_input
64
+ while val = STDIN.gets
65
+ break if /\n$/ =~ val
66
+ end
67
+ val.strip
68
+ end
69
+
70
+ def quot(val)
71
+ "'#{val.to_s}'"
72
+ end
73
+
74
+ def max(a, b)
75
+ (a.to_i > b.to_i) ? a : b
76
+ end
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: am
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ka-yamashita
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: command note command.
56
+ email:
57
+ - www.kazu.com@gmail.com
58
+ executables:
59
+ - am
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - am.gemspec
69
+ - bin/am
70
+ - lib/am.rb
71
+ - lib/am/cli.rb
72
+ - lib/am/version.rb
73
+ - lib/config.rb
74
+ - lib/tail.rb
75
+ - lib/ui.rb
76
+ homepage: http://ten-snapon.com
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.6
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: command note command.
100
+ test_files: []