• Stars
    star
    1,994
  • Rank 23,236 (Top 0.5 %)
  • Language
    Rust
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Mirror of https://gitlab.redox-os.org/redox-os/termion

Termion logo

Build Status Latest Version Documentation Examples Changelog Tutorial

Termion is a pure Rust, bindless library for low-level handling, manipulating and reading information about terminals. This provides a full-featured alternative to Termbox.

Termion aims to be simple and yet expressive. It is bindless, meaning that it is not a front-end to some other library (e.g., ncurses or termbox), but a standalone library directly talking to the TTY.

Termion is quite convenient, due to its complete coverage of essential TTY features, providing one consistent API. Termion is rather low-level containing only abstraction aligned with what actually happens behind the scenes. For something more high-level, refer to inquirer-rs, which uses Termion as backend.

Termion generates escapes and API calls for the user. This makes it a whole lot cleaner to use escapes.

Supports Redox, Mac OS X, BSD, and Linux (or, in general, ANSI terminals).

A note on stability

This crate is stable.

Cargo.toml

[dependencies]
termion = "*"

1.0.0 to 2.0.0 guide

1.0.0 2.0.0
AlternativeScreen::from(x) x.into_alternative_screen()

0.1.0 to 1.0.0 guide

This sample table gives an idea of how to go about converting to the new major version of Termion.

0.1.0 1.0.0
use termion::IntoRawMode use termion::raw::IntoRawMode
use termion::TermRead use termion::input::TermRead
stdout.color(color::Red); write!(stdout, "{}", color::Fg(color::Red));
stdout.color_bg(color::Red); write!(stdout, "{}", color::Bg(color::Red));
stdout.goto(x, y); write!(stdout, "{}", cursor::Goto(x, y));
color::rgb(r, g, b); color::Rgb(r, g, b) (truecolor)
x.with_mouse() MouseTerminal::from(x)

Features

  • Raw mode.
  • TrueColor.
  • 256-color mode.
  • Cursor movement.
  • Text formatting.
  • Console size.
  • TTY-only stream.
  • Control sequences.
  • Termios control.
  • Password input.
  • Redox support.
  • Safe isatty wrapper.
  • Panic-free error handling.
  • Special keys events (modifiers, special keys, etc.).
  • Allocation-free.
  • Asynchronous key events.
  • Mouse input.
  • Carefully tested.
  • Detailed documentation on every item.

and much more.

Examples

Style and colors.

extern crate termion;

use termion::{color, style};

use std::io;

fn main() {
    println!("{}Red", color::Fg(color::Red));
    println!("{}Blue", color::Fg(color::Blue));
    println!("{}Blue'n'Bold{}", style::Bold, style::Reset);
    println!("{}Just plain italic", style::Italic);
}

Moving the cursor

extern crate termion;

fn main() {
    print!("{}{}Stuff", termion::clear::All, termion::cursor::Goto(1, 1));
}

Mouse

extern crate termion;

use termion::event::{Key, Event, MouseEvent};
use termion::input::{TermRead, MouseTerminal};
use termion::raw::IntoRawMode;
use std::io::{Write, stdout, stdin};

fn main() {
    let stdin = stdin();
    let mut stdout = MouseTerminal::from(stdout().into_raw_mode().unwrap());

    write!(stdout, "{}{}q to exit. Click, click, click!", termion::clear::All, termion::cursor::Goto(1, 1)).unwrap();
    stdout.flush().unwrap();

    for c in stdin.events() {
        let evt = c.unwrap();
        match evt {
            Event::Key(Key::Char('q')) => break,
            Event::Mouse(me) => {
                match me {
                    MouseEvent::Press(_, x, y) => {
                        write!(stdout, "{}x", termion::cursor::Goto(x, y)).unwrap();
                    },
                    _ => (),
                }
            }
            _ => {}
        }
        stdout.flush().unwrap();
    }
}

Read a password

extern crate termion;

use termion::input::TermRead;
use std::io::{Write, stdout, stdin};

fn main() {
    let stdout = stdout();
    let mut stdout = stdout.lock();
    let stdin = stdin();
    let mut stdin = stdin.lock();

    stdout.write_all(b"password: ").unwrap();
    stdout.flush().unwrap();

    let pass = stdin.read_passwd(&mut stdout);

    if let Ok(Some(pass)) = pass {
        stdout.write_all(pass.as_bytes()).unwrap();
        stdout.write_all(b"\n").unwrap();
    } else {
        stdout.write_all(b"Error\n").unwrap();
    }
}

Usage

See examples/, and the documentation, which can be rendered using cargo doc.

For a more complete example, see a minesweeper implementation, that I made for Redox using termion.

License

MIT/X11.

More Repositories

1

redox

Mirror of https://gitlab.redox-os.org/redox-os/redox
Shell
14,647
star
2

orbtk

The Rust UI-Toolkit.
Rust
3,773
star
3

tfs

Mirror of https://gitlab.redox-os.org/redox-os/tfs
Rust
2,938
star
4

ion

Mirror of https://gitlab.redox-os.org/redox-os/ion
Rust
1,401
star
5

relibc

Mirror of https://gitlab.redox-os.org/redox-os/relibc
Rust
780
star
6

rusttype

Mirror of https://gitlab.redox-os.org/redox-os/rusttype
Rust
613
star
7

kernel

Mirror of https://gitlab.redox-os.org/redox-os/kernel
Rust
517
star
8

coreutils

Mirror of https://gitlab.redox-os.org/redox-os/coreutils
Rust
259
star
9

sodium

Mirror of https://gitlab.redox-os.org/redox-os/sodium
Rust
181
star
10

games

Mirror of https://gitlab.redox-os.org/redox-os/games
Rust
119
star
11

book

Mirror of https://gitlab.redox-os.org/redox-os/book
98
star
12

redoxfs

Mirror of https://gitlab.redox-os.org/redox-os/redoxfs
Rust
85
star
13

orbital

Mirror of https://gitlab.redox-os.org/redox-os/orbital
Rust
69
star
14

drivers

Mirror of https://gitlab.redox-os.org/redox-os/drivers
Rust
44
star
15

bootloader

Mirror of https://gitlab.redox-os.org/redox-os/bootloader
Rust
40
star
16

orbutils

Mirror of https://gitlab.redox-os.org/redox-os/orbutils
Rust
39
star
17

orbclient

Mirror of https://gitlab.redox-os.org/redox-os/orbclient
Rust
33
star
18

cookbook

Mirror of https://gitlab.redox-os.org/redox-os/cookbook
Shell
32
star
19

pkgutils

Mirror of https://gitlab.redox-os.org/redox-os/pkgutils
Rust
30
star
20

orbtk-template

A template for starting an OrbTk project
Rust
30
star
21

redox-ssh

Mirror of https://gitlab.redox-os.org/redox-os/redox-ssh
Rust
30
star
22

syscall

Mirror of https://gitlab.redox-os.org/redox-os/syscall
Rust
29
star
23

netstack

Mirror of https://gitlab.redox-os.org/redox-os/netstack
Rust
29
star
24

netutils

Mirror of https://gitlab.redox-os.org/redox-os/netutils
Rust
28
star
25

extrautils

Mirror of https://gitlab.redox-os.org/redox-os/extrautils
Rust
28
star
26

website

Mirror of https://gitlab.redox-os.org/redox-os/website
CSS
24
star
27

installer

Mirror of https://gitlab.redox-os.org/redox-os/installer
Rust
21
star
28

calc

Mirror of https://gitlab.redox-os.org/redox-os/calc
Rust
21
star
29

orbgame

Mirror of https://gitlab.redox-os.org/redox-os/orbgame
Rust
19
star
30

binutils

Mirror of https://gitlab.redox-os.org/redox-os/binutils
Rust
18
star
31

stb_truetype-rs

Mirror of https://gitlab.redox-os.org/redox-os/stb_truetype-rs
Rust
17
star
32

orbterm

Mirror of https://gitlab.redox-os.org/redox-os/orbterm
Rust
17
star
33

ransid

Mirror of https://gitlab.redox-os.org/redox-os/ransid
Rust
17
star
34

orbtk-book

(WIP) The OrbTk book
Rust
16
star
35

bootloader-efi

Mirror of https://gitlab.redox-os.org/redox-os/bootloader-efi
Rust
16
star
36

userutils

Mirror of https://gitlab.redox-os.org/redox-os/userutils
Rust
16
star
37

libc

Mirror of https://gitlab.redox-os.org/redox-os/libc
C
15
star
38

binutils-gdb

Mirror of https://gitlab.redox-os.org/redox-os/binutils-gdb
C
13
star
39

thrussh

Mirror of https://gitlab.redox-os.org/redox-os/thrussh
Rust
12
star
40

rfcs

Mirror of https://gitlab.redox-os.org/redox-os/rfcs
12
star
41

uefi

Mirror of https://gitlab.redox-os.org/redox-os/uefi
Rust
11
star
42

pkgar

Mirror of https://gitlab.redox-os.org/redox-os/pkgar
Rust
11
star
43

users

Mirror of https://gitlab.redox-os.org/redox-os/users
Rust
9
star
44

redoxer

Mirror of https://gitlab.redox-os.org/redox-os/redoxer
Rust
9
star
45

ipcd

Mirror of https://gitlab.redox-os.org/redox-os/ipcd
Rust
8
star
46

init

Mirror of https://gitlab.redox-os.org/redox-os/init
Rust
8
star
47

libpager

Mirror of https://gitlab.redox-os.org/redox-os/libpager
Rust
8
star
48

design

Mirror of https://gitlab.redox-os.org/redox-os/design
8
star
49

handbook

Mirror of https://gitlab.redox-os.org/redox-os/handbook
7
star
50

libextra

Mirror of https://gitlab.redox-os.org/redox-os/libextra
Rust
6
star
51

orbfont

Mirror of https://gitlab.redox-os.org/redox-os/orbfont
Rust
6
star
52

playbot

Mirror of https://gitlab.redox-os.org/redox-os/playbot
Rust
6
star
53

acid

Mirror of https://gitlab.redox-os.org/redox-os/acid
Rust
5
star
54

arg-parser

Mirror of https://gitlab.redox-os.org/redox-os/arg-parser
Rust
5
star
55

assets

Mirror of https://gitlab.redox-os.org/redox-os/assets
Shell
4
star
56

rine

Mirror of https://gitlab.redox-os.org/redox-os/rine
Rust
4
star
57

uefi_alloc

Mirror of https://gitlab.redox-os.org/redox-os/uefi_alloc
Rust
4
star
58

mesa

Mirror of https://gitlab.redox-os.org/redox-os/mesa
C
4
star
59

termios

Mirror of https://gitlab.redox-os.org/redox-os/termios
Rust
4
star
60

dash

Mirror of https://gitlab.redox-os.org/redox-os/dash
C
3
star
61

ptyd

Mirror of https://gitlab.redox-os.org/redox-os/ptyd
Rust
3
star
62

orbimage

Mirror of https://gitlab.redox-os.org/redox-os/orbimage
Rust
3
star
63

isolinux

Mirror of https://gitlab.redox-os.org/redox-os/isolinux
3
star
64

bots

Mirror of https://gitlab.redox-os.org/redox-os/bots
Rust
3
star
65

smallstring

Mirror of https://gitlab.redox-os.org/redox-os/smallstring
Rust
3
star
66

gawk

Mirror of https://gitlab.redox-os.org/redox-os/gawk
C
2
star
67

contain

Mirror of https://gitlab.redox-os.org/redox-os/contain
Rust
2
star
68

event

Mirror of https://gitlab.redox-os.org/redox-os/event
Rust
2
star
69

docgen

Mirror of https://gitlab.redox-os.org/redox-os/docgen
Rust
2
star
70

orbdata

Mirror of https://gitlab.redox-os.org/redox-os/orbdata
2
star
71

dmi

Mirror of https://gitlab.redox-os.org/redox-os/dmi
Rust
2
star
72

randd

Mirror of https://gitlab.redox-os.org/redox-os/randd
Rust
2
star
73

periodictable

Mirror of https://gitlab.redox-os.org/redox-os/periodictable
Rust
2
star
74

libc-artifacts

Mirror of https://gitlab.redox-os.org/redox-os/libc-artifacts
C
2
star
75

backgrounds

Mirror of https://gitlab.redox-os.org/redox-os/backgrounds
2
star
76

orbaudio

Mirror of https://gitlab.redox-os.org/redox-os/orbaudio
Rust
1
star
77

netdb

Mirror of https://gitlab.redox-os.org/redox-os/netdb
1
star
78

netsurf

Mirror of https://gitlab.redox-os.org/redox-os/netsurf
C
1
star
79

conc

Mirror of https://gitlab.redox-os.org/redox-os/conc
Rust
1
star
80

small

Mirror of https://gitlab.redox-os.org/redox-os/small
Rust
1
star
81

logd

Mirror of https://gitlab.redox-os.org/redox-os/logd
Rust
1
star
82

keyboard-sfx

Mirror of https://gitlab.redox-os.org/redox-os/keyboard-sfx
1
star
83

shellstorm

Mirror of https://gitlab.redox-os.org/redox-os/shellstorm
Rust
1
star