Fix display issues

This commit is contained in:
Artlef 2020-11-22 16:15:58 +01:00
parent 11e3c7d48e
commit fdb701dbf1

View File

@ -7,7 +7,7 @@ use std::os::unix::net::UnixStream;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::thread;
use std::time;
use termion::event::Key;
use termion::event::{parse_event, Event, Key};
use std::io::{stdin, stdout, Read, Write};
use termion::raw::{IntoRawMode, RawTerminal};
@ -64,6 +64,7 @@ fn main() {
let login_result = login(&mut client, &mut stream);
if login_result == LoginResult::UserExited {
send_request(&client, &mut stream, ClientRequest::Exit);
client.display_sender.send(DisplayMessage::Exit).unwrap();
return;
}
if client.player.role == UserRole::Black {
@ -148,7 +149,7 @@ fn main() {
}
client.display_sender.send(DisplayMessage::Clear).unwrap();
let end_message = match current_position.outcome() {
None => "Bye",
None => "",
Some(Outcome::Draw) => "Draw game.",
Some(Outcome::Decisive { winner }) => {
if winner == Color::White {
@ -159,24 +160,14 @@ fn main() {
}
}
.to_string();
if !end_message.is_empty() {
client
.display_sender
.send(DisplayMessage::Message(end_message))
.unwrap();
thread::sleep(time::Duration::from_secs(1));
client
.display_sender
.send(DisplayMessage::Debug(
"sending request to server to exit...".to_string(),
))
.unwrap();
}
send_request(&client, &mut stream, ClientRequest::Exit);
client
.display_sender
.send(DisplayMessage::Debug(
"request to server to exit sent.".to_string(),
))
.unwrap();
client.display_sender.send(DisplayMessage::Exit).unwrap();
}
@ -238,6 +229,7 @@ pub enum DisplayMessage {
Information(String),
Debug(String),
Input(Key),
RemoveLastInput,
Enter,
Clear,
Exit,
@ -291,6 +283,7 @@ fn start_display_thread(request_recv: Receiver<DisplayMessage>) {
)
.unwrap(),
DisplayMessage::Input(k) => display_key(&mut stdout, k),
DisplayMessage::RemoveLastInput => write!(stdout, "{}{}", termion::cursor::Left(1), termion::clear::AfterCursor).unwrap(),
DisplayMessage::Debug(s) => write!(
stdout,
"{}{}{}{}{}",
@ -322,15 +315,16 @@ fn start_keyboard_input_thread(
let mut bytes = stdin.bytes();
loop {
let b = bytes.next().unwrap().unwrap();
match b {
b'q' => {
let e = parse_event(b, &mut bytes).unwrap();
match e {
Event::Key(Key::Ctrl('c')) => {
sender.send(String::from(EXIT_MSG)).unwrap();
waiting_server_msg_sender
.send(WaitingServerMsg::UserCanceled)
.unwrap();
break;
}
b'\r' => {
},
Event::Key(Key::Char('\n')) => {
display_sender.send(DisplayMessage::Enter).unwrap();
sender.send(buffer.clone()).unwrap();
if buffer == EXIT_MSG {
@ -339,15 +333,23 @@ fn start_keyboard_input_thread(
.unwrap();
}
buffer.clear();
}
b => {
let c = b as char;
},
Event::Key(Key::Char(c)) => {
buffer.push_str(&c.to_string());
display_sender
.send(DisplayMessage::Input(Key::Char(c)))
.unwrap();
}
}
},
Event::Key(Key::Backspace) => {
buffer.pop();
display_sender
.send(DisplayMessage::RemoveLastInput)
.unwrap();
},
Event::Key(k) => display_sender.send(DisplayMessage::Debug(format!("unknown key {:?}", k))).unwrap(),
Event::Mouse(_) => continue,
Event::Unsupported(_) => continue
};
}
});
receiver
@ -466,6 +468,7 @@ fn prompt_user_for_role(client: &Client, stream: &mut UnixStream) -> PromptedRol
let mut input = client.keyboard_input_recv.recv().unwrap();
input = String::from(input.trim());
if input == EXIT_MSG {
client.waiting_server_msg_receiver.recv().unwrap();
return PromptedRoleResponse::Exit;
}