Skip to content
Merged
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
38 changes: 38 additions & 0 deletions .github/workflows/hooky.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Verify linting

on:
push:
branches: [main]
pull_request:
types: [opened, synchronize, reopened]

jobs:
hooky:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4
- uses: moonrepo/setup-rust@v1
- name: Get latest Hooky release tag
id: get_tag
run: |
TAG=$(curl -sSf https://api.github.com/repos/brandonchinn178/hooky/releases/latest \
| jq -r .tag_name)
echo "Latest Hooky tag is $TAG"
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
- name: Download Hooky binary
id: download
run: |
TAG="${{ steps.get_tag.outputs.tag }}"
ASSET="hooky-${TAG#v}-linux-x86_64"
URL="https://github.com/brandonchinn178/hooky/releases/download/${TAG}/${ASSET}"
echo "Downloading $URL"
sudo curl -L -o /usr/bin/hooky "${URL}"
sudo chmod +x /usr/bin/hooky
- name: Verify dependency versions
run: |
hooky --version
rustfmt --version
- name: Execute hooks
run: hooky run --all --format=verbose
8 changes: 8 additions & 0 deletions .hooky.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ hook hooky {
files *
}

hook cargo_fmt {
command cargo fmt {
check_args --check
pass_files none
}
files *.rs
}

lint_rules {
- check_broken_symlinks
- check_case_conflict
Expand Down
78 changes: 48 additions & 30 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::constants::{BIN, BIN_VERSION};
use crate::models::NewTodo;

use colored::Colorize;
use chrono::{DateTime, Utc, Local};
use chrono::{DateTime, Local, Utc};
use clap::{Parser, Subcommand};
use colored::Colorize;

#[derive(Parser)]
#[command(
Expand Down Expand Up @@ -106,7 +106,11 @@ pub fn run_cli() {
Commands::Add { title, complete } => {
add_todo(title, complete);
}
Commands::List { all, completed, open: _ } => {
Commands::List {
all,
completed,
open: _,
} => {
// Priority: --all > --completed > default (--open)
if all {
// Show all TODOs
Expand Down Expand Up @@ -142,31 +146,25 @@ fn format_datetime(ts: DateTime<Utc>, precise: bool) -> String {
if !precise {
let ht = chrono_humanize::HumanTime::from(ts);
return format!("{}", ht);

}
format!("{}", local_tz.format("%d/%m/%Y %H:%M"))
}

fn format_datetime_or_else(ts: Option<DateTime<Utc>>, else_item: String, precise: bool) -> String {
match ts {
Some(ts) => {
format_datetime(ts, precise)
}
None => {
else_item
}
Some(ts) => format_datetime(ts, precise),
None => else_item,
}
}

fn show_todo(id: &String) {
let found_todo = crate::get_todo(id);
let created_str: String = format_datetime(found_todo.created, false);
let completed_str: String = format_datetime_or_else(found_todo.completed, "not yet".to_string(), false);
println!("{}\n{}\nIt was created: {}\nIt was completed: {}",
found_todo.title,
found_todo.notes,
created_str,
completed_str,
let completed_str: String =
format_datetime_or_else(found_todo.completed, "not yet".to_string(), false);
println!(
"{}\n{}\nIt was created: {}\nIt was completed: {}",
found_todo.title, found_todo.notes, created_str, completed_str,
);
}

Expand All @@ -186,12 +184,22 @@ pub fn edit_todo(id: String) {

fn complete_todo(id: &String) {
crate::complete_todo(id, None);
println!("{} completed, if this was a mistake reopen with `{} reopen {}`", id.yellow(), BIN, id)
println!(
"{} completed, if this was a mistake reopen with `{} reopen {}`",
id.yellow(),
BIN,
id
)
}

fn reopen_todo(id: &String) {
crate::reopen_todo(id);
println!("{} reopened, if this was a mistake complete with `{} complete {}`", id.yellow(), BIN, id)
println!(
"{} reopened, if this was a mistake complete with `{} complete {}`",
id.yellow(),
BIN,
id
)
}

pub fn delete_todo(id: &String) {
Expand All @@ -208,8 +216,11 @@ pub fn add_todo(title: Option<String>, complete_after_creation: bool) {
};
let p_buff = crate::get_todoeditmsg_file();
let fp = p_buff.as_path();
let (title, notes) =
crate::create_temp_todo_file_open_and_then_read_remove_process(fp, title_str, String::new());
let (title, notes) = crate::create_temp_todo_file_open_and_then_read_remove_process(
fp,
title_str,
String::new(),
);
let new_todo = NewTodo {
title: title.as_str(),
notes: notes.as_str(),
Expand All @@ -221,14 +232,15 @@ pub fn add_todo(title: Option<String>, complete_after_creation: bool) {
&crate::encode_id(created_todo.id.try_into().unwrap()),
Some(created_todo.created),
);

}
println!(
"{} created{}",
crate::encode_id(created_todo.id.try_into().unwrap()).yellow(),
if complete_after_creation {
" and was subsequently completed"
} else {""}
} else {
""
}
);
}

Expand Down Expand Up @@ -257,7 +269,10 @@ pub fn list_todos(show_completed: Option<bool>) {
results.sort_by(|a, b| b.id.cmp(&a.id));

if results.is_empty() {
println!("There's nothing to do currently :) Add a new one with `{} add`", BIN);
println!(
"There's nothing to do currently :) Add a new one with `{} add`",
BIN
);
} else {
let mut table = comfy_table::Table::new();
table.load_preset(comfy_table::presets::NOTHING);
Expand All @@ -268,17 +283,20 @@ pub fn list_todos(show_completed: Option<bool>) {
// With custom_styling comfy_table flag we can keep using colorize colors, but
// slow down comfy table by 30-50%. I think this is acceptable for now, but
// could later switch to using comfy_table's built-in coloring.
crate::encode_id(post.id.try_into().expect("Failed to cast post id in list")).yellow().to_string(),
),
comfy_table::Cell::new(
format_datetime(post.created, false),
crate::encode_id(post.id.try_into().expect("Failed to cast post id in list"))
.yellow()
.to_string(),
),
comfy_table::Cell::new(format_datetime(post.created, false)),
comfy_table::Cell::new(post.title),
]);
}
table.column_mut(2).unwrap().set_constraint(
comfy_table::ColumnConstraint::UpperBoundary(comfy_table::Width::Percentage(60))
);
table
.column_mut(2)
.unwrap()
.set_constraint(comfy_table::ColumnConstraint::UpperBoundary(
comfy_table::Width::Percentage(60),
));
println!("{table}")
}
}
28 changes: 20 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ fn create_sqids_encoder_with_custom_alphabet() -> Sqids {
}

pub fn encode_id(i: u64) -> String {
create_sqids_encoder_with_custom_alphabet().encode(&vec![i]).expect("Problem encoding id")
create_sqids_encoder_with_custom_alphabet()
.encode(&vec![i])
.expect("Problem encoding id")
}

pub fn decode_id(s: &str) -> i32 {
// TODO can I make this nicer?
(*create_sqids_encoder_with_custom_alphabet().decode(s).first().expect("Couldn't decode id"))
.try_into()
.unwrap()
(*create_sqids_encoder_with_custom_alphabet()
.decode(s)
.first()
.expect("Couldn't decode id"))
.try_into()
.unwrap()
}

// Path-related functions
Expand Down Expand Up @@ -158,7 +163,8 @@ pub fn get_todo(get_id: &String) -> Todos {
use self::schema::todos::dsl::*;
let connection = &mut establish_connection();
let decoded_id = decode_id(get_id);
todos.select(Todos::as_select())
todos
.select(Todos::as_select())
.filter(id.eq(decoded_id))
.first(connection)
.unwrap_or_else(|_| panic!("Single TODO couldn't be found with id {}", get_id))
Expand All @@ -167,7 +173,8 @@ pub fn get_todo(get_id: &String) -> Todos {
pub fn get_todos() -> Vec<Todos> {
use self::schema::todos::dsl::*;
let connection = &mut establish_connection();
todos.select(Todos::as_select())
todos
.select(Todos::as_select())
.load(connection)
.expect("Was unable to get all TODOs")
}
Expand Down Expand Up @@ -200,7 +207,7 @@ pub fn set_todo_notes(update_id: &String, new_notes: &String) {
diesel::update(todos.find(decoded_id))
.set(notes.eq(new_notes))
.execute(connection)
.unwrap_or_else( |_| panic!("notes of TODO: {} couldn't be updated", update_id));
.unwrap_or_else(|_| panic!("notes of TODO: {} couldn't be updated", update_id));
}

pub fn reopen_todo(show_id: &String) {
Expand All @@ -212,7 +219,12 @@ pub fn reopen_todo(show_id: &String) {
.set(completed.eq(None::<DateTime<Utc>>))
.execute(connection)
.expect("TODO couldn't be reopened");
println!("{} reopened, if this was a mistake complete with `{} complete {}`", show_id.yellow(), BIN, show_id)
println!(
"{} reopened, if this was a mistake complete with `{} complete {}`",
show_id.yellow(),
BIN,
show_id
)
}

pub fn delete_todo(delete_id: &String) {
Expand Down
Loading