-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathshell.rb
46 lines (34 loc) · 1.02 KB
/
shell.rb
1
2
3
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
require 'highline'
require 'cli-console'
class ShellUI
private
extend CLI::Task
public
usage 'Usage: ls'
desc 'List file information about current directory'
def ls(params)
Dir.foreach(Dir.pwd) do |file|
puts file
end
end
usage 'Usage: pwd'
desc 'Display current directory'
def pwd(params)
puts "Current directory: #{Dir.pwd}"
end
usage 'Usage: cd <Directory>'
desc 'Change current directory'
def cd(params)
Dir.chdir(params[0]) unless params.empty?
end
end
io = HighLine.new
shell = ShellUI.new
console = CLI::Console.new(io)
console.addCommand('ls', shell.method(:ls), 'List files')
console.addCommand('pwd', shell.method(:pwd), 'Current directory')
console.addCommand('cd', shell.method(:cd), 'Change directory')
console.addHelpCommand('help', 'Help')
console.addExitCommand('exit', 'Exit from program')
console.addAlias('quit', 'exit')
console.start("%s> ",[Dir.method(:pwd)])