Skip to content

Commit

Permalink
maybe getting there
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpasmantier committed Oct 11, 2024
1 parent c7bc99a commit b178bd1
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 48 deletions.
8 changes: 3 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ use crate::{
config::Config,
event::{Event, EventLoop, Key},
render::{render, RenderingTask},
tui::Tui,
};

pub struct App {
Expand Down Expand Up @@ -117,14 +116,13 @@ impl App {
}

pub async fn run(&mut self) -> Result<Option<Entry>> {
let mut tui = Tui::new()?.frame_rate(self.frame_rate);
info!("Starting event loop");
info!("Starting backend event loop");
let event_loop = EventLoop::new(self.tick_rate, true);
info!("Started event loop");
self.event_rx = event_loop.rx;
self.event_abort_tx = event_loop.abort_tx;

// Rendering loop
debug!("Starting rendering loop");
let (render_tx, render_rx) = mpsc::unbounded_channel();
self.render_tx = render_tx.clone();
let action_tx_r = self.action_tx.clone();
Expand All @@ -133,7 +131,6 @@ impl App {
let frame_rate = self.frame_rate;
tokio::spawn(async move {
render(
&mut tui,
render_rx,
//render_tx,
action_tx_r,
Expand All @@ -145,6 +142,7 @@ impl App {
});

// event handling loop
debug!("Starting event handling loop");
let action_tx = self.action_tx.clone();
loop {
// handle event and convert to action
Expand Down
9 changes: 8 additions & 1 deletion src/components/finders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ impl Entry {
}

pub fn stdout_repr(&self) -> String {
self.name.clone() + &self.line_number.map_or(String::new(), |n| format!(":{n}"))
let mut repr = self.name.clone();
if let Some(line_number) = self.line_number {
repr.push_str(&format!(":{}", line_number));
}
if let Some(preview) = &self.preview {
repr.push_str(&format!("\n{}", preview));
}
repr
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::{stdout, IsTerminal, Write};
use std::io::{stdout, Write};

use clap::Parser;
use cli::Cli;
Expand Down Expand Up @@ -27,6 +27,7 @@ async fn main() -> Result<()> {
let mut app = App::new(args.channel, args.tick_rate, args.frame_rate)?;
if let Some(entry) = app.run().await? {
// print entry to stdout
stdout().flush()?;
info!("{:?}", entry);
stdout().write_all(entry.stdout_repr().as_bytes())?;
}
Expand Down
16 changes: 13 additions & 3 deletions src/render.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use color_eyre::Result;
use ratatui::layout::Rect;
use std::sync::Arc;
use tracing::debug;

use tokio::{
select,
Expand All @@ -14,6 +15,7 @@ use crate::{
tui::Tui,
};

#[derive(Debug)]
pub enum RenderingTask {
ClearScreen,
Render,
Expand All @@ -24,15 +26,18 @@ pub enum RenderingTask {
}

pub async fn render(
tui: &mut Tui,
mut render_rx: mpsc::UnboundedReceiver<RenderingTask>,
action_tx: mpsc::UnboundedSender<Action>,
config: Config,
television: Arc<Mutex<Television>>,
frame_rate: f64,
) -> Result<()> {
let mut tui = Tui::new()?.frame_rate(frame_rate);

debug!("Entering tui");
tui.enter()?;

debug!("Registering action handler and config handler");
television
.lock()
.await
Expand All @@ -51,17 +56,22 @@ pub async fn render(
}
maybe_task = render_rx.recv() => {
if let Some(task) = maybe_task {
debug!("Received task: {:?}", task);
match task {
RenderingTask::ClearScreen => {
tui.terminal.clear()?;
}
RenderingTask::Render => {
let mut television = television.lock().await;
tui.terminal.draw(|frame| {
debug!("Drawing television");
tui.terminal.try_draw(|frame| {
debug!("Drawing frame");
if let Err(err) = television.draw(frame, frame.area()) {
debug!("Failed to draw: {:?}", err);
let _ = action_tx
.send(Action::Error(format!("Failed to draw: {:?}", err)));
.send(Action::Error(format!("Failed to draw: {err:?}")));
}
Ok::<() ,std::io::Error>(())
})?;
}
RenderingTask::Resize(w, h) => {
Expand Down
101 changes: 63 additions & 38 deletions src/tui.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
use std::{
io::{stderr, BufWriter, Stderr},
io::{stderr, stdout, BufWriter, Stderr, Write},
ops::{Deref, DerefMut},
};

use color_eyre::Result;
use crossterm::{
cursor,
terminal::{EnterAlternateScreen, LeaveAlternateScreen},
terminal::{
disable_raw_mode, enable_raw_mode, is_raw_mode_enabled, EnterAlternateScreen,
LeaveAlternateScreen,
},
ExecutableCommand,
};
use ratatui::backend::CrosstermBackend as Backend;
use ratatui::backend::CrosstermBackend;
use tokio::task::JoinHandle;
use tracing::debug;

#[derive(Copy, Clone, Debug, Default)]
enum IoStream {
#[default]
Stdout,
BlockBufferedStderr,
}

impl IoStream {
fn as_stream(&self) -> Result<Box<dyn Write>> {
Ok(match self {
IoStream::Stdout => Box::new(stdout()),
IoStream::BlockBufferedStderr => Box::new(BufWriter::new(stderr())),
})
}
}

pub struct Tui {
pub terminal: ratatui::Terminal<Backend<BufWriter<Stderr>>>,
pub io_stream: Box<dyn Write>,
pub task: JoinHandle<()>,
pub frame_rate: f64,
}

impl Tui {
pub fn new() -> Result<Self> {
Ok(Self {
terminal: ratatui::Terminal::new(Backend::new(BufWriter::new(stderr())))?,
io_stream: IoStream::BlockBufferedStderr.as_stream()?,
task: tokio::spawn(async {}),
frame_rate: 60.0,
})
Expand All @@ -31,26 +52,27 @@ impl Tui {
self
}

pub fn size(&self) -> Result<(u16, u16)> {
Ok(crossterm::terminal::size()?)
}

pub fn enter(&mut self) -> Result<()> {
crossterm::terminal::enable_raw_mode()?;
crossterm::execute!(
self.terminal.backend_mut(),
EnterAlternateScreen,
cursor::Hide
)?;
enable_raw_mode()?;

self.io_stream.execute(EnterAlternateScreen)?;
self.io_stream.execute(cursor::Hide)?;
Ok(())
}

pub fn exit(&mut self) -> Result<()> {
if crossterm::terminal::is_raw_mode_enabled()? {
self.flush()?;
crossterm::execute!(
self.terminal.backend_mut(),
cursor::Show,
LeaveAlternateScreen
)?;
crossterm::terminal::disable_raw_mode()?;
if is_raw_mode_enabled()? {
debug!("Exiting terminal");
self.io_stream.flush()?;
self.io_stream.execute(cursor::Show)?;
self.io_stream.execute(LeaveAlternateScreen)?;
disable_raw_mode()?;
}

Ok(())
}

Expand All @@ -67,22 +89,25 @@ impl Tui {
}
}

impl Deref for Tui {
type Target = ratatui::Terminal<Backend<BufWriter<Stderr>>>;

fn deref(&self) -> &Self::Target {
&self.terminal
}
}

impl DerefMut for Tui {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.terminal
}
}

impl Drop for Tui {
fn drop(&mut self) {
self.exit().unwrap();
}
}
//impl Deref for Tui {
// type Target = ratatui::Terminal<CrosstermBackend<Stderr>>;
//
// fn deref(&self) -> &Self::Target {
// &self.terminal
// }
//}
//
//impl DerefMut for Tui {
// fn deref_mut(&mut self) -> &mut Self::Target {
// &mut self.terminal
// }
//}
//
//impl Drop for Tui {
// fn drop(&mut self) {
// match self.exit() {
// Ok(_) => debug!("Successfully exited terminal"),
// Err(e) => debug!("Failed to exit terminal: {:?}", e),
// }
// }
//}

0 comments on commit b178bd1

Please sign in to comment.