Module glerminal::text_buffer [] [src]

This module contains two of the most central structs of GLerminal; TextBuffer and Parser

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.

Parser is a struct 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::terminal::TerminalBuilder;
use glerminal::text_buffer::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::terminal::TerminalBuilder;
use glerminal::text_buffer::TextBuffer;
use glerminal::text_buffer::parser::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);

Modules

parser

The module that contains the Parser.

Structs

TermCharacter

Represents a single character in a TextBuffer

TextBuffer

Represents the text buffer of the terminal; contains the "grid of TermCharacters" that will be drawn.

Type Definitions

Color

Represents a color with values from 0.0 to 1.0 (red, green, blue, alpha)