Skip to content
Merged
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
canvas.set_named_border(
"HELLO WORLD",
Align::Right,
Attr::NORMAL,
Attr::default(),
Color::White,
Color::default(),
); // Set a named border for the canvas
canvas.set_str(
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
);
})?;

Expand Down
6 changes: 3 additions & 3 deletions examples/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
canvas.set_named_border(
"COLORS",
Align::Right,
Attr::NORMAL,
Attr::default(),
Color::White,
Color::default(),
);
Expand Down Expand Up @@ -50,7 +50,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
canvas.width / 2 + 1,
canvas.height / 2,
"Color Circle",
Attr::BOLD,
Attr::Bold,
Color::White,
Color::default(),
Align::Center,
Expand All @@ -59,7 +59,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3,
2,
"Press 'q' to quit",
Attr::BOLD,
Attr::Bold,
Color::White,
Color::default(),
Align::Left,
Expand Down
8 changes: 4 additions & 4 deletions examples/file_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
Expand All @@ -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())
};

// 行番号を表示
Expand Down Expand Up @@ -193,7 +193,7 @@ impl FileReader {
canvas.width / 2,
canvas.height - 1,
&status,
Attr::NORMAL,
Attr::default(),
Color::Yellow,
Color::default(),
Align::Center,
Expand Down
4 changes: 2 additions & 2 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
canvas.set_named_border(
"HELLO WORLD",
Align::Right,
Attr::NORMAL,
Attr::default(),
Color::White,
Color::default(),
); // Set a named border for the canvas
canvas.set_str(
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
Expand Down
4 changes: 2 additions & 2 deletions examples/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
canvas.set_named_border(
"INPUTS",
Align::Right,
Attr::NORMAL,
Attr::default(),
Color::White,
Color::default(),
);
Expand All @@ -51,7 +51,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
canvas.width / 2,
canvas.height / 2,
&full_text,
Attr::NORMAL,
Attr::default(),
Color::White,
Color::default(),
Align::Center,
Expand Down
14 changes: 7 additions & 7 deletions examples/tetris/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -153,7 +153,7 @@ impl Core {
x * 2 + 1,
y + 1,
"──",
Attr::NORMAL,
Attr::default(),
Color::White,
color,
Align::Left,
Expand All @@ -164,7 +164,7 @@ impl Core {
x * 2 + 1,
y + 1,
" ",
Attr::NORMAL,
Attr::default(),
Color::White,
color,
Align::Left,
Expand All @@ -178,7 +178,7 @@ impl Core {
self.next_block_frame.set_named_border(
"NEXT",
Align::Center,
Attr::NORMAL,
Attr::default(),
Color::White,
Color::default(),
);
Expand All @@ -191,7 +191,7 @@ impl Core {
self.holding_block_frame.set_named_border(
"HOLD",
Align::Center,
Attr::NORMAL,
Attr::default(),
Color::White,
Color::default(),
);
Expand Down
6 changes: 3 additions & 3 deletions examples/tetris/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
canvas.set_named_border(
"TETRIS",
Align::Right,
Attr::NORMAL,
Attr::default(),
Color::White,
Color::default(),
);
Expand Down Expand Up @@ -92,12 +92,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

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,
Expand Down
57 changes: 34 additions & 23 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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, "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);
Expand Down
6 changes: 3 additions & 3 deletions src/framebuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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];
}
}
}
Expand Down Expand Up @@ -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())?;
Expand Down
2 changes: 1 addition & 1 deletion src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl Window {
2,
1,
&format!("FPS: {fps:.2}"),
Attr::BOLD,
Attr::Bold,
Color::Green,
Color::default(),
Align::Left,
Expand Down