Rendition modes

This commit is contained in:
Ticki 2016-03-07 17:57:17 +01:00
parent 6f9addc42b
commit 098ce66b84
4 changed files with 34 additions and 4 deletions

View File

@ -1,6 +1,6 @@
extern crate libterm; extern crate libterm;
use libterm::{TermControl, raw_mode, Color}; use libterm::{TermControl, raw_mode, Color, Mode};
use std::io::{Read, Write, stdout, stdin}; use std::io::{Read, Write, stdout, stdin};
fn main() { fn main() {
@ -10,7 +10,9 @@ fn main() {
stdout.goto(5, 5).unwrap(); stdout.goto(5, 5).unwrap();
stdout.clear().unwrap(); stdout.clear().unwrap();
stdout.mode(Mode::Bold).unwrap();
stdout.write(b"yo, 'q' will exit.").unwrap(); stdout.write(b"yo, 'q' will exit.").unwrap();
stdout.reset().unwrap();
stdout.flush().unwrap(); stdout.flush().unwrap();
stdout.goto(20, 10).unwrap(); stdout.goto(20, 10).unwrap();

View File

@ -1,5 +1,5 @@
use std::io::{Write, Result as IoResult}; use std::io::{Write, Result as IoResult};
use Color; use {Color, Mode};
/// Controlling terminals. /// Controlling terminals.
pub trait TermControl { pub trait TermControl {
@ -21,8 +21,8 @@ pub trait TermControl {
fn hide(&mut self) -> IoResult<usize> { fn hide(&mut self) -> IoResult<usize> {
self.csi(b"?25l") self.csi(b"?25l")
} }
/// Reset the style of the cursor. /// Reset the rendition mode.
fn reset_style(&mut self) -> IoResult<usize> { fn reset(&mut self) -> IoResult<usize> {
self.csi(b"m") self.csi(b"m")
} }
/// Go to a given position. /// Go to a given position.
@ -71,6 +71,10 @@ pub trait TermControl {
b'm', b'm',
]) ])
} }
/// Set rendition mode (SGR).
fn mode(&mut self, mode: Mode) -> IoResult<usize> {
self.rendition(mode as u8)
}
} }
impl<W: Write> TermControl for W { impl<W: Write> TermControl for W {

View File

@ -16,3 +16,6 @@ pub use size::termsize;
mod color; mod color;
pub use color::Color; pub use color::Color;
mod mode;
pub use mode::Mode;

21
src/mode.rs Normal file
View File

@ -0,0 +1,21 @@
/// A SGR parameter (rendition mode).
pub enum Mode {
/// Reset SGR parameters.
Reset = 0,
/// Bold text.
Bold = 1,
/// Fainted text (not widely supported).
Faint = 2,
/// Italic text.
Italic = 3,
/// Underlined text.
Underline = 4,
/// Blinking text (not widely supported).
Blink = 5,
/// Inverted colors (negative mode).
Invert = 7,
/// Crossed out text (not widely supported).
CrossedOut = 9,
/// Framed text (not widely supported).
Framed = 51,
}