Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
v2.2.0
Browse files Browse the repository at this point in the history
finished cmd swap-project-records
changed day to full date in reports
changed summary line format in reports
fixed record minute being hour in reports
updated manpage
updated bash-completions
  • Loading branch information
SchokiCoder committed Aug 4, 2022
1 parent 55fe069 commit a9222b3
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "smng"
version = "2.1.2"
version = "2.2.0"
authors = ["SchokiCoder <afschoknecht@gmail.com>"]
repository = "https://github.com/SchokiCoder/smng"
edition = "2018"
Expand Down
2 changes: 1 addition & 1 deletion completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

_smng_completions()
{
COMPREPLY+=($(compgen -W "help about add-project show-projects edit-project delete-project record status stop add-record edit-record-project edit-record-begin edit-record-end edit-record-description delete-record transfer-project-records show-week show-month merge-db" "${COMP_WORDS[1]}"))
COMPREPLY+=($(compgen -W "help about add-project show-projects edit-project delete-project record status stop add-record edit-record-project edit-record-begin edit-record-end edit-record-description delete-record transfer-project-records swap-project-records show-week show-month show-project-records merge-db show-config-path show-db-path" "${COMP_WORDS[1]}"))
}

complete -F _smng_completions smng
28 changes: 26 additions & 2 deletions manpage.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH "SMNG" "1" "29 July 2022" "smng 2.1.2" ""
.TH "SMNG" "1" "04 August 2022" "smng 2.2.0" ""
.hy

.SH NAME
Expand Down Expand Up @@ -118,6 +118,12 @@ Delete given record.
Transfer all records from one project to another.
.RE

.PP
\f[B]swap-project-records\f[R] \f[I]project_id_a\f[R] \f[I]project_id_b\f[R]
.RS 4
Swap records of two projects.
.RE

.SS Report Commands (show records)

.PP
Expand All @@ -132,14 +138,32 @@ Show records of a given week or if none given current week.
Show records of a given month or if none given current month.
.RE

.SS Administration Commands (handling databases)
.PP
\f[B]show-project-records\f[R] \f[I]project_id\f[R]
.RS 4
Show records of a certain project.
.RE

.SS Administration Commands (handling configurations and databases)

.PP
\f[B]merge-db | mdb\[R] \f[I]source_database_path\f[R] \f[I]destination_database_path\f[R]
.RS 4
Merge projects and records of two databases.
.RE

.PP
\f[B]show-cfg-path | scp\f[R]
.RS 4
Show path of currently used config.
.RE

.PP
\f[B]show-db-path | sdbp\f[R]
.RS 4
Show path of currently configured database.
.RE

.SH FILES
.PP
.I
Expand Down
141 changes: 114 additions & 27 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ pub const EDIT_PROJECT_NAME: &str = "edit-project";
pub const EDIT_PROJECT_ABBR: &str = "ep";
pub const EDIT_PROJECT_ARGS: &str = "project_id project_name";

pub const SWAP_PROJECTS_INFO: &str = "swap projects and the respective records";
pub const SWAP_PROJECTS_NAME: &str = "swap-projects";
pub const SWAP_PROJECTS_ARGS: &str = "project_id_a project_id_b";

pub const DELETE_PROJECT_INFO: &str = "delete a project and if wished purge all records";
pub const DELETE_PROJECT_NAME: &str = "delete-project";
pub const DELETE_PROJECT_ARGS: &str = "project_id [purge]";
Expand Down Expand Up @@ -94,6 +90,10 @@ pub const TRANSFER_PROJECT_RECORDS_INFO: &str = "transfer all records from one p
pub const TRANSFER_PROJECT_RECORDS_NAME: &str = "transfer-project-records";
pub const TRANSFER_PROJECT_RECORDS_ARGS: &str = "source_project_id destination_project_id";

pub const SWAP_PROJECT_RECORDS_INFO: &str = "swap records of two projects";
pub const SWAP_PROJECT_RECORDS_NAME: &str = "swap-project-records";
pub const SWAP_PROJECT_RECORDS_ARGS: &str = "project_id_a project_id_b";

pub const SHOW_WEEK_INFO: &str = "show records of a certain week or current";
pub const SHOW_WEEK_NAME: &str = "show-week";
pub const SHOW_WEEK_ABBR: &str = "sw";
Expand Down Expand Up @@ -394,6 +394,11 @@ pub fn help() {
TRANSFER_PROJECT_RECORDS_NAME,
None,
Some(TRANSFER_PROJECT_RECORDS_ARGS));
print_cmd_help(
SWAP_PROJECT_RECORDS_INFO,
SWAP_PROJECT_RECORDS_NAME,
None,
Some(SWAP_PROJECT_RECORDS_ARGS));

println!("-- Report --");
print_cmd_help(
Expand Down Expand Up @@ -491,18 +496,6 @@ pub fn edit_project(project_id: i64, project_name: &str) {
println!("Project ({}) name set to \"{}\".", project_id, project_name);
}

pub fn swap_projects(project_id_a: i64, project_id_b: i64) {
// if project id's are equal, educate user and stop
if project_id_a == project_id_b {
println!(
"ERROR: This command swaps the projects and records given.\n\
A swap needs two different projects.");
return;
}


}

pub fn delete_project(project_id: i64, purge: bool) {
let db = database_open();

Expand Down Expand Up @@ -782,6 +775,88 @@ pub fn transfer_project_records(src_project_id: i64, dest_project_id: i64) {
src_project_id, dest_project_id);
}

pub fn swap_project_records(project_id_a: i64, project_id_b: i64) {
// if project id's are equal, educate user and stop
if project_id_a == project_id_b {
println!(
"ERROR: This command swaps the projects and records given.\n\
A swap needs two different projects.");
return;
}

// create temp project, get temp project id
let db = database_open();

db
.execute(
"INSERT INTO tbl_projects(project_name)\n\
VALUES('__swap');")
.unwrap();

let mut stmt = db
.prepare(
"SELECT project_id\n\
FROM tbl_projects\n\
WHERE project_name = '__swap';")
.unwrap();

stmt.next().unwrap();

let tempid = stmt.read::<i64>(0).unwrap();

// move a to temp
let mut stmt = db
.prepare(
"UPDATE tbl_work_records\n\
SET project_id = ?\n\
WHERE project_id = ?;")
.unwrap();

stmt.bind(1, tempid).unwrap();
stmt.bind(2, project_id_a).unwrap();

while stmt.next().unwrap() != sqlite::State::Done {}

// move b to a
let mut stmt = db
.prepare(
"UPDATE tbl_work_records\n\
SET project_id = ?\n\
WHERE project_id = ?;")
.unwrap();

stmt.bind(1, project_id_a).unwrap();
stmt.bind(2, project_id_b).unwrap();

while stmt.next().unwrap() != sqlite::State::Done {}

// move temp to b
let mut stmt = db
.prepare(
"UPDATE tbl_work_records\n\
SET project_id = ?\n\
WHERE project_id = ?;")
.unwrap();

stmt.bind(1, project_id_b).unwrap();
stmt.bind(2, tempid).unwrap();

while stmt.next().unwrap() != sqlite::State::Done {}

// delete temp project
let mut stmt = db
.prepare(
"DELETE FROM tbl_projects\n\
WHERE project_id = ?;")
.unwrap();

stmt.bind(1, tempid).unwrap();
stmt.next().unwrap();

println!("Records of project ({}) swapped with project ({}).",
project_id_a, project_id_b);
}

fn show_record(stmt: &sqlite::Statement, win_width: usize) -> i64 {
let seconds = stmt.read::<i64>(5).unwrap();
let minutes: u32 = seconds as u32 / 60;
Expand All @@ -796,7 +871,7 @@ fn show_record(stmt: &sqlite::Statement, win_width: usize) -> i64 {
stmt.read::<i64>(0).unwrap(),
stmt.read::<String>(2).unwrap(),
rec_end.unwrap(),
hours, (minutes / 60),
hours, (minutes % 60),
stmt.read::<i64>(6).unwrap());
}
else {
Expand Down Expand Up @@ -826,6 +901,12 @@ fn show_record(stmt: &sqlite::Statement, win_width: usize) -> i64 {
return seconds;
}

fn print_char(num: u32, c: char) {
for _ in 0..num {
print!("{}", c);
}
}

fn show_records(
ts_begin: Option<i64>,
ts_end: Option<i64>,
Expand All @@ -839,7 +920,7 @@ fn show_records(

let mut sql = String::from(
"SELECT work_record_id, \
strftime('%d', begin, 'unixepoch') as begin_day, \
strftime('%Y.%m.%d', begin, 'unixepoch') as begin_day, \
strftime('%H:%M', begin, 'unixepoch') as begin_time, \
strftime('%d', end, 'unixepoch') as end_day, \
strftime('%H:%M', end, 'unixepoch') as end_time, \
Expand Down Expand Up @@ -895,15 +976,15 @@ fn show_records(
"id", "begin", "end", "time", "project", "description");

let mut sum_seconds: u32 = 0;
let mut pre_day: i64;
let mut cur_day: i64 = 0;
let mut pre_day: String;
let mut cur_day = String::from("");
let mut day_seconds: u32 = 0;

// print first record
// (because the if (cur_day change) would needlessly print day_worktime)
if stmt.next().unwrap() == sqlite::State::Row {
cur_day = stmt.read::<i64>(1).unwrap();
println!("- day {:3} -", cur_day);
cur_day = stmt.read::<String>(1).unwrap();
println!("- {} -", cur_day);

let seconds = show_record(&stmt, win_width);

Expand All @@ -915,7 +996,7 @@ fn show_records(
while stmt.next().unwrap() == sqlite::State::Row {
// if current day changes
pre_day = cur_day;
cur_day = stmt.read::<i64>(1).unwrap();
cur_day = stmt.read::<String>(1).unwrap();

if cur_day != pre_day {

Expand All @@ -929,8 +1010,11 @@ fn show_records(
.format("%H:%M")
.to_string();

println!("{:32}- {:5} -", "", worktime);
println!("- day {:3} -", cur_day);
print_char(33, '-');
print!(" {:5} ", worktime);
print_char(25, '-');
println!("");
println!("- {} -", cur_day);

// reset day worktime
day_seconds = 0;
Expand All @@ -951,8 +1035,11 @@ fn show_records(
.and_hms(hours, minutes - hours * 60, 0)
.format("%H:%M")
.to_string();

println!("{:32}- {:5} -", "", worktime);

print_char(33, '-');
print!(" {:5} ", worktime);
print_char(25, '-');
println!("");

// summarized worktime
let minutes: u32 = sum_seconds / 60;
Expand Down
40 changes: 20 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,6 @@ fn main() {

commands::edit_project(project_id, args[3].as_str());
},

commands::SWAP_PROJECTS_NAME => {
if args.len() == 2 {
commands::print_cmd_help(
commands::SWAP_PROJECTS_INFO,
commands::SWAP_PROJECTS_NAME,
None,
Some(commands::SWAP_PROJECTS_ARGS));
return;
}

if argcount_check(args.len(), 4, 4) {
return;
}

let project_id_a = args[2].parse::<i64>().unwrap();
let project_id_b = args[3].parse::<i64>().unwrap();

commands::swap_projects(project_id_a, project_id_b);
},

commands::DELETE_PROJECT_NAME => {
if args.len() == 2 {
Expand Down Expand Up @@ -349,6 +329,26 @@ fn main() {
commands::transfer_project_records(src_prj_id, dest_prj_id);
},

commands::SWAP_PROJECT_RECORDS_NAME => {
if args.len() == 2 {
commands::print_cmd_help(
commands::SWAP_PROJECT_RECORDS_INFO,
commands::SWAP_PROJECT_RECORDS_NAME,
None,
Some(commands::SWAP_PROJECT_RECORDS_ARGS));
return;
}

if argcount_check(args.len(), 4, 4) {
return;
}

let project_id_a = args[2].parse::<i64>().unwrap();
let project_id_b = args[3].parse::<i64>().unwrap();

commands::swap_project_records(project_id_a, project_id_b);
},

commands::SHOW_WEEK_NAME | commands::SHOW_WEEK_ABBR => {
if argcount_check(args.len(), 2, 5) {
return;
Expand Down
24 changes: 12 additions & 12 deletions todo.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
--- done ---

{
-formatting v_12-46

Expand All @@ -24,24 +26,22 @@
-unnecessary conversion in v_14-22
show record

-cmds .
-cmds v_14-12_04-08-2022
-show-project-records v_14-24
-swap-projects .
(with records)
-swap-project-record v_14-12_04-08-2022

-report .
-day value in needs full .
date
-full line for new day .
-report v_14-33
-day value needs full date v_14-25
-full line for new day v_14-29
-fix record minute same as v_14-33
hour

-update manpage .
-update manpage v_14-56
(including version)
-update completions .
-v . -> 2.2.0
-update completions v_14-57
-v v_14-58 -> 2.2.0
}

--- done ---

{
-better man page v_09-33_13-07-2022
}
Expand Down

0 comments on commit a9222b3

Please sign in to comment.