Skip to content

Components to ANSI Strings #548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Feather

[![build](https://github.com/feather-rs/feather/workflows/build/badge.svg)](https://github.com/feather-rs/feather/actions)
[![Discord](https://img.shields.io/discord/619316022800809995?logo=discord)](https://discordapp.com/invite/4eYmK69)

A Minecraft server implementation written in Rust.

### Status
Expand Down
2 changes: 1 addition & 1 deletion feather/protocol/src/packets/server/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ pub enum GameStateChange {
WinGame {
show_credits: bool,
},
/// See https://help.minecraft.net/hc/en-us/articles/4408948974989-Minecraft-Java-Edition-Demo-Mode-
/// See <https://help.minecraft.net/hc/en-us/articles/4408948974989-Minecraft-Java-Edition-Demo-Mode->
DemoEvent(DemoEventType),
/// Sent when any player is struck by an arrow.
ArrowHitAnyPlayer,
Expand Down
2 changes: 2 additions & 0 deletions feather/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ colored = "2"
common = { path = "../common", package = "feather-common" }
crossbeam-utils = "0.8"
ecs = { path = "../ecs", package = "feather-ecs" }
enable-ansi-support = "0.1.2"
fern = "0.6"
flate2 = "1"
flume = "0.10"
Expand Down Expand Up @@ -55,6 +56,7 @@ uuid = "0.8"
slab = "0.4"
libcraft-core = { path = "../../libcraft/core" }
libcraft-items = { path = "../../libcraft/items" }
libcraft-text = { path = "../../libcraft/text" }
worldgen = { path = "../worldgen", package = "feather-worldgen" }

[features]
Expand Down
11 changes: 11 additions & 0 deletions feather/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const CONFIG_PATH: &str = "config.toml";

#[tokio::main]
async fn main() -> anyhow::Result<()> {
enable_ansi();

let feather_server::config::ConfigContainer {
config,
was_config_created,
Expand All @@ -36,6 +38,15 @@ async fn main() -> anyhow::Result<()> {
Ok(())
}

fn enable_ansi() {
match enable_ansi_support::enable_ansi_support() {
Ok(()) => {}
Err(_) => {
log::warn!("Failed to enable ANSI support, Output will not work properly");
}
}
}

fn init_game(server: Server, config: &Config) -> anyhow::Result<Game> {
let mut game = Game::new();
init_systems(&mut game, server);
Expand Down
15 changes: 13 additions & 2 deletions feather/server/src/systems/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ fn flush_chat_boxes(game: &mut Game, server: &mut Server) -> SysResult {
fn flush_console_chat_box(game: &mut Game) -> SysResult {
for (_, (_console, mailbox)) in game.ecs.query::<(&Console, &mut ChatBox)>().iter() {
for message in mailbox.drain() {
// TODO: properly display chat message
log::info!("{:?}", message.text());
log::info!("{}", message.text().as_ansi());
}
}

Expand All @@ -56,3 +55,15 @@ fn flush_title_chat_boxes(game: &mut Game, server: &mut Server) -> SysResult {

Ok(())
}

#[cfg(test)]
mod tests {
use libcraft_text::{TextComponent, TextComponentBuilder};

#[test]
fn test_ansi_text_serialization() {
let text = TextComponent::from("Hello, world!").red().bold();
let ansi_text = text.as_ansi();
assert_eq!("\x1b[1;31mHello, world!\x1b[0m", ansi_text);
}
}
4 changes: 3 additions & 1 deletion feather/server/src/systems/player_join.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use libcraft_items::InventorySlot;
use libcraft_text::{IntoTextComponent, TextComponentBuilder};
use log::debug;

use base::anvil::player::PlayerAbilities;
Expand Down Expand Up @@ -143,7 +144,8 @@ fn accept_new_player(game: &mut Game, server: &mut Server, client_id: ClientId)

fn broadcast_player_join(game: &mut Game, username: &str) {
let message = Text::translate_with("multiplayer.player.joined", vec![username.to_owned()]);
game.broadcast_chat(ChatKind::System, message);
let component = message.into_component().yellow();
game.broadcast_chat(ChatKind::System, component);
}

fn player_abilities_or_default(
Expand Down
10 changes: 9 additions & 1 deletion libcraft/items/src/item_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,15 @@ pub enum ItemStackError {

impl Display for ItemStackError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
let name = match self {
ItemStackError::ClientOverflow => "Client overflow",
ItemStackError::EmptyStack => "Empty stack",
ItemStackError::ExceedsStackSize => "Exceeds stack size",
ItemStackError::IncompatibleStacks => "Incompatible stacks",
ItemStackError::NotEnoughItems => "Not enough items",
};

write!(f, "{}", name)
}
}

Expand Down
1 change: 1 addition & 0 deletions libcraft/text/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ serde_json = "1"
serde_with = "1"
uuid = { version = "0.8", features = [ "serde" ] }
thiserror = "1"
string-builder = "0.2.0"
97 changes: 97 additions & 0 deletions libcraft/text/src/ansi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
pub struct AnsiStyle {
format: u8,
}

impl AnsiStyle {
pub fn regular() -> AnsiStyle {
AnsiStyle { format: 0 }
}

pub fn bold() -> AnsiStyle {
AnsiStyle { format: 1 }
}

pub fn underline() -> AnsiStyle {
AnsiStyle { format: 4 }
}

fn format(&self, color: u8) -> String {
format!("\x1B[{};{}m", self.format, color)
}

pub fn black(&self) -> String {
self.format(30)
}

pub fn red(&self) -> String {
self.format(31)
}

pub fn green(&self) -> String {
self.format(32)
}

pub fn yellow(&self) -> String {
self.format(33)
}

pub fn blue(&self) -> String {
self.format(34)
}

pub fn magenta(&self) -> String {
self.format(35)
}

pub fn cyan(&self) -> String {
self.format(36)
}

pub fn white(&self) -> String {
self.format(37)
}

pub fn reset() -> &'static str {
"\x1b[0m"
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_ansi_style() {
assert_eq!(AnsiStyle::reset(), "\x1b[0m");

let style = AnsiStyle::regular();
assert_eq!(style.black(), "\x1b[0;30m");
assert_eq!(style.red(), "\x1b[0;31m");
assert_eq!(style.green(), "\x1b[0;32m");
assert_eq!(style.yellow(), "\x1b[0;33m");
assert_eq!(style.blue(), "\x1b[0;34m");
assert_eq!(style.magenta(), "\x1b[0;35m");
assert_eq!(style.cyan(), "\x1b[0;36m");
assert_eq!(style.white(), "\x1b[0;37m");

let style = AnsiStyle::bold();
assert_eq!(style.black(), "\x1b[1;30m");
assert_eq!(style.red(), "\x1b[1;31m");
assert_eq!(style.green(), "\x1b[1;32m");
assert_eq!(style.yellow(), "\x1b[1;33m");
assert_eq!(style.blue(), "\x1b[1;34m");
assert_eq!(style.magenta(), "\x1b[1;35m");
assert_eq!(style.cyan(), "\x1b[1;36m");
assert_eq!(style.white(), "\x1b[1;37m");

let style = AnsiStyle::underline();
assert_eq!(style.black(), "\x1b[4;30m");
assert_eq!(style.red(), "\x1b[4;31m");
assert_eq!(style.green(), "\x1b[4;32m");
assert_eq!(style.yellow(), "\x1b[4;33m");
assert_eq!(style.blue(), "\x1b[4;34m");
assert_eq!(style.magenta(), "\x1b[4;35m");
assert_eq!(style.cyan(), "\x1b[4;36m");
assert_eq!(style.white(), "\x1b[4;37m");
}
}
1 change: 1 addition & 0 deletions libcraft/text/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod ansi;
pub mod text;
pub mod title;

Expand Down
Loading