Improve cli screens dev integration
This commit is contained in:
parent
5d4940adc6
commit
276f4508c0
1
rust/.gitignore
vendored
1
rust/.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
/target
|
||||
.vscode
|
||||
.idea
|
||||
|
@ -1,4 +1,10 @@
|
||||
use clap::Parser;
|
||||
use clap::{Parser, ValueEnum};
|
||||
|
||||
#[derive(Clone, Copy, Debug, ValueEnum)]
|
||||
pub enum TestDevScreen {
|
||||
Popup,
|
||||
Input,
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
pub struct CliArgs {
|
||||
@ -15,6 +21,9 @@ pub struct CliArgs {
|
||||
/// Local server listen address
|
||||
#[clap(short, long, default_value = "127.0.0.1:5679")]
|
||||
pub listen_address: String,
|
||||
|
||||
#[clap(long, value_enum)]
|
||||
pub dev_screen: Option<TestDevScreen>,
|
||||
}
|
||||
|
||||
impl CliArgs {
|
||||
|
@ -1,7 +1,9 @@
|
||||
use std::error::Error;
|
||||
use std::fmt::Debug;
|
||||
use std::io;
|
||||
use std::io::ErrorKind;
|
||||
|
||||
use cli_player::cli_args::{cli_args, TestDevScreen};
|
||||
use crossterm::event::DisableMouseCapture;
|
||||
use crossterm::event::EnableMouseCapture;
|
||||
use crossterm::execute;
|
||||
@ -13,26 +15,45 @@ use tui::backend::{Backend, CrosstermBackend};
|
||||
use tui::Terminal;
|
||||
|
||||
use cli_player::server::start_server_if_missing;
|
||||
use cli_player::ui_screens::popup_screen::PopupScreen;
|
||||
use cli_player::ui_screens::*;
|
||||
use sea_battle_backend::data::GameRules;
|
||||
|
||||
async fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> Result<(), Box<dyn Error>> {
|
||||
async fn run_dev<B: Backend>(
|
||||
terminal: &mut Terminal<B>,
|
||||
d: TestDevScreen,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
let res = match d {
|
||||
TestDevScreen::Popup => PopupScreen::new("Welcome there!!")
|
||||
.show(terminal)?
|
||||
.as_string(),
|
||||
TestDevScreen::Input => input_screen::InputScreen::new("Whas it your name ?")
|
||||
.set_title("A custom title")
|
||||
.show(terminal)?
|
||||
.as_string(),
|
||||
};
|
||||
|
||||
// Temporary code
|
||||
// let res = configure_game_rules::configure_play_rules(GameRules::default(), terminal)?; // select_bot_type::select_bot_type(terminal)?;
|
||||
/*let mut rules = GameRules::default();
|
||||
rules.boats_can_touch = true;
|
||||
let res = set_boats_layout::set_boat_layout(&rules, terminal)?; // select_bot_type::select_bot_type(terminal)?;*/
|
||||
/*let res = popup_screen::popup_screen("Hi\nWelcome there", terminal);*/
|
||||
// let res = confirm_dialog::confirm_dialog("Do you really want to interrupt game ?", terminal)?; // select_bot_type::select_bot_type(terminal)?;
|
||||
let res = input_screen::InputScreen::new("Whas it your name ?")
|
||||
.set_title("custom title")
|
||||
.show(terminal)?; // select_bot_type::select_bot_type(terminal)?;
|
||||
// select_bot_type::select_bot_type(terminal)?;
|
||||
Err(io::Error::new(
|
||||
ErrorKind::Other,
|
||||
format!("result: {:?}", res),
|
||||
))?;
|
||||
format!("DEV result: {:?}", res),
|
||||
))?
|
||||
}
|
||||
|
||||
async fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> Result<(), Box<dyn Error>> {
|
||||
if let Some(d) = cli_args().dev_screen {
|
||||
run_dev(terminal, d).await
|
||||
} else {
|
||||
// TODO : run app
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
@ -1,3 +1,5 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
pub mod configure_game_rules;
|
||||
pub mod confirm_dialog;
|
||||
pub mod input_screen;
|
||||
@ -12,3 +14,9 @@ pub enum ScreenResult<E> {
|
||||
Ok(E),
|
||||
Canceled,
|
||||
}
|
||||
|
||||
impl<E: Debug> ScreenResult<E> {
|
||||
pub fn as_string(&self) -> String {
|
||||
format!("{:#?}", self)
|
||||
}
|
||||
}
|
||||
|
@ -14,23 +14,23 @@ use crate::ui_screens::utils::centered_rect_size;
|
||||
use crate::ui_screens::ScreenResult;
|
||||
use crate::ui_widgets::button_widget::ButtonWidget;
|
||||
|
||||
struct PopupScreen<'a> {
|
||||
pub struct PopupScreen<'a> {
|
||||
title: &'a str,
|
||||
msg: &'a str,
|
||||
}
|
||||
|
||||
pub fn popup_screen<B: Backend>(
|
||||
msg: &str,
|
||||
terminal: &mut Terminal<B>,
|
||||
) -> io::Result<ScreenResult<()>> {
|
||||
let mut model = PopupScreen {
|
||||
impl<'a> PopupScreen<'a> {
|
||||
pub fn new(msg: &'a str) -> Self {
|
||||
Self {
|
||||
title: "Message",
|
||||
msg,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn show<B: Backend>(mut self, terminal: &mut Terminal<B>) -> io::Result<ScreenResult<()>> {
|
||||
let mut last_tick = Instant::now();
|
||||
loop {
|
||||
terminal.draw(|f| ui(f, &mut model))?;
|
||||
terminal.draw(|f| self.ui(f))?;
|
||||
|
||||
let timeout = TICK_RATE
|
||||
.checked_sub(last_tick.elapsed())
|
||||
@ -53,14 +53,14 @@ pub fn popup_screen<B: Backend>(
|
||||
}
|
||||
}
|
||||
|
||||
fn ui<B: Backend>(f: &mut Frame<B>, model: &mut PopupScreen) {
|
||||
fn ui<B: Backend>(&mut self, f: &mut Frame<B>) {
|
||||
// Preprocess message
|
||||
let lines = textwrap::wrap(model.msg, f.size().width as usize - 20);
|
||||
let lines = textwrap::wrap(self.msg, f.size().width as usize - 20);
|
||||
let line_max_len = lines.iter().map(|l| l.len()).max().unwrap();
|
||||
|
||||
let area = centered_rect_size(line_max_len as u16 + 4, 5 + lines.len() as u16, &f.size());
|
||||
|
||||
let block = Block::default().borders(Borders::ALL).title(model.title);
|
||||
let block = Block::default().borders(Borders::ALL).title(self.title);
|
||||
f.render_widget(block, area);
|
||||
|
||||
// Create two chunks with equal horizontal screen space
|
||||
@ -88,3 +88,4 @@ fn ui<B: Backend>(f: &mut Frame<B>, model: &mut PopupScreen) {
|
||||
let ok_button = ButtonWidget::new("OK", true);
|
||||
f.render_widget(ok_button, chunks[1]);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user