1
0
Fork 0
roguelike-game/src/gamelog/builder.rs

105 lines
2.6 KiB
Rust

use ::bracket_lib::prelude::*;
use super::{append_entry, LogFragment};
use crate::colors;
/// The game log builder - builds one line at a time
pub struct Logger {
current_color: RGB,
fragments: Vec<LogFragment>,
}
/// Convenience method to create a new [`Logger`] and
/// immediately append text
pub fn line<T: ToString>(text: T) -> Logger {
Logger::new().append(text)
}
/// Convenience method to create a new [`Logger`] and
/// immediately append text of the given color
pub fn color_line<T: ToString>(color: RGB, text: T) -> Logger {
Logger::new().append_color(color, text)
}
/// Convenience function to log a simple line of white text
pub fn log_line<T: ToString>(text: T) {
line(text).log();
}
/// Convenience function to log a line of one color
pub fn log_color_line<T: ToString>(color: RGB, text: T) {
color_line(color, text).log();
}
impl Logger {
pub fn new() -> Self {
Logger {
current_color: colors::WHITE,
fragments: Vec::new(),
}
}
/// Sets the color of text to be added to the current line
#[allow(dead_code)]
pub fn color(mut self, color: RGB) -> Self {
self.current_color = color;
self
}
/// Appends text to the current line of the game log.
/// Generic on the [`ToString`] trait so that [`&str`], [`String`],
/// and `&String` types, all work without more type juggling
/// at the call site.
pub fn append<T: ToString>(mut self, text: T) -> Self {
self.fragments.push(LogFragment {
color: self.current_color,
text: text.to_string(),
});
self
}
pub fn npc_name<T: ToString>(mut self, text: T) -> Self {
self.fragments.push(LogFragment {
color: colors::YELLOW,
text: text.to_string(),
});
self
}
pub fn item_name<T: ToString>(mut self, text: T) -> Self {
self.fragments.push(LogFragment {
color: colors::CYAN,
text: text.to_string(),
});
self
}
pub fn damage(mut self, damage: i32) -> Self {
self.fragments.push(LogFragment {
color: colors::RED,
text: format!("{}", damage),
});
self
}
/// Convenience method to append text with a new color
pub fn append_color<T: ToString>(mut self, color: RGB, text: T) -> Self {
self.fragments.push(LogFragment {
color,
text: text.to_string(),
});
self
}
/// Append the current line to the log output
pub fn log(self) {
append_entry(self.fragments);
}
}