Add basic text editor

This commit is contained in:
Jeff 2023-12-28 18:51:39 -05:00
parent cf6acd9ae7
commit afd9e8dcf4
4 changed files with 35 additions and 4 deletions

13
Cargo.lock generated
View File

@ -431,6 +431,7 @@ dependencies = [
"tracing", "tracing",
"tracing-error", "tracing-error",
"tracing-subscriber", "tracing-subscriber",
"tui-textarea",
] ]
[[package]] [[package]]
@ -1924,6 +1925,18 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" 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]] [[package]]
name = "unicode-bidi" name = "unicode-bidi"
version = "0.3.14" version = "0.3.14"

View File

@ -22,3 +22,4 @@ tokio-util = "0.7.10"
tracing = "0.1.40" tracing = "0.1.40"
tracing-error = "0.2.0" tracing-error = "0.2.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tui-textarea = { version = "0.4.0", features = ["search"] }

View File

@ -1,19 +1,26 @@
use ratatui::{widgets::Paragraph, Frame}; use ratatui::{style::Style, Frame};
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
use tui_textarea::TextArea;
use crate::{Action, Event, Result, Tui}; use crate::{Action, Event, Result, Tui};
pub struct App { pub struct App {
action_rx: UnboundedReceiver<Action>, action_rx: UnboundedReceiver<Action>,
action_tx: UnboundedSender<Action>, action_tx: UnboundedSender<Action>,
source_buffers: Vec<String>,
should_quit: bool, should_quit: bool,
} }
impl App { impl App {
pub fn new(action_rx: UnboundedReceiver<Action>, action_tx: UnboundedSender<Action>) -> Self { pub fn new(
action_rx: UnboundedReceiver<Action>,
action_tx: UnboundedSender<Action>,
source_buffers: Vec<String>,
) -> Self {
App { App {
action_rx, action_rx,
action_tx, action_tx,
source_buffers,
should_quit: false, should_quit: false,
} }
} }
@ -66,7 +73,13 @@ impl App {
} }
pub fn ui(&mut self, frame: &mut Frame) { 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<Option<Action>> { fn handle_event(&self, event: Event) -> Result<Option<Action>> {

View File

@ -4,7 +4,11 @@ use tokio::sync::mpsc;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let (action_tx, action_rx) = mpsc::unbounded_channel(); 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; let run_result = app.run().await;
if let Err(report) = run_result { if let Err(report) = run_result {