Crate async_editor

Crate async_editor 

Source
Expand description

AsyncEditor Async Console Editor that supports asyncronous output to a resizeable print window. Perfect for an AI Chat Interface.

Keyboard Commands:

  • Arrows, PgUp, PgDn => Move todo - Ctrl-W: Erase the input from the cursor to the previous whitespace todo - Ctrl-U: Erase the input before the cursor
  • Ctrl-L: Clear the screen
  • Ctrl-Left / Ctrl-Right: Move to previous/next word
  • Home: Jump to the start of the line
  • End: Jump to the end of the line
  • Ctrl-Up, Ctrl-Down: Contract / Expand Print vs Edit windows
  • Ctrl-C: Ignored Ctrl Left/Right => Move Left/Right by Word
  • Ctrl PgUp / PgDn - Print History Scrollback, ESC to exit. in dev - Ctrl-k => Delete current line Ctrl-C, Ctrl-D, Ctrl-Q, Ctrl-X => Exit/Quit

Note: this works, but doctest will fail, so doc test have been disabled in Cargo.toml

use async_editor::{EditorEvent, AsyncEditor, Result};
use std::io::Write;
use std::time::Duration;
use tokio::time::sleep;

#[tokio::main]
async fn main() -> Result<()> {
  let mut st = r###"LInitial String"###;
  let st = st.to_string();
  let (mut async_editor, mut async_editor_stdout) = AsyncEditor::new(&st, "Ctrl-C/D/Q/X to Quit".to_string(), 0.5f32, 4)?;
  let mut count: u32 = 0;

  loop {
	tokio::select! {
	_ = sleep(Duration::from_millis(200)) => {
		count += 1;
		//write!(async_editor_stdout, "{}", format!("_Message {}\nreceived\n!", count))?;
		write!(async_editor_stdout, "{}", format!("_Message {} received!", count))?;
	}
	cmd = async_editor.async_editor() => match cmd {
		Ok(EditorEvent::CtrlC | EditorEvent::CtrlD | EditorEvent::CtrlQ | EditorEvent::CtrlX) => {
			break;
		}
		Ok(EditorEvent::CtrlS) => {
			writeln!(async_editor_stdout, "\n\nCtrlS\n")?;
		}
		Ok(_) => {continue;}
			Err(e) => {
				writeln!(async_editor_stdout, "\n\nError: {e:?}\n")?;
				break;
			}
		}
	}
	async_editor.flush()?;
  }
  async_editor.flush()?;

  let text = async_editor.text();
  drop(async_editor);

  println!("\n\nEdited Text:\n{}",text);
  Ok(())
}

Structs§

AsyncEditor
AsyncEditor - Multiline Terminal Editor with simultaneous stdout
Editor
SharedStdout

Enums§

EditorEvent
Error
WriteHistoryType

Type Aliases§

Result