Add ascii mode: @ print the board using only ascii
This commit is contained in:
parent
4c54d9b5e5
commit
69adc99e19
@ -239,6 +239,7 @@ pub enum DisplayMessage {
|
|||||||
Debug(String),
|
Debug(String),
|
||||||
Input(Key),
|
Input(Key),
|
||||||
RemoveLastInput,
|
RemoveLastInput,
|
||||||
|
SwitchAsciiMode,
|
||||||
Enter,
|
Enter,
|
||||||
Clear,
|
Clear,
|
||||||
Exit,
|
Exit,
|
||||||
@ -256,12 +257,31 @@ fn start_display_thread(request_recv: Receiver<DisplayMessage>) {
|
|||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
stdout.flush().unwrap();
|
stdout.flush().unwrap();
|
||||||
|
let mut ascii_mode = false;
|
||||||
|
let mut last_fen_position = String::default();
|
||||||
|
let mut last_side = Color::White;
|
||||||
loop {
|
loop {
|
||||||
let msg = request_recv.recv().unwrap();
|
let msg = request_recv.recv().unwrap();
|
||||||
match msg {
|
match msg {
|
||||||
DisplayMessage::Chessboard { fen, side } => {
|
DisplayMessage::Chessboard { fen, side } => {
|
||||||
let current_position = clichess::parse_position(&fen);
|
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!(
|
DisplayMessage::Message(s) => write!(
|
||||||
stdout,
|
stdout,
|
||||||
@ -355,6 +375,11 @@ fn start_keyboard_input_thread(
|
|||||||
}
|
}
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
}
|
}
|
||||||
|
Event::Key(Key::Char('@')) => {
|
||||||
|
display_sender
|
||||||
|
.send(DisplayMessage::SwitchAsciiMode)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
Event::Key(Key::Char(c)) => {
|
Event::Key(Key::Char(c)) => {
|
||||||
buffer.push_str(&c.to_string());
|
buffer.push_str(&c.to_string());
|
||||||
display_sender
|
display_sender
|
||||||
|
36
src/lib.rs
36
src/lib.rs
@ -133,6 +133,7 @@ pub fn print_board_representation(
|
|||||||
chess: &Chess,
|
chess: &Chess,
|
||||||
side: Color,
|
side: Color,
|
||||||
stdout: &mut RawTerminal<io::StdoutLock>,
|
stdout: &mut RawTerminal<io::StdoutLock>,
|
||||||
|
ascii_mode: bool,
|
||||||
) {
|
) {
|
||||||
let mut rank_numbers;
|
let mut rank_numbers;
|
||||||
if side == Color::White {
|
if side == Color::White {
|
||||||
@ -141,10 +142,10 @@ pub fn print_board_representation(
|
|||||||
rank_numbers = 1;
|
rank_numbers = 1;
|
||||||
}
|
}
|
||||||
let mut linenb = 6;
|
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();
|
write!(stdout, "{}", termion::cursor::Goto(10, linenb)).unwrap();
|
||||||
for square_to_print in v {
|
for square_to_print in v {
|
||||||
print_square(&square_to_print, stdout);
|
print_square(&square_to_print, stdout, ascii_mode);
|
||||||
}
|
}
|
||||||
linenb += 1;
|
linenb += 1;
|
||||||
resetcolors(stdout);
|
resetcolors(stdout);
|
||||||
@ -169,7 +170,11 @@ pub fn print_board_representation(
|
|||||||
stdout.flush().unwrap();
|
stdout.flush().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_square(square_to_print: &SquareToPrint, stdout: &mut RawTerminal<io::StdoutLock>) {
|
fn print_square(
|
||||||
|
square_to_print: &SquareToPrint,
|
||||||
|
stdout: &mut RawTerminal<io::StdoutLock>,
|
||||||
|
ascii_mode: bool,
|
||||||
|
) {
|
||||||
match square_to_print.color.as_ref() {
|
match square_to_print.color.as_ref() {
|
||||||
"black" => write!(stdout, "{}", termion::color::Fg(termion::color::Black)),
|
"black" => write!(stdout, "{}", termion::color::Fg(termion::color::Black)),
|
||||||
_ => write!(stdout, "{}", termion::color::Fg(termion::color::Reset)),
|
_ => write!(stdout, "{}", termion::color::Fg(termion::color::Reset)),
|
||||||
@ -182,6 +187,10 @@ fn print_square(square_to_print: &SquareToPrint, stdout: &mut RawTerminal<io::St
|
|||||||
_ => write!(stdout, "{}", termion::color::Bg(termion::color::Reset)),
|
_ => write!(stdout, "{}", termion::color::Bg(termion::color::Reset)),
|
||||||
}
|
}
|
||||||
.unwrap();
|
.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();
|
write!(stdout, "{}", square_to_print.square_representation).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,7 +199,11 @@ fn resetcolors(stdout: &mut RawTerminal<io::StdoutLock>) {
|
|||||||
write!(stdout, "{}", termion::color::Bg(termion::color::Reset)).unwrap();
|
write!(stdout, "{}", termion::color::Bg(termion::color::Reset)).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn board_string_representation(chess: &Chess, side: Color) -> Vec<Vec<SquareToPrint>> {
|
fn board_string_representation(
|
||||||
|
chess: &Chess,
|
||||||
|
side: Color,
|
||||||
|
ascii_mode: bool,
|
||||||
|
) -> Vec<Vec<SquareToPrint>> {
|
||||||
let mut full_board_to_print = Vec::new();
|
let mut full_board_to_print = Vec::new();
|
||||||
for _ in 0..8 {
|
for _ in 0..8 {
|
||||||
full_board_to_print.push(Vec::new())
|
full_board_to_print.push(Vec::new())
|
||||||
@ -205,7 +218,7 @@ fn board_string_representation(chess: &Chess, side: Color) -> Vec<Vec<SquareToPr
|
|||||||
} else {
|
} else {
|
||||||
"green".to_string()
|
"green".to_string()
|
||||||
},
|
},
|
||||||
square_representation: get_square_representation(&square, chess),
|
square_representation: get_square_representation(&square, chess, ascii_mode),
|
||||||
};
|
};
|
||||||
full_board_to_print[j as usize].push(square_to_print);
|
full_board_to_print[j as usize].push(square_to_print);
|
||||||
}
|
}
|
||||||
@ -229,11 +242,18 @@ pub fn try_to_play_move(chess: &Chess, movestr: String) -> Result<Chess, MoveInp
|
|||||||
Ok((*chess).clone().play(&mv)?)
|
Ok((*chess).clone().play(&mv)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_square_representation(square: &Square, chess: &Chess) -> String {
|
fn get_square_representation(square: &Square, chess: &Chess, ascii_mode: bool) -> String {
|
||||||
let board = (*chess).board();
|
let board = (*chess).board();
|
||||||
match board.piece_at(*square) {
|
match board.piece_at(*square) {
|
||||||
Some(piece) => format!("{} ", piece_char_to_utf8(piece.char())),
|
Some(piece) => format!(
|
||||||
None => " ".to_string(),
|
"{} ",
|
||||||
|
if ascii_mode {
|
||||||
|
piece.char()
|
||||||
|
} else {
|
||||||
|
piece_char_to_utf8(piece.char())
|
||||||
|
}
|
||||||
|
),
|
||||||
|
None => if ascii_mode { ". " } else { " " }.to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user