Crate workflow_terminal
source ·Expand description
[workflow-terminal
] is a terminal shell that functions uniformly in native
Rust application command-line environment and in WASM-based browser environment.
This is achieved by combining termion
and
xterm.js into a unified module and offering an intermediary
API that can interface with both libraries.
You can initialize this crate from a regular bin project or a WASM project using
dedicated functions and provide a Cli
trait implementing the command-line
interface that will receive input from the underlying terminal.
Workflow Terminal example can be found at https://github.com/workflow-rs/workflow-terminal-examples
Loading in both native and WASM-browser application environment:
use async_trait::async_trait;
use std::sync::Arc;
use workflow_terminal::Cli;
use workflow_terminal::Terminal;
use workflow_terminal::result::Result;
struct ExampleCli;
#[async_trait]
impl Cli for ExampleCli {
async fn digest(&self, _term: Arc<Terminal>, _cmd: String) -> Result<()>{
Ok(())
}
async fn complete(&self, _term: Arc<Terminal>, _cmd: String) -> Result<Vec<String>>{
Ok(vec![])
}
}
#[tokio::test]
let cli = Arc::new(ExampleCli{});
let term = Arc::new(Terminal::try_new(cli.clone(),"$ ")?);
term.init().await?;
term.writeln("Terminal example (type 'help' for list of commands)");
term.run().await?;
Loading terminal in specific element
use async_trait::async_trait;
use std::sync::Arc;
use workflow_terminal::Cli;
use workflow_terminal::Terminal;
use workflow_terminal::result::Result;
use workflow_terminal::{Options, TargetElement};
#[derive(Clone)]
struct ExampleCli;
#[async_trait]
impl Cli for ExampleCli {
async fn digest(&self, _term: Arc<Terminal>, _cmd: String) -> Result<()>{
Ok(())
}
async fn complete(&self, _term: Arc<Terminal>, _cmd: String) -> Result<Vec<String>>{
Ok(vec![])
}
}
let cli = Arc::new(ExampleCli{});
let options = Options::new()
.with_prompt("$ ")
.with_element(TargetElement::Id("terminal_container".to_string()));
//.with_element(TargetElement::Element(element));
//.with_element(TargetElement::Body);
//.with_element(TargetElement::TagName("body".to_string()));
let term = Arc::new(Terminal::try_new_with_options(cli.clone(), options).unwrap());
Re-exports
pub use cli::Cli;
pub use result::Result;
pub use terminal::parse;
pub use terminal::Options;
pub use terminal::TargetElement;
pub use terminal::Terminal;
Modules
- Clear line helper (ANSI escape codes to clear the terminal line)
- Cli trait for implementing a user-side command-line processor.
- Cursor helper structs (from https://crates.io/crates/termion)
- Errors produced by
workflow_terminal
- Terminal key definitions
- Module implementing the terminal interface abstraction