Class: STShell

Inherits:
Object
  • Object
show all
Defined in:
lib/repl.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_addr, port) ⇒ STShell

Returns a new instance of STShell.



17
18
19
20
# File 'lib/repl.rb', line 17

def initialize(server_addr, port)
    @port = port
    @client = SimpleTransfer::Sender.new(server_addr, port, logging=true)
end

Class Method Details

.list_files(path) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/repl.rb', line 22

def self.list_files(path)
    Dir.entries(path).each do |f|
        if File.directory?(f)
            puts "#{f}/"
        else
            puts f
        end
    end
end

Instance Method Details

#eval(query) ⇒ Object



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
61
62
# File 'lib/repl.rb', line 33

def eval(query)
    p1, p2 = query.split(" ")
    if p1 == "clear"
        system "clear"
    elsif p1 == "send_file"
        if File.directory?(p2)
            puts "You can't send a directory as a file, friend!"
        else
            @client.send_file(p2)
        end
    elsif p1 == "send_directory"
        if not File.directory?(p2)
            puts "You can't send a file as a directory, friend!"
        else
            @client.send_directory(p2)
        end
    elsif p1 == "cd"
        Dir.chdir(p2)
    elsif p1 == "ls"
        if p2.nil?
          STShell.list_files(Dir.pwd)
        else
            STShell.list_files(p2)
        end
    elsif p1 == "help"
        help()
    else
        p "I don't recognize this command: `#{p1}` friend!"
    end
end

#helpObject



64
65
66
67
68
69
70
# File 'lib/repl.rb', line 64

def help()
    puts "Type 'send_file <filename>' to send a file"
    puts "Type 'send_directory <directory>' to send a directory"
    puts "Type 'cd <directory>' to change directory"
    puts "Type 'ls <directory>' to list directory contents"
    puts "Type help to see this message"
end

#init_loopObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/repl.rb', line 82

def init_loop()
    puts $logo
    puts "STShell initialized on port #{@port}!"
    puts "Hello friend, thank you for using simple transfer!"
    puts "Are you ready to drown in simplicity?"
    puts "It is indeed extremely simple:"
    help()
    repl()
    #real...
    # let it be
end


78
79
80
# File 'lib/repl.rb', line 78

def print()
    Kernel.print("[ST]🍀 #{Dir.pwd} >>>> ")
end

#readObject



72
73
74
75
76
# File 'lib/repl.rb', line 72

def read()
    query = $stdin.gets.chomp
    #puts query, echo is very annoying
    return query
end

#replObject



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/repl.rb', line 94

def repl()
    while true
        print()
        query = read()
        begin
            eval(query)
        rescue => e
            p e
            puts "An error occurred, friend! >:3"
        end
    end
end