Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tui #30

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

Tui #30

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
682 changes: 571 additions & 111 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ dialoguer = "0.8.0"
console = "0.14.1"
sled = "0.34.6"
bincode = "1.3.3"
prettytable-rs = "0.8.0"
prettytable-rs = "0.10.0"
ratatui = "0.26.0"
crossterm = "0.27.0"

[dependencies.serde]
version = "1.0.64"
Expand Down
7 changes: 4 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use std::env;
use std::io::Write;

fn write_link_info_type(file: &mut std::fs::File) -> Result<(), std::io::Error> {
let cid = env::var("GOOGLE_CLIENT_ID").unwrap();
let cls = env::var("GOOGLE_CLIENT_SECRET").unwrap();
let cid = env::var("GOOGLE_CLIENT_ID").expect("env var GOOGLE_CLIENT_ID should be set but was");
let cls = env::var("GOOGLE_CLIENT_SECRET")
.expect("env var GOOGLE_CLIENT_SECRET should be set but was");
let data = format!(
"pub struct Secrets {{
pub client_id: String,
Expand All @@ -25,7 +26,7 @@ impl Secrets {{
}

fn generate_module() -> Result<(), std::io::Error> {
let mut module = std::fs::File::create(&format!("src/{}.rs", "secrets"))?;
let mut module = std::fs::File::create(format!("src/{}.rs", "secrets"))?;
write_link_info_type(&mut module)?;
Ok(())
}
Expand Down
4 changes: 3 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub enum TaskAction {

/// Set the tasks status to completed.
#[structopt(short, long)]
completed: bool
completed: bool,
},
/// Clear all completed tasks in a task-list.
Clear,
Expand Down Expand Up @@ -108,4 +108,6 @@ pub enum Commands {
},
/// Helps you set-up battery for p10k
Battery,
/// Opens a terminal user interface
Tui,
}
36 changes: 27 additions & 9 deletions src/handlers/task_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ impl TaskManager {
Ok(())
}

pub fn add_task(&self, title: Option<String>, notes: Option<String>, completed: bool) -> anyhow::Result<()> {
pub fn add_task(
&self,
title: Option<String>,
notes: Option<String>,
completed: bool,
) -> anyhow::Result<()> {
let task = match title {
Some(t) => self.create_task_without_prompts(t, notes, completed),
None => self.create_task_with_prompts(notes, completed)?
None => self.create_task_with_prompts(notes, completed)?,
};
let resp = &self.client.add_task(task);
match resp {
Expand All @@ -41,31 +46,44 @@ impl TaskManager {
Ok(())
}

fn create_task_without_prompts(&self, title: String, notes: Option<String>, completed: bool) -> Tasks {
fn create_task_without_prompts(
&self,
title: String,
notes: Option<String>,
completed: bool,
) -> Tasks {
let status = if completed {
String::from("completed")
} else {
String::from("needsAction")
};
Tasks::new(None, title, notes.unwrap_or_else(||String::from("")), status)
Tasks::new(
None,
title,
notes.unwrap_or_else(|| String::from("")),
status,
)
}

fn create_task_with_prompts (&self, notes: Option<String>, done: bool) -> anyhow::Result<Tasks> {
fn create_task_with_prompts(&self, notes: Option<String>, done: bool) -> anyhow::Result<Tasks> {
let title: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Title of the task")
.with_initial_text("task")
.allow_empty(false)
.interact_text()?;

let notes: String = notes.map(Result::Ok).unwrap_or_else(|| Input::with_theme(&ColorfulTheme::default())

let notes: String = notes.map(Result::Ok).unwrap_or_else(|| {
Input::with_theme(&ColorfulTheme::default())
.with_prompt("Note for task")
.with_initial_text("note")
.allow_empty(true)
.interact_text()
)?;
})?;

let items = vec!["No", "Yes"];
let completed = if done { 1_usize } else {
let completed = if done {
1_usize
} else {
Select::with_theme(&ColorfulTheme::default())
.with_prompt("Is the task completed?")
.items(&items)
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/tasklist_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl TaskListManager {
let resp = &self.client.fetch_tasklist(false);
match resp {
Ok(data) => Ok(data.items.clone()),
Err(_err) => Err(anyhow!("Cannot fetch tasklists!")),
Err(err) => Err(anyhow!("Cannot fetch tasklists! {}", err)),
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod oauth;
mod printer;
mod secrets;
mod service;
mod tui;
#[macro_use]
extern crate prettytable;

Expand All @@ -30,7 +31,11 @@ fn main() -> anyhow::Result<()> {
}
Delete { position } => generate_task_manager(tasks_database).delete_task(position)?,
Show { position } => generate_task_manager(tasks_database).show_task(position)?,
Add { title, notes, completed } => generate_task_manager(tasks_database).add_task(title, notes, completed)?,
Add {
title,
notes,
completed,
} => generate_task_manager(tasks_database).add_task(title, notes, completed)?,
Clear => generate_task_manager(tasks_database).clear_tasks()?,
Undo { position } => {
generate_task_manager(tasks_database).complete_task(position, false)?
Expand Down Expand Up @@ -60,6 +65,7 @@ fn main() -> anyhow::Result<()> {
Logout => oauth::logout(&tasks_database)?,
},
Battery => MiscManager.help_p10k_script_generation()?,
Tui => tui::open(tasks_database)?,
}
Ok(())
}
Expand Down
29 changes: 25 additions & 4 deletions src/models/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub struct Tasks {
pub status: String,
#[serde(default)]
pub due: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub parent: Option<String>,
}

impl Tasks {
Expand All @@ -46,6 +48,7 @@ impl Tasks {
notes,
status,
due: String::from(""),
parent: None,
}
}

Expand All @@ -61,10 +64,11 @@ impl Tasks {
notes: String::from(&self.notes),
status: String::from(&self.status),
due: String::from(&self.due),
parent: self.parent.clone(),
}
}

pub fn get_sanitised_data(&self) -> (String, String, String, String) {
pub fn get_sanitised_data(&self, tasks: &[Tasks]) -> (String, String, String, String, String) {
let status = if self.status == "needsAction" {
String::from("Incomplete")
} else {
Expand All @@ -82,7 +86,22 @@ impl Tasks {
} else {
String::from(&self.notes)
};
(String::from(&self.title), status, notes, due)
let parent = self.parent.clone().unwrap_or("No parent".into());

println!("{self:?}");

let parent = tasks
.iter()
.find(|t| t.id == Some(parent.clone()))
.map(|t| t.title.clone())
.unwrap_or("Parent not found!".to_string());
(
String::from(&self.title),
status,
notes,
due,
parent.to_string(),
)
}
}

Expand All @@ -103,13 +122,15 @@ impl fmt::Display for Tasks {
} else {
String::from(&self.notes)
};
let parent = self.parent.clone().unwrap_or("No parent".into());
write!(
f,
"{0: <10} | {1: <10} | {2: <10} | {3: <10}",
"{0: <10} | {1: <10} | {2: <10} | {3: <10} | {4: <10}",
style(&self.title).for_stdout().green(),
notes,
status,
due
due,
parent,
)
}
}
Loading