From a52bfe14decbbbebb48f194324e7481c7b5f662d Mon Sep 17 00:00:00 2001 From: broccolingual Date: Wed, 3 Sep 2025 03:10:57 +0900 Subject: [PATCH 1/2] refactor: update attribute usage to use default variants and improve consistency --- README.md | 10 +++---- examples/colors.rs | 6 ++-- examples/file_reader.rs | 8 +++--- examples/hello_world.rs | 4 +-- examples/inputs.rs | 4 +-- examples/tetris/src/core.rs | 14 ++++----- examples/tetris/src/main.rs | 6 ++-- src/attr.rs | 57 ++++++++++++++++++++++--------------- src/framebuffer.rs | 6 ++-- src/window.rs | 2 +- 10 files changed, 64 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 1d6e219..7719069 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ fn main() -> Result<(), Box> { canvas.set_named_border( "HELLO WORLD", Align::Right, - Attr::NORMAL, + Attr::default(), Color::White, Color::default(), ); // Set a named border for the canvas @@ -45,10 +45,10 @@ fn main() -> Result<(), Box> { canvas.width / 2, // Center the text horizontally canvas.height / 2, "Hello, world! (Press 'q' to quit)", - Attr::NORMAL, // Set text decoration - Color::Green, // Set text color - Color::RGB(128, 192, 128), // Set background color - Align::Center, // Set text alignment to center + Attr::Bold, // Set text decoration + Color::Green, // Set text color + Color::default(), // Set background color + Align::Center, // Set text alignment to center ); })?; diff --git a/examples/colors.rs b/examples/colors.rs index 25b97c9..55c03d3 100644 --- a/examples/colors.rs +++ b/examples/colors.rs @@ -20,7 +20,7 @@ fn main() -> Result<(), Box> { canvas.set_named_border( "COLORS", Align::Right, - Attr::NORMAL, + Attr::default(), Color::White, Color::default(), ); @@ -50,7 +50,7 @@ fn main() -> Result<(), Box> { canvas.width / 2 + 1, canvas.height / 2, "Color Circle", - Attr::BOLD, + Attr::Bold, Color::White, Color::default(), Align::Center, @@ -59,7 +59,7 @@ fn main() -> Result<(), Box> { 3, 2, "Press 'q' to quit", - Attr::BOLD, + Attr::Bold, Color::White, Color::default(), Align::Left, diff --git a/examples/file_reader.rs b/examples/file_reader.rs index f3375a5..13c8570 100644 --- a/examples/file_reader.rs +++ b/examples/file_reader.rs @@ -125,7 +125,7 @@ impl FileReader { canvas.set_named_border( &format!("File Reader - {}", self.file_path), Align::Center, - Attr::NORMAL, + Attr::default(), Color::White, Color::default(), ); @@ -147,9 +147,9 @@ impl FileReader { // 現在行のハイライト let (attrs, fg, bg) = if is_current { - (Attr::NORMAL, Color::Black, Color::White) + (Attr::default(), Color::Black, Color::White) } else { - (Attr::NORMAL, Color::White, Color::default()) + (Attr::default(), Color::White, Color::default()) }; // 行番号を表示 @@ -193,7 +193,7 @@ impl FileReader { canvas.width / 2, canvas.height - 1, &status, - Attr::NORMAL, + Attr::default(), Color::Yellow, Color::default(), Align::Center, diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 852be2a..612e715 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -20,7 +20,7 @@ fn main() -> Result<(), Box> { canvas.set_named_border( "HELLO WORLD", Align::Right, - Attr::NORMAL, + Attr::default(), Color::White, Color::default(), ); // Set a named border for the canvas @@ -28,7 +28,7 @@ fn main() -> Result<(), Box> { canvas.width / 2, // Center the text horizontally canvas.height / 2, "Hello, world! (Press 'q' to quit)", - Attr::NORMAL, // Set text decoration + Attr::Bold, // Set text decoration Color::Green, // Set text color Color::default(), // Set background color Align::Center, // Set text alignment to center diff --git a/examples/inputs.rs b/examples/inputs.rs index 0beccae..14fc579 100644 --- a/examples/inputs.rs +++ b/examples/inputs.rs @@ -24,7 +24,7 @@ fn main() -> Result<(), Box> { canvas.set_named_border( "INPUTS", Align::Right, - Attr::NORMAL, + Attr::default(), Color::White, Color::default(), ); @@ -51,7 +51,7 @@ fn main() -> Result<(), Box> { canvas.width / 2, canvas.height / 2, &full_text, - Attr::NORMAL, + Attr::default(), Color::White, Color::default(), Align::Center, diff --git a/examples/tetris/src/core.rs b/examples/tetris/src/core.rs index 659eda8..f37e332 100644 --- a/examples/tetris/src/core.rs +++ b/examples/tetris/src/core.rs @@ -121,7 +121,7 @@ impl Core { pos.x as usize * 2 + 1, pos.y as usize + 1, " ", - Attr::NORMAL, + Attr::default(), Color::White, block.get_color(), Align::Left, @@ -132,12 +132,12 @@ impl Core { fn update_field_frame(&mut self) { self.field_frame.clear(); self.field_frame - .set_border(Attr::NORMAL, Color::White, Color::default()); + .set_border(Attr::default(), Color::White, Color::default()); self.field_frame.set_str( 0, 0, " ", - Attr::NORMAL, + Attr::default(), Color::White, Color::default(), Align::Left, @@ -153,7 +153,7 @@ impl Core { x * 2 + 1, y + 1, "──", - Attr::NORMAL, + Attr::default(), Color::White, color, Align::Left, @@ -164,7 +164,7 @@ impl Core { x * 2 + 1, y + 1, " ", - Attr::NORMAL, + Attr::default(), Color::White, color, Align::Left, @@ -178,7 +178,7 @@ impl Core { self.next_block_frame.set_named_border( "NEXT", Align::Center, - Attr::NORMAL, + Attr::default(), Color::White, Color::default(), ); @@ -191,7 +191,7 @@ impl Core { self.holding_block_frame.set_named_border( "HOLD", Align::Center, - Attr::NORMAL, + Attr::default(), Color::White, Color::default(), ); diff --git a/examples/tetris/src/main.rs b/examples/tetris/src/main.rs index cc0f7df..8c74863 100644 --- a/examples/tetris/src/main.rs +++ b/examples/tetris/src/main.rs @@ -64,7 +64,7 @@ fn main() -> Result<(), Box> { canvas.set_named_border( "TETRIS", Align::Right, - Attr::NORMAL, + Attr::default(), Color::White, Color::default(), ); @@ -92,12 +92,12 @@ fn main() -> Result<(), Box> { if core.is_gameover { win.draw(|canvas| { - canvas.set_border(Attr::NORMAL, Color::Red, Color::default()); + canvas.set_border(Attr::default(), Color::Red, Color::default()); canvas.set_str( canvas.width / 2, canvas.height / 2, "G A M E O V E R", - Attr::BOLD, + Attr::Bold, Color::Red, Color::default(), Align::Center, diff --git a/src/attr.rs b/src/attr.rs index a7afa53..9f4d915 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -5,23 +5,34 @@ bitflags! { /// Represents terminal attributes using bitflags. #[derive(Debug, Clone, Copy, PartialEq)] pub struct Attr: u16 { - const NORMAL = 1; // 0 - const BOLD = 2; // 1 - const THIN = 4; // 2 - const ITALIC = 8; // 3 - const UNDERLINE = 16; // 4 - const BLINK = 32; // 5 - const FASTBLINK = 64; // 6 - const INVERT = 128; // 7 - const HIDDEN = 256; // 8 - const REMOVE = 512; // 9 - const PRIMARY = 1024; // 10 + const NORMAL = 0b0000_0000_0000_0001; // deprecated + const Normal = 0b0000_0000_0000_0001; + const BOLD = 0b0000_0000_0000_0010; // deprecated + const Bold = 0b0000_0000_0000_0010; + const THIN = 0b0000_0000_0000_0100; // deprecated + const Thin = 0b0000_0000_0000_0100; + const ITALIC = 0b0000_0000_0000_1000; // deprecated + const Italic = 0b0000_0000_0000_1000; + const UNDERLINE = 0b0000_0000_0001_0000; // deprecated + const Underline = 0b0000_0000_0001_0000; + const BLINK = 0b0000_0000_0010_0000; // deprecated + const Blink = 0b0000_0000_0010_0000; + const FASTBLINK = 0b0000_0000_0100_0000; // deprecated + const FastBlink = 0b0000_0000_0100_0000; + const INVERT = 0b0000_0000_1000_0000; // deprecated + const Invert = 0b0000_0000_1000_0000; + const HIDDEN = 0b0000_0001_0000_0000; // deprecated + const Hidden = 0b0000_0001_0000_0000; + const REMOVE = 0b0000_0010_0000_0000; // deprecated + const Remove = 0b0000_0010_0000_0000; + const PRIMARY = 0b0000_0100_0000_0000; // deprecated + const Primary = 0b0000_0100_0000_0000; } } impl Default for Attr { fn default() -> Self { - Attr::NORMAL + Attr::Normal } } @@ -35,17 +46,17 @@ impl Attr { } let attr_mappings = [ - (Attr::NORMAL, "0"), - (Attr::BOLD, "1"), - (Attr::THIN, "2"), - (Attr::ITALIC, "3"), - (Attr::UNDERLINE, "4"), - (Attr::BLINK, "5"), - (Attr::FASTBLINK, "6"), - (Attr::INVERT, "7"), - (Attr::HIDDEN, "8"), - (Attr::REMOVE, "9"), - (Attr::PRIMARY, "10"), + (Attr::NORMAL | Attr::Normal, "0"), + (Attr::BOLD | Attr::Bold, "1"), + (Attr::THIN | Attr::Thin, "2"), + (Attr::ITALIC | Attr::Italic, "3"), + (Attr::UNDERLINE | Attr::Underline, "4"), + (Attr::BLINK | Attr::Blink, "5"), + (Attr::FASTBLINK | Attr::FastBlink, "6"), + (Attr::INVERT | Attr::Invert, "7"), + (Attr::HIDDEN | Attr::Hidden, "8"), + (Attr::REMOVE | Attr::Remove, "9"), + (Attr::PRIMARY | Attr::Primary, "10"), ]; let mut buf = String::with_capacity(24); diff --git a/src/framebuffer.rs b/src/framebuffer.rs index 1477015..7e7a248 100644 --- a/src/framebuffer.rs +++ b/src/framebuffer.rs @@ -5,7 +5,7 @@ use crate::{Attr, Color}; const CHUNK_SIZE: usize = 1024; /// Represents a single cell in the framebuffer. -#[derive(Clone, PartialEq, Debug)] +#[derive(Clone, Copy, PartialEq, Debug)] struct Cell { /// The character displayed in the cell. pub ch: char, @@ -258,7 +258,7 @@ impl Framebuffer { for x in 0..other.width { if x + x_offset < self.width && y + y_offset < self.height { let idx = (y + y_offset) * self.width + (x + x_offset); - self.buffer[idx] = other.buffer[y * other.width + x].clone(); + self.buffer[idx] = other.buffer[y * other.width + x]; } } } @@ -331,7 +331,7 @@ impl Framebuffer { chunk.push_str(&back.bg.to_ansi(false)); } chunk.push(back.ch); // Add the character - self.buffer[idx] = back.clone(); // Copy the Cell to the front buffer + self.buffer[idx] = *back; // Copy the Cell to the front buffer if chunk.len() >= CHUNK_SIZE { stdout_lock.write_all(chunk.as_bytes())?; diff --git a/src/window.rs b/src/window.rs index 5ac930b..30101f4 100644 --- a/src/window.rs +++ b/src/window.rs @@ -99,7 +99,7 @@ impl Window { 2, 1, &format!("FPS: {fps:.2}"), - Attr::BOLD, + Attr::Bold, Color::Green, Color::default(), Align::Left, From a0d0887208d4e2b7ae16a0579d61b62a8969a460 Mon Sep 17 00:00:00 2001 From: broccolingual Date: Wed, 3 Sep 2025 03:14:34 +0900 Subject: [PATCH 2/2] fix: update attribute mappings to remove deprecated variants --- src/attr.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/attr.rs b/src/attr.rs index 9f4d915..71e29c6 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -46,17 +46,17 @@ impl Attr { } let attr_mappings = [ - (Attr::NORMAL | Attr::Normal, "0"), - (Attr::BOLD | Attr::Bold, "1"), - (Attr::THIN | Attr::Thin, "2"), - (Attr::ITALIC | Attr::Italic, "3"), - (Attr::UNDERLINE | Attr::Underline, "4"), - (Attr::BLINK | Attr::Blink, "5"), - (Attr::FASTBLINK | Attr::FastBlink, "6"), - (Attr::INVERT | Attr::Invert, "7"), - (Attr::HIDDEN | Attr::Hidden, "8"), - (Attr::REMOVE | Attr::Remove, "9"), - (Attr::PRIMARY | Attr::Primary, "10"), + (Attr::Normal, "0"), + (Attr::Bold, "1"), + (Attr::Thin, "2"), + (Attr::Italic, "3"), + (Attr::Underline, "4"), + (Attr::Blink, "5"), + (Attr::FastBlink, "6"), + (Attr::Invert, "7"), + (Attr::Hidden, "8"), + (Attr::Remove, "9"), + (Attr::Primary, "10"), ]; let mut buf = String::with_capacity(24);