Skip to content

Commit

Permalink
Display prompt for task
Browse files Browse the repository at this point in the history
  • Loading branch information
hjaremko committed Oct 28, 2021
1 parent 5a64d25 commit d33b871
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
29 changes: 29 additions & 0 deletions src/command/prompt.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<String> {
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())
}
}
15 changes: 10 additions & 5 deletions src/command/submit.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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())?;
Expand Down
7 changes: 7 additions & 0 deletions src/model/task.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::model::Language;
use std::fmt::{Display, Formatter};

#[derive(Debug, Clone, PartialEq)]
pub struct Task {
Expand All @@ -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)
}
}
15 changes: 0 additions & 15 deletions tests/commands/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,6 @@ fn inactive_task_should_report_error() -> Result<(), Box<dyn std::error::Error>>
Ok(())
}

#[test]
fn no_task_id_should_report_error() -> Result<(), Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
let dir = initialize_correct_workspace()?;
Expand Down

0 comments on commit d33b871

Please sign in to comment.