|
|
@@ -1,4 +1,4 @@
|
|
1
|
|
-use std::io::{BufRead,Write,Read, StdinLock, StdoutLock};
|
|
|
1
|
+use std::io::{BufRead, Write, Read, StdinLock, StdoutLock};
|
|
2
|
2
|
use std::io;
|
|
3
|
3
|
|
|
4
|
4
|
use command::*;
|
|
|
@@ -16,14 +16,14 @@ pub struct Shell<R, W> {
|
|
16
|
16
|
pub name: String,
|
|
17
|
17
|
}
|
|
18
|
18
|
|
|
19
|
|
-impl <'a> Shell<R<'a>, W<'a>> {
|
|
|
19
|
+impl<'a> Shell<R<'a>, W<'a>> {
|
|
20
|
20
|
/// Creates a new Shell with a name, input and output.
|
|
21
|
21
|
pub fn new(input: R<'a>, output: W<'a>, name: String) -> Self {
|
|
22
|
22
|
Shell {
|
|
23
|
|
- reader : input,
|
|
24
|
|
- writer : output,
|
|
25
|
|
- should_exit : false,
|
|
26
|
|
- name : name,
|
|
|
23
|
+ reader: input,
|
|
|
24
|
+ writer: output,
|
|
|
25
|
+ should_exit: false,
|
|
|
26
|
+ name: name,
|
|
27
|
27
|
}
|
|
28
|
28
|
}
|
|
29
|
29
|
/// Initializes the Shell Loop.
|
|
|
@@ -46,46 +46,44 @@ impl <'a> Shell<R<'a>, W<'a>> {
|
|
46
|
46
|
|
|
47
|
47
|
/// Prints the shell prompt.
|
|
48
|
48
|
/// Waits for user input and returns the read line.
|
|
49
|
|
- fn prompt (&mut self) -> Result<String, &str> {
|
|
|
49
|
+ fn prompt(&mut self) -> Result<String, &str> {
|
|
50
|
50
|
match env::current_dir() {
|
|
51
|
51
|
Ok(pwd) => {
|
|
52
|
|
- let _ = self.writer.write(format!("{} {} > ", self.name, pwd.display()).as_bytes());
|
|
|
52
|
+ let _ = self.writer.write(
|
|
|
53
|
+ format!("{} {} > ", self.name, pwd.display())
|
|
|
54
|
+ .as_bytes(),
|
|
|
55
|
+ );
|
|
53
|
56
|
let _ = self.writer.flush();
|
|
54
|
57
|
|
|
55
|
58
|
let mut line: String = String::new();
|
|
56
|
59
|
let read_result = self.reader.read_line(&mut line);
|
|
57
|
60
|
match read_result {
|
|
58
|
|
- Ok(_) => {
|
|
59
|
|
- Ok(line)
|
|
60
|
|
- },
|
|
61
|
|
- Err(_) => {
|
|
62
|
|
- Err("Error reading.")
|
|
63
|
|
- },
|
|
|
61
|
+ Ok(_) => Ok(line),
|
|
|
62
|
+ Err(_) => Err("Error reading."),
|
|
64
|
63
|
}
|
|
65
|
|
- },
|
|
|
64
|
+ }
|
|
66
|
65
|
Err(_) => return Err("Path Error"),
|
|
67
|
66
|
}
|
|
68
|
67
|
}
|
|
69
|
68
|
|
|
70
|
69
|
/// Runs a command.
|
|
71
|
70
|
/// Currently only `cd` and `exit` are working.
|
|
72
|
|
- fn run(&mut self, command: Command) -> Result<(), CommandNotFoundError>{
|
|
|
71
|
+ fn run(&mut self, command: Command) -> Result<(), CommandNotFoundError> {
|
|
73
|
72
|
|
|
74
|
73
|
match command {
|
|
75
|
|
- Command::Empty => {},
|
|
|
74
|
+ Command::Empty => {}
|
|
76
|
75
|
Command::Exit => self.should_exit = true,
|
|
77
|
76
|
Command::Cd(_) => {
|
|
78
|
77
|
command.exec_cd();
|
|
79
|
|
- },
|
|
|
78
|
+ }
|
|
80
|
79
|
}
|
|
81
|
80
|
Ok(())
|
|
82
|
81
|
}
|
|
83
|
|
-
|
|
84
|
82
|
}
|
|
85
|
83
|
|
|
86
|
84
|
/// Implement Read-Trait for Tuple-Struct R
|
|
87
|
85
|
/// Simply calls `read` of StdinLock
|
|
88
|
|
-impl <'a>Read for R<'a> {
|
|
|
86
|
+impl<'a> Read for R<'a> {
|
|
89
|
87
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
|
|
90
|
88
|
self.0.read(buf)
|
|
91
|
89
|
}
|
|
|
@@ -93,7 +91,7 @@ impl <'a>Read for R<'a> {
|
|
93
|
91
|
|
|
94
|
92
|
/// Implement BufRead-Trait for Tuple-Struct R
|
|
95
|
93
|
/// Simply calls `fill_buff` and `consume` from StdinLock
|
|
96
|
|
-impl <'a>BufRead for R<'a> {
|
|
|
94
|
+impl<'a> BufRead for R<'a> {
|
|
97
|
95
|
fn fill_buf(&mut self) -> Result<&[u8], io::Error> {
|
|
98
|
96
|
self.0.fill_buf()
|
|
99
|
97
|
}
|
|
|
@@ -105,13 +103,12 @@ impl <'a>BufRead for R<'a> {
|
|
105
|
103
|
|
|
106
|
104
|
/// Implement Write-Trait for Tuple-Struct W
|
|
107
|
105
|
/// Simply calls `write` and `flush` from StdoutLock
|
|
108
|
|
-impl <'a>Write for W<'a> {
|
|
|
106
|
+impl<'a> Write for W<'a> {
|
|
109
|
107
|
fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
|
|
110
|
108
|
self.0.write(buf)
|
|
111
|
109
|
}
|
|
112
|
110
|
|
|
113
|
|
- fn flush(&mut self) -> Result<(), io::Error> {
|
|
|
111
|
+ fn flush(&mut self) -> Result<(), io::Error> {
|
|
114
|
112
|
self.0.flush()
|
|
115
|
113
|
}
|
|
116
|
114
|
}
|
|
117
|
|
-
|