git text editor opening bug fixed; display functions for individual
tasks and lists
This commit is contained in:
@ -25,7 +25,8 @@ pub fn file(text : &str) -> colored::ColoredString {
|
||||
text.truecolor(255, 184, 184).bold()
|
||||
}
|
||||
|
||||
// Pink
|
||||
// Blue
|
||||
pub fn id(text : &str) -> colored::ColoredString {
|
||||
text.truecolor(232, 67, 147).bold()
|
||||
text.truecolor(52, 152, 219)
|
||||
}
|
||||
|
||||
|
@ -64,12 +64,12 @@ impl Config {
|
||||
}
|
||||
|
||||
/// Lists all vaults to stdout.
|
||||
pub fn list_vaults(&self) {
|
||||
pub fn list_vaults(&self) -> Result<(), error::Error> {
|
||||
|
||||
let width = self.vaults.iter().fold(usize::MIN, |c, (n, _)| c.max(n.len()));
|
||||
|
||||
if self.vaults.is_empty() {
|
||||
println!("No vaults currently set up, try running: {}", colour::command("toru vault new <NAME> <PATH>"));
|
||||
Err(error::Error::Generic(format!("No vaults currently set up, try running: {}", colour::command("toru vault new <NAME> <PATH>"))))
|
||||
}
|
||||
else {
|
||||
for (i, (name, path)) in self.vaults.iter().enumerate() {
|
||||
@ -93,6 +93,8 @@ impl Config {
|
||||
|
||||
println!();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ pub enum Error {
|
||||
TomlDe(toml::de::Error),
|
||||
TomlSer(toml::ser::Error),
|
||||
Utf8(str::Utf8Error),
|
||||
Fmt(fmt::Error),
|
||||
Generic(String),
|
||||
}
|
||||
|
||||
@ -24,6 +25,7 @@ impl fmt::Display for Error {
|
||||
Error::TomlDe(err) => write!(f, "{} {}", colour::error("Internal Error:"), err),
|
||||
Error::TomlSer(err) => write!(f, "{} {}", colour::error("Internal Error:"), err),
|
||||
Error::Utf8(err) => write!(f, "{} {}", colour::error("Internal Error:"), err),
|
||||
Error::Fmt(err) => write!(f, "{} {}", colour::error("Internal Error:"), err),
|
||||
Error::Generic(message) => write!(f, "{}", message),
|
||||
}
|
||||
}
|
||||
@ -64,3 +66,10 @@ impl From<str::Utf8Error> for Error {
|
||||
Error::Utf8(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<fmt::Error> for Error {
|
||||
fn from(err : fmt::Error) -> Self {
|
||||
Error::Fmt(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
16
src/git.rs
16
src/git.rs
@ -1,26 +1,22 @@
|
||||
use crate::error;
|
||||
|
||||
use std::io;
|
||||
use std::str;
|
||||
use std::path;
|
||||
use std::process;
|
||||
use std::io::Write;
|
||||
|
||||
pub fn run_command(args : Vec<String>, vault_folder : &path::Path) -> Result<(), error::Error> {
|
||||
|
||||
let mut command = process::Command::new("git");
|
||||
|
||||
command
|
||||
let mut child = command
|
||||
.current_dir(vault_folder)
|
||||
// Force colour output even though run from other process.
|
||||
.args(["-c", "color.ui=always"])
|
||||
.args(args);
|
||||
.args(args)
|
||||
.spawn()?;
|
||||
|
||||
let output = command.output()?;
|
||||
let output_string = str::from_utf8(&output.stdout)?;
|
||||
|
||||
print!("{}", output_string);
|
||||
io::stdout().flush()?;
|
||||
// No point handling the potential error code as Git will report the error directly with more
|
||||
// info.
|
||||
let _ = child.wait()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
21
src/main.rs
21
src/main.rs
@ -32,6 +32,10 @@ enum Command {
|
||||
#[clap(short, long, value_enum)]
|
||||
priority : Option<tasks::Priority>,
|
||||
},
|
||||
/// Displays the specified task in detail.
|
||||
View {
|
||||
id : tasks::Id,
|
||||
},
|
||||
/// Delete a task completely.
|
||||
Delete {
|
||||
id : tasks::Id,
|
||||
@ -49,6 +53,14 @@ enum Command {
|
||||
Git {
|
||||
args : Vec<String>,
|
||||
},
|
||||
/// Lists tasks according to the specified ordering and filters.
|
||||
List {
|
||||
// Need to have options for:
|
||||
// - column to order by
|
||||
// - ascending or descending
|
||||
// - which columns to include
|
||||
// - filters which exclude values
|
||||
},
|
||||
/// Commands for interacting with vaults.
|
||||
#[clap(subcommand)]
|
||||
Vault(VaultCommand),
|
||||
@ -124,7 +136,7 @@ fn program() -> Result<(), error::Error> {
|
||||
println!("Deleted vault {}", colour::vault(&name));
|
||||
},
|
||||
List => {
|
||||
config.list_vaults();
|
||||
config.list_vaults()?;
|
||||
},
|
||||
Switch { name } => {
|
||||
config.switch(&name)?;
|
||||
@ -144,6 +156,10 @@ fn program() -> Result<(), error::Error> {
|
||||
Delete { id } => {
|
||||
tasks::Task::delete_by_id(id, vault_folder)?;
|
||||
println!("Deleted task {}", colour::id(&id.to_string()));
|
||||
},
|
||||
View { id } => {
|
||||
let task = tasks::Task::load(id, vault_folder.clone(), true)?;
|
||||
task.display()?;
|
||||
}
|
||||
Discard { id } => {
|
||||
let mut task = tasks::Task::load(id, vault_folder.clone(), false)?;
|
||||
@ -160,6 +176,9 @@ fn program() -> Result<(), error::Error> {
|
||||
Git { args } => {
|
||||
git::run_command(args, vault_folder)?;
|
||||
},
|
||||
List {} => {
|
||||
tasks::list(vault_folder)?;
|
||||
}
|
||||
Vault(_) => unreachable!(),
|
||||
}
|
||||
|
||||
|
112
src/tasks.rs
112
src/tasks.rs
@ -2,12 +2,14 @@ use crate::error;
|
||||
use crate::state;
|
||||
use crate::colour;
|
||||
|
||||
use std::io;
|
||||
use std::fs;
|
||||
use std::fmt;
|
||||
use std::mem;
|
||||
use std::path;
|
||||
use std::io;
|
||||
use std::io::{Write, Seek};
|
||||
use std::collections::HashSet;
|
||||
use colored::Colorize;
|
||||
|
||||
pub type Id = u64;
|
||||
|
||||
@ -20,19 +22,42 @@ pub struct Task {
|
||||
#[derive(Default, Debug, Clone, clap::ValueEnum, serde::Serialize, serde::Deserialize)]
|
||||
pub enum Priority {
|
||||
#[default]
|
||||
Unspecified,
|
||||
Low,
|
||||
Medium,
|
||||
High,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
impl fmt::Display for Priority {
|
||||
fn fmt(&self, f : &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
use Priority::*;
|
||||
let priority = match self {
|
||||
Low => "low",
|
||||
Medium => "medium",
|
||||
High => "high",
|
||||
};
|
||||
write!(f, "{}", priority)
|
||||
}
|
||||
}
|
||||
|
||||
impl Priority {
|
||||
pub fn coloured(&self) -> String {
|
||||
use Priority::*;
|
||||
let priority = match self {
|
||||
Low => "low".truecolor(46, 204, 113),
|
||||
Medium => "medium".truecolor(241, 196, 15),
|
||||
High => "high".truecolor(231, 76, 60),
|
||||
};
|
||||
format!("{}", priority)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct TimeEntry {
|
||||
hours : u32,
|
||||
minutes : u8,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct InternalTask {
|
||||
pub id : Id,
|
||||
pub name : String,
|
||||
@ -152,6 +177,85 @@ impl Task {
|
||||
fs::remove_file(&path)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn display(&self) -> Result<(), error::Error> {
|
||||
|
||||
fn line(len : usize) {
|
||||
for _ in 0..len {
|
||||
print!("-");
|
||||
}
|
||||
println!();
|
||||
}
|
||||
|
||||
let id = &self.data.id.to_string();
|
||||
let heading = format!("[{}] {} {}", if self.data.complete {"X"} else {" "}, colour::id(&id), colour::task_name(&self.data.name));
|
||||
println!("{}", heading);
|
||||
line(5 + self.data.name.len() + id.len());
|
||||
println!("Priority: {}", self.data.priority.coloured());
|
||||
println!("Tags: [{}]", format_hash_set(&self.data.tags)?);
|
||||
println!("Created: {}", self.data.created);
|
||||
|
||||
if let Some(info) = &self.data.info {
|
||||
println!("Info:");
|
||||
// Figure out how to indent this properly:
|
||||
println!("\t{}", info);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
// dependencies as a tree
|
||||
}
|
||||
}
|
||||
|
||||
fn format_hash_set<T : fmt::Display>(set : &HashSet<T>) -> Result<String, error::Error> {
|
||||
let mut output = String::new();
|
||||
|
||||
for value in set.iter() {
|
||||
fmt::write(&mut output, format_args!("{}, ", value))?;
|
||||
}
|
||||
|
||||
if output.len() != 0 {
|
||||
output.pop();
|
||||
output.pop();
|
||||
}
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
pub fn list(vault_folder : &path::Path) -> Result<(), error::Error> {
|
||||
let ids : Vec<Id> =
|
||||
fs::read_dir(vault_folder.join("notes"))
|
||||
.unwrap()
|
||||
.map(|entry| entry.unwrap().path())
|
||||
.filter(|p| p.is_file())
|
||||
.map(|p| p.file_stem().unwrap().to_str().unwrap().to_string())
|
||||
.filter_map(|n| n.parse::<Id>().ok())
|
||||
.collect();
|
||||
|
||||
let mut table = comfy_table::Table::new();
|
||||
|
||||
table
|
||||
.load_preset(comfy_table::presets::UTF8_FULL)
|
||||
.apply_modifier(comfy_table::modifiers::UTF8_ROUND_CORNERS);
|
||||
|
||||
table.set_header(vec!["Id", "Name", "Tags", "Priority"]);
|
||||
|
||||
for id in ids {
|
||||
let task = Task::load(id, vault_folder.to_path_buf(), true)?;
|
||||
|
||||
table.add_row(
|
||||
vec![
|
||||
task.data.id.to_string(),
|
||||
task.data.name,
|
||||
format_hash_set(&task.data.tags)?,
|
||||
task.data.priority.to_string()
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
println!("{}", table);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user