ratatui icon indicating copy to clipboard operation
ratatui copied to clipboard

Idea: Merge `Terminal` with the `Backend` trait

Open EdJoPaTo opened this issue 1 year ago • 2 comments

Problem

#1180 simplifies the usage of the CrosstermBackend a lot. While working on it into_terminal was thought about. (Personally I don't like it as it creates another way of doing Terminal::new.) But this shows that there exists a wish for simplifying the usage even further here.

Solution

Can Terminal be merged into the Backend trait? This would be a breaking change for sure but it would simplify the usage of ratatui. #1180 introduces a impl Drop for the CrosstermBackend while also one exists for the Terminal. Currently, the question exists which one should clean up which parts. In my opinion the backend knows best how to clean itself up. Especially as Terminal only calls methods on the Backend trait.

 let backend = CrosstermBackend::whatever()?;
-let terminal = Terminal::new(backend)?;
-App::default().run(terminal)?;
+App::default().run(backend)?;

EdJoPaTo avatar Jul 31 '24 10:07 EdJoPaTo

I like how you're thinking out of the box on this. I've paused a bit on 1180 again as I don't think it's still quite there either. I wonder if you'd expand on the idea here a bit. It seems like there's a lot of behavior that is terminal agnostic and then a lot that is backend specific.

joshka avatar Aug 02 '24 05:08 joshka

Stuff like this points to merging or at least moving stuff around:

https://github.com/ratatui-org/ratatui/blob/cd93547db869aab3dbca49f11667a8091a7077bd/src/terminal/terminal.rs#L427-L429

Also, there can be default implementations on traits using other trait methods so it doesn't need to be reimplemented several times. As Terminal is often a wrapper on Backend methods, maybe move them to methods implemented on the Backend trait.

EdJoPaTo avatar Aug 02 '24 07:08 EdJoPaTo