Skip to main content

Crate workflow_terminal

Crate workflow_terminal 

Source
Expand description

github crates.io docs.rs license

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 : Arc<Self>, _term: Arc<Terminal>, _cmd: String) -> Result<()> {
        Ok(())
    }
    async fn complete(self : Arc<Self>, _term: Arc<Terminal>, _cmd: String) -> Result<Option<Vec<String>>> {
        Ok(None)
    }
    fn prompt(&self) -> Option<String> {
        Some("$ ".to_string())
    }
}

#[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 : Arc<Self>, _term: Arc<Terminal>, _cmd: String) -> Result<()> {
        Ok(())
    }
    async fn complete(self : Arc<Self>, _term: Arc<Terminal>, _cmd: String) -> Result<Option<Vec<String>>> {
        Ok(None)
    }
    fn prompt(&self) -> Option<String> {
        Some("$ ".to_string())
    }
}


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 cli::Context;
pub use cli::Handler;
pub use cli::HandlerCli;
pub use crlf::CrLf;
pub use result::Result;
pub use terminal::Event;
pub use terminal::Modifiers;
pub use terminal::Options;
pub use terminal::TargetElement;
pub use terminal::Terminal;
pub use terminal::parse;
pub use terminal::Theme;
pub use terminal::ThemeOption;
pub use unicode::UnicodeString;
pub use terminal::init_panic_hook;
pub use textwrap;
pub use macros::*;

Modules§

clear
Clear line helper (ANSI escape codes to clear the terminal line)
cli
Cli trait for implementing a user-side command-line processor.
crlf
Conversion of \n line endings into terminal-friendly \r\n.
cursor
Cursor helper structs (from https://crates.io/crates/termion)
error
Errors produced by workflow_terminal
keys
Terminal key definitions
macros
Convenience macros for terminal logging and output.
prelude
Re-exports of the most commonly used types and traits.
result
Result encapsulating internal terminal Error and CliResult encapsulating a String error (for output in the terminal)
terminal
Module implementing the terminal interface abstraction
unicode
Helpers for working with strings as sequences of Unicode characters.

Macros§

terrorln
Write a formatted error line (styled in red) to the given terminal. Usage: terrorln!(term, "format {}", value).
tpara
Write formatted text to the given terminal as a word-wrapped paragraph. Usage: tpara!(term, "format {}", value).
tprint
Write formatted text without a trailing newline to the given terminal. Usage: tprint!(term) or tprint!(term, "format {}", value).
tprintln
Write a formatted line followed by a newline to the given terminal. Usage: tprintln!(term) or tprintln!(term, "format {}", value).
twarnln
Write a formatted warning line (styled in yellow) to the given terminal. Usage: twarnln!(term, "format {}", value).

Functions§

disable_raw_mode
Disables raw mode.

Derive Macros§

Handler
Derives a workflow_terminal::cli::Handler implementation for the annotated type, taking the command verb and help text from #[verb(..)] and #[help(..)] attributes and defaulting the verb to the kebab-cased type name.