further code refactoring

This commit is contained in:
RaphyJake 2021-11-03 12:00:08 +01:00
parent 769e5d860e
commit fb630fd5a7
4 changed files with 33 additions and 39 deletions

View File

@ -1,4 +1,3 @@
use crossterm::style;
use crossterm::style::Color;
use crossterm::style::Color::Rgb;

View File

@ -5,3 +5,5 @@ pub enum Events {
Quit,
Scroll,
}
// TODO: Wrap key events in my own event type

View File

@ -109,18 +109,18 @@ impl Renderer {
}
async fn refresh_screen(&mut self) -> Result<(), Box<dyn Error + Send + Sync>> {
Terminal::cursor_hide();
self.terminal.cursor_hide()?;
self.draw_rows().await;
self.draw_info().await;
self.terminal.cursor_position(&self.calculate_pos().await);
Terminal::cursor_show();
self.terminal.cursor_show()?;
self.terminal.flush()?;
Ok(())
}
async fn refresh_status(&mut self) -> Result<(), Box<dyn Error + Send + Sync>> {
Terminal::cursor_hide();
self.terminal.cursor_hide()?;
let s = self.terminal.size();
@ -132,7 +132,7 @@ impl Renderer {
self.draw_info().await;
self.terminal.cursor_position(&self.calculate_pos().await);
Terminal::cursor_show();
self.terminal.cursor_show();
self.terminal.flush()?;
Ok(())
}
@ -211,7 +211,7 @@ impl Renderer {
self.terminal.clear_current_line();
print!("{}\r", status2);
Terminal::reset_color();
self.terminal.reset_color();
}
async fn scroll(&mut self) {

View File

@ -21,20 +21,25 @@ use crossterm::{
use tokio::signal::unix::{signal, SignalKind};
use tokio::sync::mpsc::UnboundedSender;
/// Structure to represent the size of the terminal.
#[derive(Clone, Copy)]
pub struct Size {
pub width: u16,
pub height: u16,
}
/// Structure to represent a terminal interface.
///
/// Size is stored in a arclock, so it can be updated from another thread.
pub struct Terminal {
size: Arc<RwLock<Size>>,
pub sender: UnboundedSender<Events>,
stdout: Stdout,
inputsender: UnboundedSender<KeyEvent>,
keythread: Option<std::thread::JoinHandle<()>>,
pub sender: UnboundedSender<Events>, // sends screen refresh events to the renderer.
inputsender: UnboundedSender<KeyEvent>, // sends input to the editor
keythread: Option<std::thread::JoinHandle<()>>, // thread that reads input
}
impl Terminal {
@ -43,31 +48,21 @@ impl Terminal {
ist: UnboundedSender<KeyEvent>,
) -> Result<Self, Box<dyn Error + Send + Sync>> {
let size = size()?;
// let out = stdout().into_raw_mode()?;
// let screen = AlternateScreen::from(out);
enable_raw_mode()?;
let stdout = stdout();
// let (st, rt) = unbounded_channel();
// let (ist, irt) = unbounded_channel();
let mut res = Self {
size: Arc::new(RwLock::new(Size {
width: size.0,
height: size.1,
})),
stdout,
// screen,
sender: st,
// receiver: rt,
inputsender: ist,
// inputreceiver: irt,
// sigthread: None,
keythread: None,
};
// res.start_update_thread()?;
res.start_reading()?;
Ok(res)
@ -99,14 +94,11 @@ impl Terminal {
Event::Key(event) => {
isender.send(event);
}
Event::Mouse(event) => {} //println!("{:?}", event),
Event::Mouse(event) => {}
// Mouse events will be supported later?
Event::Resize(width, height) => {
let s = size().unwrap();
let mut w = sarc.write().unwrap();
*w = Size {
width: s.0,
height: s.1,
};
*w = Size { width, height };
sender.send(Events::Refresh);
}
}
@ -153,28 +145,29 @@ impl Terminal {
Ok(())
}
pub fn cursor_hide() {
queue!(stdout(), Hide);
pub fn cursor_hide(&mut self) -> Result<(), Box<dyn Error + Send + Sync>> {
queue!(self.stdout, Hide)?;
Ok(())
}
pub fn cursor_show() {
queue!(stdout(), Show);
pub fn cursor_show(&mut self) -> Result<(), Box<dyn Error + Send + Sync>> {
queue!(self.stdout, Show)?;
Ok(())
}
pub fn set_bg_color(&mut self, color: Color) {
queue!(self.stdout, SetBackgroundColor(color));
pub fn set_bg_color(&mut self, color: Color) -> Result<(), Box<dyn Error + Send + Sync>> {
queue!(self.stdout, SetBackgroundColor(color))?;
Ok(())
}
// pub fn reset_bg_color() {
// print!("{}", color::Bg(color::Reset))
// }
pub fn set_text_color(&mut self, color: Color) {
queue!(self.stdout, SetForegroundColor(color));
pub fn set_text_color(&mut self, color: Color) -> Result<(), Box<dyn Error + Send + Sync>> {
queue!(self.stdout, SetForegroundColor(color))?;
Ok(())
}
pub fn reset_color() {
queue!(stdout(), ResetColor);
pub fn reset_color(&mut self) -> Result<(), Box<dyn Error + Send + Sync>> {
queue!(self.stdout, ResetColor)?;
Ok(())
}
pub fn quit(&self) -> Result<(), Box<dyn Error>> {