use std::fs::File;
use std::io::{self, BufRead, BufReader, Read, Write, Result};
#[derive(Debug,PartialEq , Eq ,PartialOrd , Ord)]
pub struct TextEditor {
content: String,
}
impl TextEditor {
pub fn new() -> Self {
TextEditor {
content: String::new(),
}
}
pub fn add_text(&mut self, new_text: &str) {
self.content.push_str(new_text);
}
pub fn remove_text(&mut self, substring: &str) {
if let Some(pos) = self.content.find(substring) {
self.content.replace_range(pos..pos + substring.len(), "");
}
}
pub fn replace_text(&mut self, old: &str, new: &str) {
self.content = self.content.replace(old, new);
}
pub fn get_content(&self) -> &str {
&self.content
}
pub fn clear(&mut self) {
self.content.clear();
}
pub fn save_to_file(&self, filename: &str) -> Result<()> {
let mut file = File::create(filename)?;
file.write_all(self.content.as_bytes())?;
Ok(())
}
pub fn open_file(filename: &str) -> Result<Self> {
let mut file = File::open(filename)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(Self{ content: contents})
}
pub fn read_file(&self,filename: &str) -> Result<Self> {
let mut file = File::open(filename)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(Self { content: contents })
}
pub fn get_line(filename: &str, line_number: usize) -> Result<String> {
let file = File::open(filename)?;
let reader = BufReader::new(file);
for (i, line) in reader.lines().enumerate() {
let line = line?;
if i + 1 == line_number {
return Ok(line);
}
}
Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!("Line {} not found in file '{}'", line_number, filename),
))
}
}