mysh 0.1.17 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mysh.rb +28 -25
- data/lib/mysh/expression.rb +1 -9
- data/lib/mysh/external_ruby.rb +1 -1
- data/lib/mysh/internal/action.rb +3 -11
- data/lib/mysh/internal/action_pool.rb +6 -20
- data/lib/mysh/internal/actions/actions_path.rb +2 -2
- data/lib/mysh/internal/actions/cd.rb +14 -23
- data/lib/mysh/internal/actions/exit.rb +10 -7
- data/lib/mysh/internal/actions/gls.rb +63 -8
- data/lib/mysh/internal/actions/help.rb +20 -33
- data/lib/mysh/internal/actions/help/help.txt +4 -4
- data/lib/mysh/internal/actions/help/help_env.txt +14 -0
- data/lib/mysh/internal/actions/help/help_expr.txt +3 -2
- data/lib/mysh/internal/actions/help/help_gls.txt +41 -0
- data/lib/mysh/internal/actions/help/help_help.txt +7 -4
- data/lib/mysh/internal/actions/help/help_math.txt +1 -1
- data/lib/mysh/internal/actions/help/help_ruby.txt +21 -0
- data/lib/mysh/internal/actions/help/help_show.txt +12 -0
- data/lib/mysh/internal/actions/help/history.txt +19 -0
- data/lib/mysh/internal/actions/help/quick.txt +25 -0
- data/lib/mysh/internal/actions/help/sub_help.rb +41 -0
- data/lib/mysh/internal/actions/history.rb +46 -10
- data/lib/mysh/internal/actions/pwd.rb +18 -0
- data/lib/mysh/internal/actions/show.rb +22 -13
- data/lib/mysh/internal/actions/show/env.rb +43 -0
- data/lib/mysh/internal/actions/show/ruby.rb +50 -0
- data/lib/mysh/internal/actions/type.rb +27 -0
- data/lib/mysh/internal/actions/vls.rb +12 -12
- data/lib/mysh/internal/decorate.rb +2 -2
- data/lib/mysh/internal/format.rb +2 -4
- data/lib/mysh/internal/format/array.rb +23 -13
- data/lib/mysh/internal/format/bullets.rb +2 -2
- data/lib/mysh/internal/format/columns.rb +6 -11
- data/lib/mysh/internal/format/nil.rb +13 -0
- data/lib/mysh/internal/format/string.rb +23 -9
- data/lib/mysh/internal/manage.rb +2 -2
- data/lib/mysh/quick.rb +32 -0
- data/lib/mysh/user_input.rb +11 -4
- data/lib/mysh/user_input/parse.rb +1 -1
- data/lib/mysh/user_input/smart_source.rb +1 -1
- data/lib/mysh/version.rb +1 -1
- data/mysh.gemspec +1 -1
- data/samples/show.txt +1 -1
- data/tests/my_shell_tests.rb +25 -18
- metadata +16 -3
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
#* mysh/internal/actions/type.rb -- The mysh type command.
|
4
|
+
module Mysh
|
5
|
+
|
6
|
+
#* mysh/internal/actions/type.rb -- The mysh type command.
|
7
|
+
class TypeCommand < Action
|
8
|
+
|
9
|
+
#Execute the type command.
|
10
|
+
def call(args)
|
11
|
+
file_name = args.shift
|
12
|
+
|
13
|
+
@exec_binding = binding
|
14
|
+
|
15
|
+
if file_name
|
16
|
+
show_handlebar_file(file_name)
|
17
|
+
else
|
18
|
+
puts "Error: A text file must be specified."
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
#Add the type command to the library.
|
25
|
+
desc = 'Display a text file with optional embedded handlebars.'
|
26
|
+
COMMANDS.add_action(TypeCommand.new('type', desc))
|
27
|
+
end
|
@@ -1,22 +1,22 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
|
-
|
4
|
-
module Mysh
|
5
|
-
|
6
|
-
#* internal/actions/vls.rb -- The mysh module version ls command.
|
7
|
-
class Action
|
3
|
+
require 'vls'
|
8
4
|
|
9
|
-
|
10
|
-
|
5
|
+
#* mysh/internal/actions/vls.rb -- The mysh module vls (Version LS) command.
|
6
|
+
module Mysh
|
11
7
|
|
12
|
-
|
13
|
-
|
8
|
+
#* mysh/internal/actions/vls.rb -- The mysh module vls (Version LS) command.
|
9
|
+
class VlsCommand < Action
|
14
10
|
|
15
|
-
|
16
|
-
|
11
|
+
#Execute the vls command.
|
12
|
+
def call(args)
|
13
|
+
puts VersionLS.vls(args[0] || /./).mysh_bulletize, ""
|
17
14
|
end
|
18
15
|
|
19
16
|
end
|
20
17
|
|
18
|
+
#Add the vls command to the library.
|
19
|
+
desc = 'Display the loaded modules, matching the optional mask, that ' +
|
20
|
+
'have version info.'
|
21
|
+
COMMANDS.add_action(VlsCommand.new('vls <mask>', desc))
|
21
22
|
end
|
22
|
-
|
@@ -1,9 +1,9 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
|
-
#* internal/decorate.rb -- mysh internal file name beauty treatments.
|
3
|
+
#* mysh/internal/decorate.rb -- mysh internal file name beauty treatments.
|
4
4
|
module Mysh
|
5
5
|
|
6
|
-
|
6
|
+
#* mysh/internal/decorate.rb -- The mysh internal file name beauty treatments.
|
7
7
|
class Action
|
8
8
|
|
9
9
|
#Make the file name fit the local system.
|
data/lib/mysh/internal/format.rb
CHANGED
@@ -1,14 +1,11 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
|
-
#* internal/format.rb - Some formatting facilities for mysh.
|
3
|
+
#* mysh/internal/format.rb - Some formatting facilities for mysh.
|
4
4
|
module Mysh
|
5
5
|
|
6
6
|
#Assume an 80 column working area for formatting.
|
7
7
|
PAGE_WIDTH = 80
|
8
8
|
|
9
|
-
#Assume an in infinite page length for formatting.
|
10
|
-
PAGE_LENGTH = false
|
11
|
-
|
12
9
|
end
|
13
10
|
|
14
11
|
require_relative 'format/bullets'
|
@@ -16,4 +13,5 @@ require_relative 'format/columns'
|
|
16
13
|
require_relative 'format/array'
|
17
14
|
require_relative 'format/string'
|
18
15
|
require_relative 'format/object'
|
16
|
+
require_relative 'format/nil'
|
19
17
|
|
@@ -7,28 +7,38 @@ class Array
|
|
7
7
|
|
8
8
|
#Print out the array with efficient columns.
|
9
9
|
def puts_mysh_columns(page_width = Mysh::PAGE_WIDTH)
|
10
|
-
|
11
10
|
puts format_mysh_columns(page_width)
|
12
11
|
end
|
13
12
|
|
14
13
|
#Convert the array to strings with efficient columns.
|
15
14
|
#<br>Returns
|
16
|
-
#* An array of
|
17
|
-
|
18
|
-
|
15
|
+
#* An array of strings.
|
16
|
+
#<br>Endemic Code Smells
|
17
|
+
#* :reek:FeatureEnvy -- false positive.
|
18
|
+
def raw_mysh_columns(page_width)
|
19
|
+
builder = Mysh::ColumnizedPage.new(page_width)
|
19
20
|
|
20
|
-
|
21
|
+
each {|item| builder.add(item)}
|
21
22
|
|
22
23
|
builder.render
|
23
24
|
end
|
24
25
|
|
25
|
-
alias :format_description :
|
26
|
+
alias :format_description :raw_mysh_columns
|
27
|
+
|
28
|
+
#Convert the array to strings with efficient columns.
|
29
|
+
#<br>Returns
|
30
|
+
#* A string.
|
31
|
+
#<br>Endemic Code Smells
|
32
|
+
#* :reek:FeatureEnvy -- false positive.
|
33
|
+
def format_mysh_columns(page_width = Mysh::PAGE_WIDTH)
|
34
|
+
raw_mysh_columns(page_width).join("\n")
|
35
|
+
end
|
26
36
|
|
27
37
|
#Get the widest element of an array.
|
28
38
|
#<br>Returns
|
29
39
|
#* The width of the widest string in the array.
|
30
40
|
def mysh_column_width
|
31
|
-
|
41
|
+
max_by {|item| item.length}.length
|
32
42
|
end
|
33
43
|
|
34
44
|
|
@@ -40,16 +50,16 @@ class Array
|
|
40
50
|
end
|
41
51
|
|
42
52
|
#Convert the array to strings with bullet points.
|
43
|
-
#<br>
|
44
|
-
#*
|
53
|
+
#<br>Returns
|
54
|
+
#* A string.
|
55
|
+
#<br>Endemic Code Smells
|
56
|
+
#* :reek:FeatureEnvy -- false positive.
|
45
57
|
def mysh_bulletize(page_width = Mysh::PAGE_WIDTH)
|
46
58
|
builder = Mysh::BulletPoints.new(page_width)
|
47
59
|
|
48
|
-
|
49
|
-
builder.add(*pair)
|
50
|
-
end
|
60
|
+
each {|pair| builder.add(*pair)}
|
51
61
|
|
52
|
-
builder.render
|
62
|
+
builder.render.join("\n")
|
53
63
|
end
|
54
64
|
|
55
65
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
|
-
#* internal/format/bullets.rb - Print out bullet points.
|
3
|
+
#* mysh/internal/format/bullets.rb - Print out bullet points.
|
4
4
|
module Mysh
|
5
5
|
|
6
|
-
|
6
|
+
#* mysh/internal/format/bullets.rb - A class to display data in bullet points.
|
7
7
|
class BulletPoints
|
8
8
|
|
9
9
|
#Prepare a blank slate.
|
@@ -1,13 +1,13 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
|
-
#* internal/format/
|
3
|
+
#* mysh/internal/format/columns.rb - Print out data in neat columns.
|
4
4
|
module Mysh
|
5
5
|
|
6
|
-
|
6
|
+
#* mysh/internal/format/columns.rb - A class to display data in columns.
|
7
7
|
class ColumnizedPage
|
8
8
|
#Prepare a blank page.
|
9
|
-
def initialize(
|
10
|
-
@
|
9
|
+
def initialize(page_width)
|
10
|
+
@page_width = page_width
|
11
11
|
@page_data = []
|
12
12
|
end
|
13
13
|
|
@@ -52,24 +52,19 @@ module Mysh
|
|
52
52
|
end
|
53
53
|
|
54
54
|
#Make sure the page fits within its boundaries.
|
55
|
-
#<br>Returns
|
56
|
-
#* The number if items that did not fit in the page.
|
57
55
|
def adjust_configuration
|
58
56
|
while total_width >= @page_width
|
59
|
-
return @page_data.pop.length if rows == @page_length
|
60
57
|
add_a_row
|
61
58
|
end
|
62
|
-
|
63
|
-
0
|
64
59
|
end
|
65
60
|
|
66
61
|
#Add a row to the page, moving items as needed.
|
67
62
|
def add_a_row
|
68
|
-
|
63
|
+
new_rows = rows + 1
|
69
64
|
pool, @page_data = @page_data.flatten, []
|
70
65
|
|
71
66
|
until pool.empty?
|
72
|
-
@page_data << pool.shift(
|
67
|
+
@page_data << pool.shift(new_rows)
|
73
68
|
end
|
74
69
|
end
|
75
70
|
|
@@ -4,38 +4,52 @@
|
|
4
4
|
class String
|
5
5
|
|
6
6
|
#Create a bullet point description from this string.
|
7
|
+
#<br>Returns
|
8
|
+
#* An array of strings.
|
7
9
|
def format_description(max_width)
|
8
10
|
do_format_description(split(' ').each, max_width)
|
9
11
|
end
|
10
12
|
|
11
13
|
#Do the formatting legwork.
|
14
|
+
#<br>Returns
|
15
|
+
#* An array of strings.
|
12
16
|
def do_format_description(input, max_width)
|
13
|
-
|
17
|
+
buffer, build, empty = [], "", false
|
14
18
|
|
15
|
-
loop do
|
16
|
-
build = build.split_if_over(input.next, max_width,
|
17
|
-
|
19
|
+
loop do #Grab "words" of input, splitting off lines as needed.
|
20
|
+
empty = (build = build.split_if_over(input.next, max_width, buffer)
|
21
|
+
.split_if_huge(max_width, buffer)).empty?
|
18
22
|
end
|
19
23
|
|
20
|
-
|
24
|
+
unless empty
|
25
|
+
buffer << build
|
26
|
+
end
|
27
|
+
|
28
|
+
buffer
|
21
29
|
end
|
22
30
|
|
23
31
|
#Split if adding a word goes over a little.
|
32
|
+
#<br>Returns
|
33
|
+
#* A string.
|
24
34
|
def split_if_over(word, max_width, buffer)
|
25
|
-
word.prepend(" ") unless
|
35
|
+
word.prepend(" ") unless empty? #Add a space except for the first word.
|
36
|
+
|
26
37
|
word_len = word.length
|
27
38
|
|
28
39
|
if (length + word_len) >= max_width && word_len < max_width
|
29
|
-
buffer << self
|
30
|
-
word.lstrip
|
40
|
+
buffer << self #Line done, add to buffer.
|
41
|
+
word.lstrip #Start of a new line, remove the leading space.
|
31
42
|
else
|
32
43
|
self + word
|
33
44
|
end
|
34
|
-
|
35
45
|
end
|
36
46
|
|
37
47
|
#Split up a overlong blob of text.
|
48
|
+
#<br>Returns
|
49
|
+
#* A string.
|
38
50
|
def split_if_huge(max_width, buffer)
|
51
|
+
|
52
|
+
#Slice away any excess text into lines in the buffer.
|
39
53
|
while length >= max_width
|
40
54
|
buffer << slice!(0, max_width)
|
41
55
|
end
|
data/lib/mysh/internal/manage.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
|
-
#* internal/manage.rb -- Manage mysh internal commands.
|
3
|
+
#* mysh/internal/manage.rb -- Manage mysh internal commands.
|
4
4
|
module Mysh
|
5
5
|
|
6
6
|
#Set up the command action pool.
|
@@ -19,7 +19,7 @@ module Mysh
|
|
19
19
|
action, args = parse_command(str.chomp)
|
20
20
|
|
21
21
|
if (action)
|
22
|
-
action.
|
22
|
+
action.call(args)
|
23
23
|
:internal
|
24
24
|
end
|
25
25
|
end
|
data/lib/mysh/quick.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
#* mysh/internal/quick.rb -- The mysh internal quick commands.
|
4
|
+
module Mysh
|
5
|
+
|
6
|
+
#Try to execute the string as a quick command.
|
7
|
+
def self.try_execute_quick_command(str)
|
8
|
+
case str[0]
|
9
|
+
when '!'
|
10
|
+
HISTORY_COMMAND.call(parse_args(str[1...-1]))
|
11
|
+
:history
|
12
|
+
|
13
|
+
when '='
|
14
|
+
@exec_host.execute(str)
|
15
|
+
:expression
|
16
|
+
|
17
|
+
when '?'
|
18
|
+
HELP_COMMAND.call(parse_args(str[1...-1]))
|
19
|
+
:help
|
20
|
+
|
21
|
+
when '@'
|
22
|
+
SHOW_COMMAND.call(parse_args(str[1...-1]))
|
23
|
+
:show
|
24
|
+
|
25
|
+
else
|
26
|
+
false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
end
|
32
|
+
|
data/lib/mysh/user_input.rb
CHANGED
@@ -1,16 +1,24 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
|
+
require 'mini_readline'
|
4
|
+
|
3
5
|
require_relative 'user_input/smart_source'
|
4
6
|
require_relative 'user_input/parse'
|
5
7
|
require_relative 'user_input/handlebars'
|
6
8
|
|
7
|
-
#* user_input.rb -- Get
|
9
|
+
#* mysh/user_input.rb -- Get some text from the user.
|
8
10
|
module Mysh
|
9
11
|
|
12
|
+
class << self
|
13
|
+
#The input text source.
|
14
|
+
attr_reader :input
|
15
|
+
end
|
16
|
+
|
10
17
|
#Get one command from the user.
|
11
18
|
def self.get_command
|
12
|
-
|
13
|
-
|
19
|
+
initial_input = @input.readline(prompt: 'mysh>')
|
20
|
+
@input.instance_options[:initial] = ""
|
21
|
+
get_command_extra(initial_input)
|
14
22
|
end
|
15
23
|
|
16
24
|
private
|
@@ -36,4 +44,3 @@ module Mysh
|
|
36
44
|
|
37
45
|
end
|
38
46
|
|
39
|
-
|
data/lib/mysh/version.rb
CHANGED
data/mysh.gemspec
CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.summary = Mysh::SUMMARY
|
13
13
|
spec.description = "mysh -- a Ruby inspired command shell " +
|
14
14
|
"for CLI and application use."
|
15
|
-
spec.homepage = "
|
15
|
+
spec.homepage = "https://github.com/PeterCamilleri/mysh"
|
16
16
|
|
17
17
|
spec.license = "MIT"
|
18
18
|
|
data/samples/show.txt
CHANGED
@@ -7,5 +7,5 @@ Args = {{ args.inspect }}
|
|
7
7
|
|
8
8
|
{{ [["Numbers", Array.new(count){|i| (i+1)} ],
|
9
9
|
["Squares", Array.new(count){|i| (i+1)*(i+1)} ],
|
10
|
-
["Cubes", Array.new(count){|i| (i+1)*(i+1)*(i+1)} ]].mysh_bulletize
|
10
|
+
["Cubes", Array.new(count){|i| (i+1)*(i+1)*(i+1)} ]].mysh_bulletize }}
|
11
11
|
|
data/tests/my_shell_tests.rb
CHANGED
@@ -27,18 +27,18 @@ class MyShellTester < Minitest::Test
|
|
27
27
|
assert_equal(Class, Mysh::ExecHost.class)
|
28
28
|
|
29
29
|
assert_equal(Mysh::ActionPool, Mysh::COMMANDS.class)
|
30
|
-
assert_equal(Mysh::ActionPool, Mysh::
|
30
|
+
assert_equal(Mysh::ActionPool, Mysh::HELP.class)
|
31
31
|
end
|
32
32
|
|
33
33
|
def test_for_internal_commands
|
34
34
|
assert(Mysh::COMMANDS['exit'], "The exit command is missing.")
|
35
35
|
assert(Mysh::COMMANDS['quit'], "The quit command is missing.")
|
36
36
|
|
37
|
-
assert(Mysh::COMMANDS['history'],
|
38
|
-
assert(Mysh::COMMANDS['
|
37
|
+
assert(Mysh::COMMANDS['history'], "The history command is missing.")
|
38
|
+
assert(Mysh::COMMANDS['!<index>'], "The ! command is missing.")
|
39
39
|
|
40
40
|
assert(Mysh::COMMANDS['help'], "The help command is missing.")
|
41
|
-
assert(Mysh::COMMANDS['
|
41
|
+
assert(Mysh::COMMANDS['?<topic>'], "The ? command is missing.")
|
42
42
|
|
43
43
|
assert(Mysh::COMMANDS['cd'], "The cd command is missing.")
|
44
44
|
assert(Mysh::COMMANDS['pwd'], "The pwd command is missing.")
|
@@ -79,11 +79,19 @@ class MyShellTester < Minitest::Test
|
|
79
79
|
end
|
80
80
|
|
81
81
|
def test_some_formatting
|
82
|
+
assert_equal("1 4\n2 5\n3 ", [1,2,3,4,5].format_mysh_columns(5))
|
83
|
+
assert_equal(["1 4", "2 5", "3 "], [1,2,3,4,5].format_description(5))
|
84
|
+
|
85
|
+
assert_equal(["1 2", "3 4", "5"], "1 2 3 4 5".format_description(5))
|
86
|
+
|
87
|
+
assert_equal([], [].format_description(5))
|
88
|
+
assert_equal([], nil.format_description(5))
|
89
|
+
|
82
90
|
result =
|
83
|
-
|
84
|
-
"2 6 10 14 18 22 26 30 34 38 42 46 50 54 58 62 66 70 74 78 82 86 90 94 98 "
|
85
|
-
"3 7 11 15 19 23 27 31 35 39 43 47 51 55 59 63 67 71 75 79 83 87 91 95 99 "
|
86
|
-
"4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100"
|
91
|
+
"1 5 9 13 17 21 25 29 33 37 41 45 49 53 57 61 65 69 73 77 81 85 89 93 97 \n" +
|
92
|
+
"2 6 10 14 18 22 26 30 34 38 42 46 50 54 58 62 66 70 74 78 82 86 90 94 98 \n" +
|
93
|
+
"3 7 11 15 19 23 27 31 35 39 43 47 51 55 59 63 67 71 75 79 83 87 91 95 99 \n" +
|
94
|
+
"4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100"
|
87
95
|
|
88
96
|
assert_equal(result, (1..100).to_a.format_mysh_columns)
|
89
97
|
|
@@ -95,17 +103,16 @@ class MyShellTester < Minitest::Test
|
|
95
103
|
]
|
96
104
|
|
97
105
|
result =
|
98
|
-
|
99
|
-
" e_descriptive_name"
|
100
|
-
"key_west Semper ubi sub ubi. Semper ubi sub ubi. Semper ubi sub ubi. Semper"
|
101
|
-
" ubi sub ubi
|
102
|
-
"counting 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95"
|
103
|
-
" 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96"
|
104
|
-
" 2 7 12 17 22 27 32 37 42 47 52 57 62 67 72 77 82 87 92 97"
|
105
|
-
" 3 8 13 18 23 28 33 38 43 48 53 58 63 68 73 78 83 88 93 98"
|
106
|
-
" 4 9 14 19 24 29 34 39 44 49 54 59 64 69 74 79 84 89 94 99"
|
106
|
+
"key_largo /long_folder_name_one/long_folder_name_two/long_folder_name_three/fin\n" +
|
107
|
+
" e_descriptive_name\n" +
|
108
|
+
"key_west Semper ubi sub ubi. Semper ubi sub ubi. Semper ubi sub ubi. Semper\n" +
|
109
|
+
" ubi sub ubi.\n" +
|
110
|
+
"counting 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95\n" +
|
111
|
+
" 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96\n" +
|
112
|
+
" 2 7 12 17 22 27 32 37 42 47 52 57 62 67 72 77 82 87 92 97\n" +
|
113
|
+
" 3 8 13 18 23 28 33 38 43 48 53 58 63 68 73 78 83 88 93 98\n" +
|
114
|
+
" 4 9 14 19 24 29 34 39 44 49 54 59 64 69 74 79 84 89 94 99\n" +
|
107
115
|
"pie 3.141592653589793"
|
108
|
-
]
|
109
116
|
|
110
117
|
assert_equal(result, data.mysh_bulletize)
|
111
118
|
end
|