xiangqi_tui 0.1.0

Chinese chess (Xiangqi) TUI client with UCI/UCCI engine and opening book support
//! 棋子汉字与格内样式。

use ratatui::style::{Color, Modifier, Style};

#[inline]
pub fn piece_label(cell: u8) -> Option<(&'static str, bool)> {
    if cell == 0 {
        return None;
    }
    let (kind, red) = piece_kind_side(cell)?;
    let label = match kind {
        5 => {
            if red {
                ""
            } else {
                ""
            }
        }
        4 => {
            if red {
                ""
            } else {
                ""
            }
        }
        3 => {
            if red {
                ""
            } else {
                ""
            }
        }
        2 => {
            if red {
                ""
            } else {
                ""
            }
        }
        1 => {
            if red {
                ""
            } else {
                ""
            }
        }
        6 => {
            if red {
                ""
            } else {
                ""
            }
        }
        7 => {
            if red {
                ""
            } else {
                ""
            }
        }
        _ => return None,
    };
    Some((label, red))
}

fn piece_kind_side(cell: u8) -> Option<(u8, bool)> {
    if cell == 0 {
        None
    } else if (1..=7).contains(&cell) {
        Some((cell, true))
    } else if (9..=15).contains(&cell) {
        Some((cell - 8, false))
    } else {
        None
    }
}

pub fn piece_cell_style(red: bool) -> Style {
    if red {
        Style::default()
            .fg(Color::White)
            .bg(Color::Rgb(188, 51, 40))
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default()
            .fg(Color::White)
            .bg(Color::Rgb(52, 52, 52))
            .add_modifier(Modifier::BOLD)
    }
}