os-terminal

Crates.ioos-terminal
lib.rsos-terminal
version
sourcesrc
created_at2024-07-26 04:54:37.83034
updated_at2024-11-08 14:43:09.982389
descriptionBeautiful terminal emulator on no-std environment
homepagehttps://github.com/wenxuanjun/os-terminal
repositoryhttps://github.com/wenxuanjun/os-terminal
max_upload_size
id1315786
Cargo.toml error:TOML parse error at line 22, column 1 | 22 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Xuanjun Wen (wenxuanjun)

documentation

README

OS Terminal

A no_std terminal library for embedded systems and OS kernels.

The environment should have initialized global_allocator since alloc crate is used for dynamic memory allocation.

Screenshot

This screenshot shows the result of running fastfetch in the example terminal. You can try it by running cargo run --release --example terminal --features=truetype (Linux only). It will execute bash by default.

Features

  • Embedded smooth noto sans mono font rendering
  • Truetype font support
  • VT100 and part of xterm escape sequence support
  • Beautiful color scheme
  • Cursor display and shape control

Usage

Create a display wrapper to wrap your framebuffer and implement the DrawTarget trait for it.

use alloc::boxed::Box;
use os_terminal::{DrawTarget, Rgb888, Terminal};
use os_terminal::font::BitmapFont;

struct Display {
    width: usize,
    height: usize,
    buffer: &'static [u32],
}

impl DrawTarget for Display {
    fn size(&self) -> (usize, usize) {
        (self.width, self.height)
    }

    #[inline(always)]
    fn draw_pixel(&mut self, x: usize, y: usize, color: Rgb888) {
        let value = (color.0 as u32) << 16 | (color.1 as u32) << 8 | color.2 as u32;
        self.buffer[y * self.width + x] = value;
    }
}

Then you can create a terminal with a box-wrapped font manager.

let mut terminal = Terminal::new(display);
terminal.set_font_manager(Box::new(BitmapFont));

Now you can redirect the keyboard events to the terminal in scancode format (currently only Scan Code Set1 and North American standard English keyboard layout are supported) to let the terminal process shortcuts or get escaped strings so you can pass it to your shell.

// LCtrl pressed, C pressed, C released, LCtrl released
let scancodes = [0x1d, 0x2e, 0xae, 0x9d];

for scancode in scancodes.iter() {
    if let Some(ansi_string) = terminal.handle_keyboard(*scancode) {
        // Pass the ansi_string to your shell (Some("") Some("\u{3}") None None)
    }
}

And then you can advance the terminal state with the escaped string from the output of your shell.

terminal.advance_state(b"\x1b[31mHello, world!\x1b[0m");
terminal.write_fmt(format_args!("{} + {} = {}", 1, 2, 3));

To use truetype font, enable truetype feature and create a TrueTypeFont instance from a font file with size.

let font_buffer = include_bytes!("SourceCodeVF.otf");
terminal.set_font_manager(Box::new(TrueTypeFont::new(10.0, font_buffer)));

Notice that you are supposed to use a variable-font-supported ttf file otherwise font weight will not change.

Italic font support is also optional. If not provided, it will be rendered with default Roman font.

let font_buffer = include_bytes!("SourceCodeVF.otf");
let italic_buffer = include_bytes!("SourceCodeVF-Italic.otf");
let font_manager = TrueTypeFont::new(10.0, font_buffer).with_italic_font(italic_buffer);
terminal.set_font_manager(Box::new(font_manager));

If you want to get the logs from the terminal, you can set a logger that receives fmt::Arguments.

os_terminal::set_logger(|args| println!("Terminal: {:?}", args));

Default flush strategy is synchronous. If you need higher performance, you can disable the auto flush and flush manually when needed.

terminal.set_auto_flush(false);
terminal.flush();

Features

  • bitmap: Enable embedded noto sans mono bitmap font support. This feature is enabled by default.
  • truetype: Enable truetype font support. This feature is disabled by default.

Acknowledgement

Thanks to the original author and contributors for their great work.

Commit count: 22

cargo fmt