|
|
@@ -1,8 +1,13 @@
|
|
1
|
1
|
use std::io;
|
|
2
|
2
|
use std::io::prelude::*;
|
|
3
|
|
-use command;
|
|
|
3
|
+use std::io::BufRead;
|
|
|
4
|
+use std::io::Read;
|
|
4
|
5
|
|
|
5
|
|
-struct Shell<R, W> {
|
|
|
6
|
+
|
|
|
7
|
+pub struct R;
|
|
|
8
|
+pub struct W;
|
|
|
9
|
+
|
|
|
10
|
+pub struct Shell<R, W> {
|
|
6
|
11
|
pub reader: R,
|
|
7
|
12
|
pub writer: W,
|
|
8
|
13
|
pub should_exit: bool,
|
|
|
@@ -13,40 +18,72 @@ impl Shell<R, W> {
|
|
13
|
18
|
/// Creates a new Shell.
|
|
14
|
19
|
pub fn new(input: R, output: W, name: String) -> Self {
|
|
15
|
20
|
Shell {
|
|
16
|
|
- reader = input,
|
|
17
|
|
- writer = output,
|
|
18
|
|
- should_exit = false,
|
|
19
|
|
- name = name,
|
|
|
21
|
+ reader : input,
|
|
|
22
|
+ writer : output,
|
|
|
23
|
+ should_exit : false,
|
|
|
24
|
+ name : name,
|
|
20
|
25
|
}
|
|
21
|
26
|
}
|
|
22
|
27
|
/// Initializes the Shell Loop
|
|
23
|
|
- pub fn start() -> Result<Self, &str> {
|
|
24
|
|
- shell_loop();
|
|
|
28
|
+ pub fn start<'a>(&self) -> Result<(), &'a str> {
|
|
|
29
|
+ self.shell_loop();
|
|
|
30
|
+ Ok(())
|
|
25
|
31
|
}
|
|
|
32
|
+
|
|
26
|
33
|
/// Waits for user inputs.
|
|
27
|
|
- fn shell_loop() -> Result {
|
|
|
34
|
+ fn shell_loop(&self) -> Result<(),()> {
|
|
28
|
35
|
loop {
|
|
29
|
36
|
}
|
|
30
|
37
|
}
|
|
|
38
|
+
|
|
|
39
|
+ /*
|
|
31
|
40
|
/// Prints the shell prompt.
|
|
32
|
41
|
fn prompt() -> Result<Option, &str> {
|
|
33
|
42
|
}
|
|
34
|
43
|
/// Runs a command.
|
|
35
|
44
|
fn run(&str) {
|
|
36
|
45
|
}
|
|
|
46
|
+
|
|
|
47
|
+ */
|
|
37
|
48
|
}
|
|
38
|
49
|
|
|
39
|
|
-impl BufRead for Shell<R, W> {
|
|
40
|
|
- fn fill_buf(&mut self) -> Result<&[u8]> {
|
|
41
|
|
- stdin.fill_buf()
|
|
|
50
|
+
|
|
|
51
|
+impl Read for R {
|
|
|
52
|
+ fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
|
|
|
53
|
+ let stdin = io::stdin();
|
|
|
54
|
+ let mut stdin = stdin.lock();
|
|
|
55
|
+
|
|
|
56
|
+
|
|
|
57
|
+ let buffer = stdin.read(buf);
|
|
|
58
|
+
|
|
|
59
|
+ match buffer {
|
|
|
60
|
+ Ok(x) => {
|
|
|
61
|
+ println!("{} ||| {:?}", x, buffer);
|
|
|
62
|
+ stdin.consume(x);
|
|
|
63
|
+ Ok(x)
|
|
|
64
|
+ },
|
|
|
65
|
+ Err(_) => Err(io::Error::new(io::ErrorKind::InvalidInput, "jhrthhzruzkikz")),
|
|
|
66
|
+ }
|
|
|
67
|
+ }
|
|
|
68
|
+}
|
|
|
69
|
+
|
|
|
70
|
+impl BufRead for R {
|
|
|
71
|
+
|
|
|
72
|
+ fn fill_buf(&mut self) -> Result<&[u8], io::Error> {
|
|
|
73
|
+ Ok(&[11])
|
|
42
|
74
|
}
|
|
|
75
|
+
|
|
43
|
76
|
fn consume(&mut self, amt: usize) {
|
|
|
77
|
+ let mut buffer = self.fill_buf;
|
|
|
78
|
+ if buffer.len <= amt {
|
|
|
79
|
+
|
|
|
80
|
+ }
|
|
44
|
81
|
}
|
|
45
|
82
|
}
|
|
46
|
83
|
|
|
47
|
|
-impl Write for Shell<R, W> {
|
|
48
|
|
- fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
|
|
84
|
+impl Write for W {
|
|
|
85
|
+ fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
|
|
49
|
86
|
}
|
|
50
|
|
- fn flush(&mut self) -> Result<()> {
|
|
|
87
|
+ fn flush(&mut self) -> Result<(), io::Error> {
|
|
51
|
88
|
}
|
|
52
|
89
|
}
|