Skip to content

Commit 6bc64df

Browse files
committed
Add tqdm to display progress bar
1 parent 53f371b commit 6bc64df

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ plist = "1.7.0"
3434
threadpool = "1.8.1"
3535
log = "0.4.25"
3636
chrono = "0.4.39"
37+
tqdm = "0.7.0"

src/dbutil.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use rusqlite::Connection;
2+
use std::path::Path;
3+
4+
#[allow(dead_code)]
5+
fn get_columns(manifest_db_path: &Path) -> rusqlite::Result<(), Box<dyn std::error::Error>> {
6+
let conn = Connection::open(manifest_db_path)?;
7+
let mut col_smt = conn.prepare("PRAGMA table_info(Files)")?;
8+
let columns: Vec<String> = col_smt
9+
.query_map([], |row| {
10+
// The column name is in the second column (index 1)
11+
let col_name: String = row.get(1)?;
12+
Ok(col_name)
13+
})?
14+
.collect::<Result<Vec<String>, _>>()?;
15+
for col in columns {
16+
println!("{}", col);
17+
}
18+
Ok(())
19+
}
20+
21+
#[allow(dead_code)]
22+
fn get_table(
23+
manifest_db_path: &Path,
24+
limit: Option<usize>,
25+
) -> rusqlite::Result<(), Box<dyn std::error::Error>> {
26+
let conn = Connection::open(manifest_db_path)?;
27+
let statement = if limit.is_some() {
28+
format!("SELECT * FROM Files LIMIT {}", limit.unwrap())
29+
} else {
30+
"SELECT * FROM Files".to_string()
31+
};
32+
let mut col_smt = conn.prepare(&statement)?;
33+
let columns: Vec<String> = col_smt
34+
.column_names()
35+
.iter()
36+
.map(|&s| s.to_string())
37+
.collect();
38+
let mut rows = col_smt.query([])?;
39+
println!("{:<20} {:<50}", "Column Name", "Value");
40+
while let Some(row) = rows.next()? {
41+
for (i, col_name) in columns.iter().enumerate() {
42+
let value: String = row.get::<_, String>(i).unwrap_or_default();
43+
println!("{:<20} {:<50}", col_name, value);
44+
}
45+
println!();
46+
}
47+
Ok(())
48+
}

src/fileio.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use std::fs::{create_dir_all, File};
66
use std::io::copy;
77
use std::path::Path;
88
use std::sync::mpsc::channel;
9+
use std::sync::{Arc, Mutex};
910
use threadpool::ThreadPool;
11+
use tqdm::{pbar, Pbar};
1012

1113
/// Function to retrieve the value of a key from a plist file
1214
///
@@ -53,6 +55,14 @@ pub fn parse_manifest_db(
5355
arguments: &parser::ArgConfig,
5456
) -> Result<(), Box<dyn std::error::Error>> {
5557
let conn = Connection::open(manifest_db_path)?;
58+
59+
// Get count to update progress bar
60+
let mut count_stmt = conn.prepare(
61+
"SELECT COUNT(*) FROM Files WHERE relativePath LIKE '%DCIM/%' OR relativePath LIKE '%PhotoData/%'"
62+
)?;
63+
let count: usize = count_stmt.query_row([], |row| row.get(0))?;
64+
let progress_bar_base: Arc<Mutex<Pbar>> = Arc::new(Mutex::new(pbar(Some(count))));
65+
5666
let mut stmt = conn.prepare("SELECT fileID, relativePath FROM Files WHERE relativePath LIKE '%DCIM/%' OR relativePath LIKE '%PhotoData/%'")?;
5767
let rows = stmt.query_map([], |row| {
5868
let file_id: String = row.get(0)?;
@@ -70,10 +80,14 @@ pub fn parse_manifest_db(
7080
let backup_cloned = backup.path.clone();
7181
let output_dir_cloned = arguments.output_dir.clone();
7282
let sender_cloned = sender.clone();
83+
let progress_bar = Arc::clone(&progress_bar_base);
7384
pool.execute(move || {
7485
let result =
7586
extract_files(&backup_cloned, &output_dir_cloned, file_id, relative_path);
7687
sender_cloned.send(result).expect("Failed to send result");
88+
// Safely update progress bar
89+
let mut progress = progress_bar.lock().unwrap();
90+
progress.update(1).unwrap();
7791
});
7892
}
7993
Err(err) => {
@@ -83,6 +97,7 @@ pub fn parse_manifest_db(
8397
}
8498
// Wait for all tasks to complete
8599
drop(sender); // Close the sending side of the channel
100+
pool.join();
86101
for result in receiver {
87102
if let Err(err) = result {
88103
log::error!("Error processing files: {:?}", err);

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub mod logger;
1313
pub mod parser;
1414
/// Module for helper functions
1515
pub mod squire;
16+
mod dbutil;
1617

1718
use rusqlite::Result;
1819

0 commit comments

Comments
 (0)