From afd9e8dcf410f59fe90071bf8251d31856f16f2c Mon Sep 17 00:00:00 2001 From: Jeff Date: Thu, 28 Dec 2023 18:51:39 -0500 Subject: [PATCH] Add basic text editor --- Cargo.lock | 13 +++++++++++++ Cargo.toml | 1 + src/app.rs | 19 ++++++++++++++++--- src/main.rs | 6 +++++- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 49eb00a..460bed6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -431,6 +431,7 @@ dependencies = [ "tracing", "tracing-error", "tracing-subscriber", + "tui-textarea", ] [[package]] @@ -1924,6 +1925,18 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "tui-textarea" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3e38ced1f941a9cfc923fbf2fe6858443c42cc5220bfd35bdd3648371e7bd8e" +dependencies = [ + "crossterm", + "ratatui", + "regex", + "unicode-width", +] + [[package]] name = "unicode-bidi" version = "0.3.14" diff --git a/Cargo.toml b/Cargo.toml index b24af9a..a78c24a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,3 +22,4 @@ tokio-util = "0.7.10" tracing = "0.1.40" tracing-error = "0.2.0" tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } +tui-textarea = { version = "0.4.0", features = ["search"] } diff --git a/src/app.rs b/src/app.rs index 694f707..af7c2dd 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,19 +1,26 @@ -use ratatui::{widgets::Paragraph, Frame}; +use ratatui::{style::Style, Frame}; use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; +use tui_textarea::TextArea; use crate::{Action, Event, Result, Tui}; pub struct App { action_rx: UnboundedReceiver, action_tx: UnboundedSender, + source_buffers: Vec, should_quit: bool, } impl App { - pub fn new(action_rx: UnboundedReceiver, action_tx: UnboundedSender) -> Self { + pub fn new( + action_rx: UnboundedReceiver, + action_tx: UnboundedSender, + source_buffers: Vec, + ) -> Self { App { action_rx, action_tx, + source_buffers, should_quit: false, } } @@ -66,7 +73,13 @@ impl App { } pub fn ui(&mut self, frame: &mut Frame) { - frame.render_widget(Paragraph::new("app"), frame.size()) + let first_buffer = self.source_buffers.first().unwrap(); + let mut textarea = + TextArea::new(first_buffer.lines().map(|line| line.to_string()).collect()); + + textarea.set_line_number_style(Style::default()); + + frame.render_widget(textarea.widget(), frame.size()) } fn handle_event(&self, event: Event) -> Result> { diff --git a/src/main.rs b/src/main.rs index 08c672e..bcb9da8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,11 @@ use tokio::sync::mpsc; #[tokio::main] async fn main() { let (action_tx, action_rx) = mpsc::unbounded_channel(); - let mut app = App::new(action_rx, action_tx); + let mut app = App::new( + action_rx, + action_tx, + vec!["(output 'Hello, world!')".to_string()], + ); let run_result = app.run().await; if let Err(report) = run_result {