Struct glerminal::TextBuffer

source ·
pub struct TextBuffer { /* private fields */ }
Expand description

The TextBuffer acts as a “state machine” where you can set foreground color, background color and shakiness for the cursor, move the cursor around, clear the screen and write with the cursor (using the cursor’s styles). It’s often the most efficient way to write things, especially if you have a very structured way of displaying things, but for a more simple-to-use way of writing, that isn’t as structured ie. for a dialogue, you might want to use the Parser.

As of version 0.2.0, drawing multiple TextBuffers on top of eachother is also possible. As of this version writing 16-bit characters is also possible.

Parser is a struct added as a default feature, that is able to take in a piece of text and then parse it and change the cursor styles easily using the TextBuffer. The Parser can handle tags imilar to BBCode tags, and can change fg, bg and shake, meaning the following tags are available to use mid-text:

  • [fg=color]
  • [bg=color]
  • [shake=decimal]
  • optional closing/style-resetting tags: [/fg], [/bg] and [/shake]

The colors the Parser uses mid text must be pre-defined however with add_color.

Example usage of TextBuffer:

use glerminal::{TerminalBuilder, TextBuffer};

let terminal = TerminalBuilder::new()
    .with_title("Hello GLerminal!")
    .with_dimensions((1280, 720))
    .build();

let mut text_buffer;
match TextBuffer::new(&terminal, (80, 24)) {
  Ok(buffer) => text_buffer = buffer,
  Err(error) => panic!(format!("Failed to initialize text buffer: {}", error)),
}

// Test TextBuffer
text_buffer.change_cursor_fg_color([1.0, 0.0, 0.0, 1.0]);
text_buffer.change_cursor_bg_color([1.0, 1.0, 1.0, 1.0]);
text_buffer.change_cursor_shakiness(0.5);
text_buffer.move_cursor(0, 0);
text_buffer.write("This text is shaking in red in a white background!");

// Flush to "apply changes"
terminal.flush(&mut text_buffer);

Example usage of Parser

use glerminal::{TerminalBuilder, TextBuffer, Parser};

let terminal = TerminalBuilder::new()
    .with_title("Hello GLerminal!")
    .with_dimensions((1280, 720))
    .build();

let mut text_buffer;
match TextBuffer::new(&terminal, (80, 24)) {
  Ok(buffer) => text_buffer = buffer,
  Err(error) => panic!(format!("Failed to initialize text buffer: {}", error)),
}

// Test Parser
let mut parser = Parser::new();
parser.add_color("red", [1.0, 0.0, 0.0, 1.0]);
parser.add_color("white", [1.0, 1.0, 1.0, 1.0]);
parser.write(&mut text_buffer, "[fg=red][bg=white][shake=1.0]This text is also shaking in red in a white background![/fg][/bg][/shake]");
// Note: it is not necessary to close fg/bg/shake tags, parser will automatically revert colors in the TextBuffer.

// Flush to "apply changes"
terminal.flush(&mut text_buffer);

Simple example drawing multiple TextBuffers

use glerminal::{TerminalBuilder, TextBuffer};

let terminal = TerminalBuilder::new().build();

let mut background_text_buffer;
match TextBuffer::new(&terminal, (80, 24)) {
  Ok(buffer) => background_text_buffer = buffer,
  Err(error) => panic!(format!("Failed to initialize text buffer: {}", error)),
}

let mut foreground_text_buffer;
match TextBuffer::new(&terminal, (80, 24)) {
  Ok(buffer) => foreground_text_buffer = buffer,
  Err(error) => panic!(format!("Failed to initialize text buffer: {}", error)),
}

// Write to the background buffer
background_text_buffer.write("I am in the background");

// Write to the foreground buffer
foreground_text_buffer.write("I am in the foreground");

// Flush to "apply changes"
terminal.flush(&mut background_text_buffer);
terminal.flush(&mut foreground_text_buffer);

// Draw the text buffers
// The two texts will appear on top of eachother because they are written in the same location
terminal.draw(&background_text_buffer);
terminal.draw(&foreground_text_buffer);

Implementations§

Creates a new text buffer with the given dimensions (width in characters, height in characters)

Get the dimensions of the text buffer (in characters). Returns (width, height)

Sets the character at the specified position. It is the user’s responsibility to check if such a position exists.

Gets the TermChaacter in the given position

Returns None if x/y are out of bounds

Clears the screen (makes every character empty and resets their style)

Puts a regular character to the current position of the cursor with the cursor’s style

Puts a raw 16-bit character to the current position of the cursor with the cursor’s style

Puts the given text the same way as put_char

Changes the foreground color for the cursor

Changes the background color of the cursor

Returns the current foreground color of the cursor

Returns the current background color of the cursor

Changes the shakiness of the cursor

Gets the current shakiness of the cursor

Moves the cursor to a specified location in the terminal. If the location does not exist, nothing happens.

Returns the current position of the cursor

Set the limits for drawing, other than the current screen. None means no limit in this direction.

Get the current term limits of the terminal.

Returns whether the TextBuffer is dirty or not (whether flush will have any effect or not)

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.