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) (through TermCursor
),
put, get characters or write strings.
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 TextBuffer
s 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
.
use glerminal::{TerminalBuilder, TextBuffer, TextStyle};
let terminal = TerminalBuilder::new()
.with_title("Hello GLerminal!")
.with_dimensions((1280, 720))
.build();
let mut text_buffer;
match TextBuffer::create(&terminal, (80, 24)) {
Ok(buffer) => text_buffer = buffer,
Err(error) => panic!(format!("Failed to initialize text buffer: {}", error)),
}
text_buffer.cursor.style = TextStyle {
fg_color: [1.0, 0.0, 0.0, 1.0],
bg_color: [1.0; 4],
shakiness: 0.5,
};
text_buffer.cursor.move_to(0, 0);
text_buffer.write("This text is shaking in red in a white background!");
terminal.flush(&mut text_buffer);
use glerminal::{TerminalBuilder, TextBuffer, Parser};
let terminal = TerminalBuilder::new()
.with_title("Hello GLerminal!")
.with_dimensions((1280, 720))
.build();
let mut text_buffer;
match TextBuffer::create(&terminal, (80, 24)) {
Ok(buffer) => text_buffer = buffer,
Err(error) => panic!(format!("Failed to initialize text buffer: {}", error)),
}
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]");
terminal.flush(&mut text_buffer);
use glerminal::{TerminalBuilder, TextBuffer};
let terminal = TerminalBuilder::new().build();
let mut background_text_buffer;
match TextBuffer::create(&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::create(&terminal, (80, 24)) {
Ok(buffer) => foreground_text_buffer = buffer,
Err(error) => panic!(format!("Failed to initialize text buffer: {}", error)),
}
background_text_buffer.write("I am in the background");
foreground_text_buffer.write("I am in the foreground");
terminal.flush(&mut background_text_buffer);
terminal.flush(&mut foreground_text_buffer);
terminal.draw(&background_text_buffer);
terminal.draw(&foreground_text_buffer);
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 (See text_buffer.cursor)
Puts the given text the same way as put_char
Returns the current position of the cursor
Returns whether the TextBuffer is dirty or not (whether flush will have any effect or not)