From f081238e334f8f46bfa6cfbaeffc10a65b748259 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Thu, 8 Jun 2023 17:26:01 +0700 Subject: [PATCH] Prompt the user for a project name if it can't find the database file --- src/app.rs | 2 +- src/main.rs | 51 ++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/app.rs b/src/app.rs index 0b11bf7..9424124 100644 --- a/src/app.rs +++ b/src/app.rs @@ -169,7 +169,7 @@ impl Project { pub fn load(path: String, mut file: &File) -> Result { let mut json = String::new(); file.read_to_string(&mut json)?; - if json.is_empty() { + if json.trim().is_empty() { Ok(Project::new("", path)) } else { Self::load_from_json(&json) diff --git a/src/main.rs b/src/main.rs index 581f740..1cb5ddf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,29 +6,47 @@ use crossterm::{ event::*, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, }; -use std::{io, env, path::PathBuf, fs::{File, OpenOptions}, error::Error}; +use std::{io::{self, Write}, env, path::PathBuf, fs::{File, OpenOptions}, error::Error}; use tui::backend::CrosstermBackend; use tui::Terminal; -use clap::Parser; +use clap::{Parser, ValueHint::FilePath}; #[derive(Debug, Parser)] #[command(name = "kanban")] /// kanban-tui is a simple, interactive TUI based task manager using kanban columns pub struct CliArgs { - #[arg(short('d'), long("database"), value_name="DATABASE")] + #[arg(short('d'), long("database"), value_name="DATABASE", value_hint=FilePath)] /// Path to the pub filepath: Option } -fn prompt_project_init() -> (String, io::Result) { - let _msg = "Database not found, would you like to start a new project?"; - let filepath = "kanban.json"; - (filepath.to_string(), +const DEFAULT_DATABASE_NAME: &str = "kanban.json"; + +fn prompt_project_init(default_name: &str) -> (String, io::Result) { + let mut input = String::new(); + + println!("Database not found, select the name of the database if it exists or enter a name to start a new project"); + print!("Database name (default: {}): ", default_name); + io::stdout().flush().unwrap(); + + let result = io::stdin().read_line(&mut input); + let input = input.trim(); + + let filename = + match result { + Ok(b) if b > 0 && !input.is_empty() => input, + _ => default_name + }; + + // TODO: This might be a good time to prompt the user if they want + // to change the default column names + + (filename.to_string(), OpenOptions::new() .write(true) .read(true) .create(true) - .open(filepath)) + .open(filename)) } fn main() -> anyhow::Result<(), Box> { @@ -39,20 +57,27 @@ fn main() -> anyhow::Result<(), Box> { let file = OpenOptions::new() .write(true) .read(true) - .create(true) .open(&fpath); match file { Ok(f) => (fpath, f), Err(_) => { - let (fp, fname) = prompt_project_init(); + let (fp, fname) = prompt_project_init(&fpath); (fp, fname.unwrap()) } } }, CliArgs { filepath: None } => { - let (fp, fname) = prompt_project_init(); - (fp, fname.unwrap()) + let file = OpenOptions::new() + .write(true) + .read(true) + .open(DEFAULT_DATABASE_NAME); + if let Ok(f) = file { + (DEFAULT_DATABASE_NAME.to_string(), f) + } else { + let (fp, fname) = prompt_project_init(DEFAULT_DATABASE_NAME); + (fp, fname.unwrap()) + } } }; let mut state = AppState::new(Project::load(filepath, &file)?); @@ -68,7 +93,7 @@ fn main() -> anyhow::Result<(), Box> { kanban_tui::handle_input(&mut state)?; } - state.project.save(); + // state.project.save(); // restore terminal disable_raw_mode()?;