use std::env;
use std::fs;
use std::fs::File;
use std::io::{ErrorKind, Read};
#[derive()]
pub struct Terminal {
value: Vec<String>
}
impl Terminal {
pub fn new() -> Terminal {
Terminal {
value: env::args().collect()
}
}
pub fn get(&self) -> String {
let output = self.value.iter().next();
let p = match output {
Some(x) => x,
None => "Error get()"
};
p.to_string()
}
pub fn get_index(&self, x: usize) -> String {
let &n = &self.value.get(x);
let c = match n {
Some(x) => x,
None => "Error get_index()"
};
c.to_string()
}
pub fn is_err(&self, x: usize) -> bool {
let &n = &self.value.get(x);
let c = match n {
Some(_) => false,
None => true
};
c
}
pub fn read_file(&self) -> String{
let mut output = String::new();
let file_name = self.get_index(1);
let mut file = File::open(file_name.clone()).unwrap_or_else(|error| {
if error.kind() == ErrorKind::NotFound {
panic!("Error in Searching File: {:#?}", error);
}
else {
panic!("Error: {:#?}", error);
}
});
file.read_to_string( &mut output).unwrap();
output
}
pub fn find(&self, word: String) -> String {
let p = self.read_file().to_ascii_lowercase();
let out: String = p.lines().filter(|line| line.contains(word.as_str())).collect();
out
}
pub fn create(&self, name: String, text: String) -> std::io::Result<()>{
File::create(name.clone())?;
fs::write(name, text)?;
Ok(())
}
}