Implémentation de Buffer et utilisation dans io/mod.rs

This commit is contained in:
Hippolyte Chauvin 2023-11-01 14:10:45 +01:00
parent d8fb7a5478
commit 6ca8606081
3 changed files with 38 additions and 16 deletions

View file

@ -0,0 +1,24 @@
use std::io as stdio;
use stdio::stdin;
pub struct Buffer {
buffer: String,
}
impl Buffer {
pub fn new() -> Self {
Self {
buffer: String::new(),
}
}
pub fn read_line(&mut self) -> stdio::Result<usize> {
self.buffer.clear();
let result: stdio::Result<usize> = stdin().read_line(&mut self.buffer);
if result.is_err() {
eprintln!("error: invalid UTF-8 characters were read");
}
result
}
}

View file

@ -1,9 +1,4 @@
use std::io;
use io::{
stdin,
stdout,
Write,
};
mod r#in;
mod r#out;
@ -11,27 +6,21 @@ mod r#out;
mod parser;
pub fn run() -> i32 {
let mut buffer = String::new();
let mut buffer = r#in::Buffer::new();
let mut bytes_read_result: io::Result<usize>;
let mut bytes_read: usize = 1;
while bytes_read != 0 {
buffer.clear();
print!("$ ");
if stdout().flush().is_err() {
eprintln!("error: can't fully flush stdout or reached EOF");
}
bytes_read_result = stdin().read_line(&mut buffer);
out::print_ps1();
bytes_read_result = buffer.read_line();
if bytes_read_result.is_err() {
eprintln!("error: invalid UTF-8 characters were read");
bytes_read = 1;
} else {
bytes_read = bytes_read_result.unwrap();
if bytes_read != 0 {
let command_vec: Vec<String> = parser::parse(&buffer);
println!("{:?}", command_vec);
//let command_vec: Vec<String> = parser::parse(&buffer);
//println!("{:?}", command_vec);
}
}
}

View file

@ -0,0 +1,9 @@
use std::io::stdout;
use std::io::Write;
pub fn print_ps1() {
print!("$ ");
if stdout().flush().is_err() {
eprintln!("error: can't fully flush stdout or reached EOF");
}
}