am 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/am.gemspec +1 -0
- data/lib/am/cli.rb +26 -17
- data/lib/am/version.rb +1 -1
- data/lib/am.rb +5 -3
- data/lib/config.rb +6 -7
- data/lib/tail.rb +27 -16
- data/lib/ui.rb +17 -22
- data/spec/lib/config_spec.rb +30 -0
- data/spec/lib/tail_spec.rb +21 -0
- data/spec/spec_helper.rb +3 -0
- metadata +22 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1c035e872c61d3c8de94e71e81b49d30fc26792
|
4
|
+
data.tar.gz: 42f8fa57dad2dd410c302a59a1cb105b30773772
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ebf1c71e6ccc74ef5ae1044c37bbead6ade425dfc4dafdd30ae637a21b6f852c4af53021d8453ce28da5da9aee7eac52bb07f7652e96e496aefad76554cc6240
|
7
|
+
data.tar.gz: 83331d4c739e1a5f7dfdbc2296801741acbe481fd3687d7879ef1622d0c51907dce6288dbff90f33452ac888c399138f4afba218c149af0a019d041699f7f9de
|
data/am.gemspec
CHANGED
data/lib/am/cli.rb
CHANGED
@@ -19,7 +19,7 @@ module AM
|
|
19
19
|
if @config.empty?
|
20
20
|
puts 'a blank config'
|
21
21
|
else
|
22
|
-
@ui.
|
22
|
+
@ui.print_current_config(@config)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -29,19 +29,21 @@ module AM
|
|
29
29
|
tail = Tail.new
|
30
30
|
# registeration from history choice
|
31
31
|
if options[:list]
|
32
|
-
commands = tail.
|
32
|
+
commands = tail.get_last_five_command
|
33
|
+
@ui.print_last_commands(commands)
|
33
34
|
add_record = @ui.add_command_with_number(commands)
|
35
|
+
|
34
36
|
# registeration from last history
|
35
37
|
else
|
36
38
|
last_command = tail.get_last_command
|
37
39
|
add_record = @ui.add_command_with_last_history(last_command)
|
38
40
|
end
|
39
41
|
|
40
|
-
if uniq?(add_record)
|
42
|
+
if uniq?(add_record) && valid?(add_record)
|
41
43
|
@config << add_record
|
42
44
|
add_config(add_record)
|
43
45
|
else
|
44
|
-
AM.
|
46
|
+
AM.p1("")
|
45
47
|
show
|
46
48
|
end
|
47
49
|
end
|
@@ -56,7 +58,7 @@ module AM
|
|
56
58
|
end
|
57
59
|
|
58
60
|
if options[:list] || delete_alias == nil
|
59
|
-
@ui.
|
61
|
+
@ui.print_current_config(@config)
|
60
62
|
delete_alias = @ui.del_command_with_number(@config)
|
61
63
|
end
|
62
64
|
delete_config(delete_alias)
|
@@ -67,8 +69,8 @@ module AM
|
|
67
69
|
def add_config(add_record)
|
68
70
|
config = Config.new
|
69
71
|
if config.save_config(@config)
|
70
|
-
AM.
|
71
|
-
AM.
|
72
|
+
AM.p1("[success] #{add_record[ALIAS]} / #{add_record[COMMAND]} added command")
|
73
|
+
AM.p2("please run: [ source #{CONFIG_FILE} ]")
|
72
74
|
else
|
73
75
|
puts "[error] #{add_record[ALIAS]} / #{add_record[COMMAND]} couldn't add command"
|
74
76
|
end
|
@@ -77,26 +79,33 @@ module AM
|
|
77
79
|
def delete_config(exclude)
|
78
80
|
config = Config.new
|
79
81
|
if config.save_config(@config, exclude)
|
80
|
-
AM.
|
81
|
-
AM.
|
82
|
+
AM.p1("[success] delete alias #{exclude}")
|
83
|
+
AM.p2("please run: [ source #{CONFIG_FILE} ]")
|
82
84
|
else
|
83
|
-
AM.
|
85
|
+
AM.p2("[error] failue delete alias #{exclude}}")
|
84
86
|
end
|
85
87
|
end
|
86
88
|
|
87
89
|
def uniq?(add_record)
|
88
|
-
@config.each do |
|
89
|
-
if add_record[ALIAS] ==
|
90
|
-
AM.
|
90
|
+
@config.each do |a,c|
|
91
|
+
if add_record[ALIAS] == a
|
92
|
+
AM.p1("[error] not written as duplecate alias is '#{add_record[ALIAS]}'")
|
91
93
|
return false
|
92
|
-
elsif add_record[COMMAND] ==
|
93
|
-
AM.
|
94
|
+
elsif add_record[COMMAND] == c
|
95
|
+
AM.p1("[error] not written as duplecate command is #{add_record[COMMAND]}")
|
94
96
|
return false
|
95
97
|
end
|
96
98
|
end
|
97
|
-
|
99
|
+
true
|
98
100
|
end
|
99
|
-
end
|
100
101
|
|
102
|
+
def valid?(add_record)
|
103
|
+
unless add_record[ALIAS].length > 0 || add_record[COMMAND].length > 0
|
104
|
+
puts "[error] #{add_record[ALIAS]} / #{add_record[COMMAND]} length equal 0"
|
105
|
+
return false
|
106
|
+
end
|
107
|
+
true
|
108
|
+
end
|
109
|
+
end
|
101
110
|
end
|
102
111
|
end
|
data/lib/am/version.rb
CHANGED
data/lib/am.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
require "am/version"
|
2
2
|
require "am/cli"
|
3
3
|
module AM
|
4
|
-
|
4
|
+
|
5
5
|
CONFIG_FILE=File.expand_path('~/.am_config')
|
6
6
|
ALIAS = 0
|
7
7
|
COMMAND = 1
|
8
|
-
|
8
|
+
|
9
|
+
def self.p1(message="")
|
9
10
|
puts "\n#{message}"
|
10
11
|
end
|
11
|
-
|
12
|
+
|
13
|
+
def self.p2(message="")
|
12
14
|
puts "#{message}\n\n"
|
13
15
|
end
|
14
16
|
end
|
data/lib/config.rb
CHANGED
@@ -2,7 +2,6 @@ require 'am'
|
|
2
2
|
|
3
3
|
module AM
|
4
4
|
class Config
|
5
|
-
|
6
5
|
def current
|
7
6
|
config = []
|
8
7
|
File.open(CONFIG_FILE, 'r') do |file|
|
@@ -13,17 +12,17 @@ module AM
|
|
13
12
|
config
|
14
13
|
end
|
15
14
|
|
16
|
-
def save_config(
|
15
|
+
def save_config(config, exclude=nil)
|
17
16
|
tmp_file = CONFIG_FILE + '.tmp'
|
18
17
|
file = File.open(tmp_file, "w")
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
if
|
23
|
-
file.puts(
|
19
|
+
config.each do |a,c|
|
20
|
+
r = "alias #{a.to_s}=#{c.to_s}"
|
21
|
+
if a.to_s != exclude
|
22
|
+
file.puts(r)
|
24
23
|
end
|
25
|
-
`#{record}`
|
26
24
|
end
|
25
|
+
|
27
26
|
file.close
|
28
27
|
File.rename(tmp_file, CONFIG_FILE)
|
29
28
|
end
|
data/lib/tail.rb
CHANGED
@@ -8,36 +8,47 @@ module AM
|
|
8
8
|
|
9
9
|
def set_profile
|
10
10
|
shell = `echo $SHELL`
|
11
|
+
@profile = {}
|
12
|
+
|
11
13
|
if shell =~ /zsh/
|
12
|
-
@
|
13
|
-
|
14
|
-
|
14
|
+
@profile = {
|
15
|
+
pattern: '.*;(.*)',
|
16
|
+
file: File.expand_path('~/.zsh_history'),
|
17
|
+
margin: 0,
|
18
|
+
max_line: 5,
|
19
|
+
}
|
15
20
|
elsif shell =~ /bash/
|
16
|
-
@
|
17
|
-
|
18
|
-
|
21
|
+
@profile = {
|
22
|
+
pattern: '(.*)',
|
23
|
+
file: File.expand_path('~/.bash_history'),
|
24
|
+
margin: 1,
|
25
|
+
max_line: 5,
|
26
|
+
}
|
19
27
|
else
|
20
28
|
puts "does not support is #{shell}"
|
29
|
+
exit
|
21
30
|
end
|
22
31
|
end
|
23
32
|
|
24
|
-
def
|
25
|
-
exit if @
|
26
|
-
|
33
|
+
def get_last_five_command
|
34
|
+
exit if @profile.empty?
|
27
35
|
commands = []
|
28
|
-
last_commands = `tail -#{
|
36
|
+
last_commands = `tail -#{@profile[:max_line] + 1 - @profile[:margin]} #{@profile[:file]} | head -#{@profile[:max_line]}`.split("\n")
|
37
|
+
|
29
38
|
last_commands.each_with_index do |c,i|
|
30
|
-
record = c.split(/#{@
|
31
|
-
puts " #{(i+1).to_s} : #{record.to_s}"
|
39
|
+
record = c.split(/#{@profile[:pattern]}/)[COMMAND].strip
|
32
40
|
commands << record
|
33
|
-
end
|
41
|
+
end unless last_commands.empty?
|
42
|
+
|
34
43
|
commands
|
35
44
|
end
|
36
45
|
|
37
46
|
def get_last_command
|
38
|
-
exit if @
|
39
|
-
`tail -#{2-@tail_margin} #{@sh_history_file} | head -1`.split(/#{@history_pattern}/)[COMMAND].strip
|
40
|
-
end
|
47
|
+
exit if @profile[:file].empty?
|
41
48
|
|
49
|
+
if last_row = `tail -#{2-@profile[:margin]} #{@profile[:file]} | head -1`.split(/#{@profile[:pattern]}/)
|
50
|
+
last_row[COMMAND].strip
|
51
|
+
end
|
52
|
+
end
|
42
53
|
end
|
43
54
|
end
|
data/lib/ui.rb
CHANGED
@@ -2,27 +2,22 @@ require 'am'
|
|
2
2
|
|
3
3
|
module AM
|
4
4
|
class Ui
|
5
|
-
def
|
6
|
-
|
5
|
+
def print_current_config(config)
|
6
|
+
aml = config.max_by{|c| c[0].length }[0].length #alias max length
|
7
|
+
iml = config.length.to_s.length #index max length
|
7
8
|
|
8
|
-
AM::
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
|
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
|
9
|
+
AM::p1("current commands of the config")
|
10
|
+
config.each_with_index do|r,i|
|
11
|
+
# 1: name=command
|
12
|
+
puts " #{' '*(iml - (i+1).to_s.length)}#{(i+1).to_s} : #{r[ALIAS].to_s}#{' '*(aml-r[ALIAS].length)} = #{r[COMMAND].to_s}"
|
13
|
+
end unless config.empty?
|
14
|
+
AM::p1
|
15
|
+
end
|
22
16
|
|
23
|
-
|
24
|
-
|
25
|
-
|
17
|
+
def print_last_commands(commands)
|
18
|
+
commands.each_with_index do |c,i|
|
19
|
+
puts " #{(i+1).to_s} : #{c.to_s}"
|
20
|
+
end unless commands.empty?
|
26
21
|
end
|
27
22
|
|
28
23
|
def add_command_with_number(commands)
|
@@ -30,11 +25,11 @@ module AM
|
|
30
25
|
number = please_input
|
31
26
|
valid?(number, '^[^1-5]', '[error] input using number!')
|
32
27
|
alias_name = get_alias
|
33
|
-
[alias_name, quot(commands[number.to_i-1])]
|
28
|
+
[alias_name, quot(commands[number.to_i-1].strip)]
|
34
29
|
end
|
35
30
|
|
36
31
|
def add_command_with_last_history(command)
|
37
|
-
puts "add command is #{quot(command)}"
|
32
|
+
puts "add command is #{quot(command.strip)}"
|
38
33
|
alias_name = get_alias
|
39
34
|
[alias_name, quot(command)]
|
40
35
|
end
|
@@ -50,7 +45,7 @@ module AM
|
|
50
45
|
print "please input add command alias: "
|
51
46
|
name = please_input
|
52
47
|
valid?(name, '^[^\w-]', '[error] input using a-z or 0-9 or _ or -!')
|
53
|
-
name
|
48
|
+
name.strip
|
54
49
|
end
|
55
50
|
|
56
51
|
def valid?(val, pattern, message)
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe AM::Config do
|
4
|
+
describe 'config commands tests' do
|
5
|
+
before() do
|
6
|
+
@config= AM::Config.new
|
7
|
+
`echo "alias test1='test az - AZ 09 _'" > #{AM::CONFIG_FILE}`
|
8
|
+
end
|
9
|
+
it 'load config' do
|
10
|
+
@config.current
|
11
|
+
expect(@config.current[0][0]).to eq 'test1'
|
12
|
+
expect(@config.current[0][1]).to eq "'test az - AZ 09 _'"
|
13
|
+
end
|
14
|
+
it 'save config'do
|
15
|
+
# add
|
16
|
+
config = @config.current << ['test2',"'abcdefgeijklmn'"]
|
17
|
+
expect(@config.save_config(config)).to eql(0)
|
18
|
+
expect(@config.current.length).to eql(2)
|
19
|
+
|
20
|
+
# delete
|
21
|
+
expect(@config.save_config(config,'test1')).to eql(0)
|
22
|
+
expect(@config.current.length).to eql(1)
|
23
|
+
|
24
|
+
# add check
|
25
|
+
expect(@config.current[0][0]).to eq 'test2'
|
26
|
+
expect(@config.current[0][1]).to eq "'abcdefgeijklmn'"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe AM::Tail do
|
4
|
+
describe 'tail commands tests' do
|
5
|
+
|
6
|
+
before() do
|
7
|
+
@tail = AM::Tail.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'print_last_commands' do
|
11
|
+
expect(@tail.get_last_five_command.length).to eq 5
|
12
|
+
expect(@tail.get_last_five_command[0]).to match(/[a-z]+/)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'get_last_command' do
|
16
|
+
expect(@tail.get_last_command.length).to be >= 1
|
17
|
+
expect(@tail.get_last_command[0]).to match(/[a-z]+/)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: am
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ka-yamashita
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: thor
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -73,6 +87,9 @@ files:
|
|
73
87
|
- lib/config.rb
|
74
88
|
- lib/tail.rb
|
75
89
|
- lib/ui.rb
|
90
|
+
- spec/lib/config_spec.rb
|
91
|
+
- spec/lib/tail_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
76
93
|
homepage: http://ten-snapon.com
|
77
94
|
licenses:
|
78
95
|
- MIT
|
@@ -97,4 +114,7 @@ rubygems_version: 2.4.6
|
|
97
114
|
signing_key:
|
98
115
|
specification_version: 4
|
99
116
|
summary: command note command.
|
100
|
-
test_files:
|
117
|
+
test_files:
|
118
|
+
- spec/lib/config_spec.rb
|
119
|
+
- spec/lib/tail_spec.rb
|
120
|
+
- spec/spec_helper.rb
|