Skip to content

Commit 763abd3

Browse files
committed
added option to check status
1 parent 31ad6aa commit 763abd3

File tree

6 files changed

+90
-6
lines changed

6 files changed

+90
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "abhyas"
3-
version = "1.1.0"
3+
version = "1.2.0"
44
edition = "2021"
55
authors = ["Harshal Jadhav <bestt1315@gmail.com>"]
66
homepage = "https://github.com/halshar/abhyas"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Abhyas is a Rust command-line application for managing and interacting with a da
44

55
### Features
66

7+
- **Check Status**: Get the total, completed, and skipped links count.
78
- **Get Link**: Get random link from the database.
89
- **Add Link**: Add new links to the database.
910
- **Search Link**: Search link from the database.

src/cli.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use crate::print::pretty_print;
1+
use crate::print::{pretty_print, pretty_status};
22
use crate::utility::show_red;
33
use crate::CustomErrors;
44
use crate::{database::Db, utility::show_green};
55
use inquire::{required, validator::Validation, Select, Text};
66

77
enum MainMenuOptions {
8+
Status,
89
GetLink,
910
AddLink,
1011
SearchLink,
@@ -30,7 +31,14 @@ enum OtherOptions {
3031
}
3132

3233
pub fn show_options(db: &Db) -> Result<(), CustomErrors> {
33-
let options = vec!["Get Link", "Add Link", "Search Link", "Other", "Exit"];
34+
let options = vec![
35+
"Check Status",
36+
"Get Link",
37+
"Add Link",
38+
"Search Link",
39+
"Other",
40+
"Exit",
41+
];
3442

3543
let user_option = match Select::new("select your option", options).prompt() {
3644
Ok(val) => val,
@@ -50,6 +58,7 @@ pub fn show_options(db: &Db) -> Result<(), CustomErrors> {
5058
};
5159

5260
let selected_item = match user_option {
61+
"Check Status" => MainMenuOptions::Status,
5362
"Get Link" => MainMenuOptions::GetLink,
5463
"Add Link" => MainMenuOptions::AddLink,
5564
"Search Link" => MainMenuOptions::SearchLink,
@@ -59,6 +68,7 @@ pub fn show_options(db: &Db) -> Result<(), CustomErrors> {
5968
};
6069

6170
match selected_item {
71+
MainMenuOptions::Status => get_status(&db)?,
6272
MainMenuOptions::GetLink => get_link_options(&db)?,
6373
MainMenuOptions::AddLink => add_link_options(&db)?,
6474
MainMenuOptions::SearchLink => search_link_options(&db)?,
@@ -69,6 +79,21 @@ pub fn show_options(db: &Db) -> Result<(), CustomErrors> {
6979
Ok(())
7080
}
7181

82+
fn get_status(db: &Db) -> Result<(), CustomErrors> {
83+
match db.get_status() {
84+
Ok(val) => {
85+
match val {
86+
Some((total_links, completed_links, skipped_links)) => {
87+
pretty_status(total_links, completed_links, skipped_links)
88+
}
89+
None => pretty_status(0, 0, 0),
90+
};
91+
}
92+
Err(e) => return Err(e),
93+
};
94+
Ok(())
95+
}
96+
7297
fn get_link_options(db: &Db) -> Result<(), CustomErrors> {
7398
let link = match db.get_single_link() {
7499
Ok(val) => match val {

src/database.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,35 @@ impl Db {
1212
Db { conn }
1313
}
1414

15+
/// get total, completed, and skipped links count
16+
pub fn get_status(&self) -> Result<Option<(i32, i32, i32)>, CustomErrors> {
17+
let mut stmt = match self.conn.prepare(
18+
"SELECT
19+
COALESCE(COUNT(*), 0) AS total_links,
20+
COALESCE(SUM(CASE WHEN is_solved = 1 THEN 1 ELSE 0 END), 0) AS completed_links,
21+
COALESCE(SUM(CASE WHEN is_skipped = 1 THEN 1 ELSE 0 END), 0) AS skipped_links
22+
FROM links",
23+
) {
24+
Ok(val) => val,
25+
Err(_) => return Err(CustomErrors::StatementFailed),
26+
};
27+
28+
match stmt.query_row([], |row| {
29+
let total_links: i32 = row.get(0)?;
30+
let completed_links: i32 = row.get(1)?;
31+
let skipped_links: i32 = row.get(2)?;
32+
Ok((total_links, completed_links, skipped_links))
33+
}) {
34+
Ok((total_links, completed_links, skipped_links)) => {
35+
Ok(Some((total_links, completed_links, skipped_links)))
36+
}
37+
Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
38+
Err(_) => Err(CustomErrors::Others(
39+
"Error: Something went wrong while checking the status".to_owned(),
40+
)),
41+
}
42+
}
43+
1544
/// add new links into the db
1645
pub fn add_link(&self, link: String) -> Result<(), CustomErrors> {
1746
match self.conn.execute(

src/print.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use tabled::settings::Style;
2-
use tabled::{Table, Tabled};
1+
use tabled::{
2+
settings::{object::Segment, Alignment, Modify, Style},
3+
Table, Tabled,
4+
};
35

46
#[derive(Tabled)]
57
struct Links {
@@ -8,6 +10,33 @@ struct Links {
810
solved_count: i32,
911
}
1012

13+
#[derive(Tabled)]
14+
struct Status {
15+
total_links: i32,
16+
completed_links: i32,
17+
skipped_links: i32,
18+
}
19+
20+
pub fn pretty_status(total_links: i32, completed_links: i32, skipped_links: i32) {
21+
let data = vec![Status {
22+
total_links,
23+
completed_links,
24+
skipped_links,
25+
}];
26+
27+
let mut table = Table::new(data);
28+
table.with(
29+
Modify::new(Segment::all())
30+
.with(Alignment::center())
31+
.with(Alignment::top()),
32+
);
33+
34+
let table = table.with(Style::modern());
35+
let table_string = table.to_string();
36+
37+
println!("{}", table_string);
38+
}
39+
1140
pub fn pretty_print(data: &[(String, i32)]) {
1241
let new_data: Vec<Links> = data
1342
.iter()

0 commit comments

Comments
 (0)