Prompt the user for a project name if it can't find the database file
This commit is contained in:
parent
bc81c05f1f
commit
f081238e33
@ -169,7 +169,7 @@ impl Project {
|
|||||||
pub fn load(path: String, mut file: &File) -> Result<Self, KanbanError> {
|
pub fn load(path: String, mut file: &File) -> Result<Self, KanbanError> {
|
||||||
let mut json = String::new();
|
let mut json = String::new();
|
||||||
file.read_to_string(&mut json)?;
|
file.read_to_string(&mut json)?;
|
||||||
if json.is_empty() {
|
if json.trim().is_empty() {
|
||||||
Ok(Project::new("", path))
|
Ok(Project::new("", path))
|
||||||
} else {
|
} else {
|
||||||
Self::load_from_json(&json)
|
Self::load_from_json(&json)
|
||||||
|
49
src/main.rs
49
src/main.rs
@ -6,29 +6,47 @@ use crossterm::{
|
|||||||
event::*,
|
event::*,
|
||||||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
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::backend::CrosstermBackend;
|
||||||
use tui::Terminal;
|
use tui::Terminal;
|
||||||
use clap::Parser;
|
use clap::{Parser, ValueHint::FilePath};
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
#[command(name = "kanban")]
|
#[command(name = "kanban")]
|
||||||
/// kanban-tui is a simple, interactive TUI based task manager using kanban columns
|
/// kanban-tui is a simple, interactive TUI based task manager using kanban columns
|
||||||
pub struct CliArgs {
|
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
|
/// Path to the
|
||||||
pub filepath: Option<PathBuf>
|
pub filepath: Option<PathBuf>
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prompt_project_init() -> (String, io::Result<File>) {
|
const DEFAULT_DATABASE_NAME: &str = "kanban.json";
|
||||||
let _msg = "Database not found, would you like to start a new project?";
|
|
||||||
let filepath = "kanban.json";
|
fn prompt_project_init(default_name: &str) -> (String, io::Result<File>) {
|
||||||
(filepath.to_string(),
|
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()
|
OpenOptions::new()
|
||||||
.write(true)
|
.write(true)
|
||||||
.read(true)
|
.read(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.open(filepath))
|
.open(filename))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> anyhow::Result<(), Box<dyn Error>> {
|
fn main() -> anyhow::Result<(), Box<dyn Error>> {
|
||||||
@ -39,21 +57,28 @@ fn main() -> anyhow::Result<(), Box<dyn Error>> {
|
|||||||
let file = OpenOptions::new()
|
let file = OpenOptions::new()
|
||||||
.write(true)
|
.write(true)
|
||||||
.read(true)
|
.read(true)
|
||||||
.create(true)
|
|
||||||
.open(&fpath);
|
.open(&fpath);
|
||||||
|
|
||||||
match file {
|
match file {
|
||||||
Ok(f) => (fpath, f),
|
Ok(f) => (fpath, f),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
let (fp, fname) = prompt_project_init();
|
let (fp, fname) = prompt_project_init(&fpath);
|
||||||
(fp, fname.unwrap())
|
(fp, fname.unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
CliArgs { filepath: None } => {
|
CliArgs { filepath: None } => {
|
||||||
let (fp, fname) = prompt_project_init();
|
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())
|
(fp, fname.unwrap())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
let mut state = AppState::new(Project::load(filepath, &file)?);
|
let mut state = AppState::new(Project::load(filepath, &file)?);
|
||||||
|
|
||||||
@ -68,7 +93,7 @@ fn main() -> anyhow::Result<(), Box<dyn Error>> {
|
|||||||
kanban_tui::handle_input(&mut state)?;
|
kanban_tui::handle_input(&mut state)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
state.project.save();
|
// state.project.save();
|
||||||
|
|
||||||
// restore terminal
|
// restore terminal
|
||||||
disable_raw_mode()?;
|
disable_raw_mode()?;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user