diff --git a/src/bin/client.rs b/src/bin/client.rs index 7b8fb3c..d278e6b 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -239,6 +239,7 @@ pub enum DisplayMessage { Debug(String), Input(Key), RemoveLastInput, + SwitchAsciiMode, Enter, Clear, Exit, @@ -256,12 +257,31 @@ fn start_display_thread(request_recv: Receiver) { ) .unwrap(); stdout.flush().unwrap(); + let mut ascii_mode = false; + let mut last_fen_position = String::default(); + let mut last_side = Color::White; loop { let msg = request_recv.recv().unwrap(); match msg { DisplayMessage::Chessboard { fen, side } => { let current_position = clichess::parse_position(&fen); - clichess::print_board_representation(¤t_position, side, &mut stdout); + last_fen_position = fen.clone(); + last_side = side.clone(); + clichess::print_board_representation( + ¤t_position, + side, + &mut stdout, + ascii_mode, + ); + } + DisplayMessage::SwitchAsciiMode => { + ascii_mode = !ascii_mode; + clichess::print_board_representation( + &clichess::parse_position(&last_fen_position), + last_side, + &mut stdout, + ascii_mode, + ); } DisplayMessage::Message(s) => write!( stdout, @@ -355,6 +375,11 @@ fn start_keyboard_input_thread( } buffer.clear(); } + Event::Key(Key::Char('@')) => { + display_sender + .send(DisplayMessage::SwitchAsciiMode) + .unwrap(); + } Event::Key(Key::Char(c)) => { buffer.push_str(&c.to_string()); display_sender diff --git a/src/lib.rs b/src/lib.rs index 0a0800c..c19c64c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -133,6 +133,7 @@ pub fn print_board_representation( chess: &Chess, side: Color, stdout: &mut RawTerminal, + ascii_mode: bool, ) { let mut rank_numbers; if side == Color::White { @@ -141,10 +142,10 @@ pub fn print_board_representation( rank_numbers = 1; } let mut linenb = 6; - for v in board_string_representation(chess, side) { + for v in board_string_representation(chess, side, ascii_mode) { write!(stdout, "{}", termion::cursor::Goto(10, linenb)).unwrap(); for square_to_print in v { - print_square(&square_to_print, stdout); + print_square(&square_to_print, stdout, ascii_mode); } linenb += 1; resetcolors(stdout); @@ -169,7 +170,11 @@ pub fn print_board_representation( stdout.flush().unwrap(); } -fn print_square(square_to_print: &SquareToPrint, stdout: &mut RawTerminal) { +fn print_square( + square_to_print: &SquareToPrint, + stdout: &mut RawTerminal, + ascii_mode: bool, +) { match square_to_print.color.as_ref() { "black" => write!(stdout, "{}", termion::color::Fg(termion::color::Black)), _ => write!(stdout, "{}", termion::color::Fg(termion::color::Reset)), @@ -182,6 +187,10 @@ fn print_square(square_to_print: &SquareToPrint, stdout: &mut RawTerminal write!(stdout, "{}", termion::color::Bg(termion::color::Reset)), } .unwrap(); + if ascii_mode { + write!(stdout, "{}", termion::color::Fg(termion::color::Reset)).unwrap(); + write!(stdout, "{}", termion::color::Bg(termion::color::Reset)).unwrap(); + } write!(stdout, "{}", square_to_print.square_representation).unwrap(); } @@ -190,7 +199,11 @@ fn resetcolors(stdout: &mut RawTerminal) { write!(stdout, "{}", termion::color::Bg(termion::color::Reset)).unwrap(); } -fn board_string_representation(chess: &Chess, side: Color) -> Vec> { +fn board_string_representation( + chess: &Chess, + side: Color, + ascii_mode: bool, +) -> Vec> { let mut full_board_to_print = Vec::new(); for _ in 0..8 { full_board_to_print.push(Vec::new()) @@ -205,7 +218,7 @@ fn board_string_representation(chess: &Chess, side: Color) -> Vec Result String { +fn get_square_representation(square: &Square, chess: &Chess, ascii_mode: bool) -> String { let board = (*chess).board(); match board.piece_at(*square) { - Some(piece) => format!("{} ", piece_char_to_utf8(piece.char())), - None => " ".to_string(), + Some(piece) => format!( + "{} ", + if ascii_mode { + piece.char() + } else { + piece_char_to_utf8(piece.char()) + } + ), + None => if ascii_mode { ". " } else { " " }.to_string(), } }