From d33b871164e8a1f8c0c3743caae9a71e10d688e1 Mon Sep 17 00:00:00 2001 From: Hubert Jaremko Date: Thu, 28 Oct 2021 20:02:17 +0200 Subject: [PATCH] Display prompt for task --- src/command/prompt.rs | 29 +++++++++++++++++++++++++++++ src/command/submit.rs | 15 ++++++++++----- src/model/task.rs | 7 +++++++ tests/commands/submit.rs | 15 --------------- 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/command/prompt.rs b/src/command/prompt.rs index 65e83a7..62546a0 100644 --- a/src/command/prompt.rs +++ b/src/command/prompt.rs @@ -1,6 +1,10 @@ use crate::error; +use crate::model::Tasks; +use dialoguer::theme::ColorfulTheme; +use dialoguer::Select; #[cfg(test)] use mockall::{automock, predicate::*}; +use tracing::info; #[cfg_attr(test, automock)] pub trait Prompt { @@ -26,3 +30,28 @@ impl Prompt for Password { .interact()?) } } + +pub struct TaskChoice { + available_tasks: Tasks, +} + +impl TaskChoice { + pub fn new(available_tasks: Tasks) -> Self { + Self { available_tasks } + } +} + +impl Prompt for TaskChoice { + fn interact(&self) -> error::Result { + let items = &self.available_tasks.tasks; + + let selection = Select::with_theme(&ColorfulTheme::default()) + .items(items) + .with_prompt("Choose task:") + .default(0) + .interact()?; + + info!("Selection index: {}", selection); + Ok(items[selection].id.clone()) + } +} diff --git a/src/command/submit.rs b/src/command/submit.rs index 53fdc2f..5156d79 100644 --- a/src/command/submit.rs +++ b/src/command/submit.rs @@ -1,6 +1,7 @@ use crate::api::baca_api::BacaApi; use crate::command::log::Log; -use crate::command::Command; +use crate::command::prompt::Prompt; +use crate::command::{prompt, Command}; use crate::error::{Error, Result}; use crate::model::Language; use crate::workspace::config_editor::ConfigEditor; @@ -121,14 +122,18 @@ impl Submit { { let (ask_for_save, mut submit_config) = self.merge_saved_and_provided_configs(workspace)?; - if submit_config.id.is_none() { - return Err(Error::SubmitArgumentNotProvided("task_id".to_string())); - } - if submit_config.file().is_none() { return Err(Error::SubmitArgumentNotProvided("file".to_string())); } + if submit_config.id.is_none() { + let connection_config = ConnectionConfig::read_config(workspace)?; + + let tasks = api.get_tasks(&connection_config)?; + let task_choice = prompt::TaskChoice::new(tasks); + submit_config.id = task_choice.interact()?.into(); + } + if submit_config.language.is_none() { let allowed_language = Self::fetch_allowed_language(workspace, api, submit_config.id().unwrap())?; diff --git a/src/model/task.rs b/src/model/task.rs index ba2d985..2449725 100644 --- a/src/model/task.rs +++ b/src/model/task.rs @@ -1,4 +1,5 @@ use crate::model::Language; +use std::fmt::{Display, Formatter}; #[derive(Debug, Clone, PartialEq)] pub struct Task { @@ -19,3 +20,9 @@ impl Task { } } } + +impl Display for Task { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.problem_name) + } +} diff --git a/tests/commands/submit.rs b/tests/commands/submit.rs index 6b8cb12..18a0ba8 100644 --- a/tests/commands/submit.rs +++ b/tests/commands/submit.rs @@ -69,21 +69,6 @@ fn inactive_task_should_report_error() -> Result<(), Box> Ok(()) } -#[test] -fn no_task_id_should_report_error() -> Result<(), Box> { - let dir = initialize_correct_workspace()?; - let mut cmd = set_up_command(&dir)?; - make_input_file_dummy(&dir)?; - - cmd.args(&["submit", "-l", "C++", "-f", "dummy.txt"]); - - cmd.assert() - .stdout(predicate::str::contains("provide task_id")); - - dir.close()?; - Ok(()) -} - #[test] fn no_file_should_report_error() -> Result<(), Box> { let dir = initialize_correct_workspace()?;