Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encrypted box support #32

Merged
merged 12 commits into from
Oct 23, 2024
334 changes: 308 additions & 26 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ regex = "1.11.0"
serde = { version = "1.0.210", features = ["derive"] }
toml = "0.8.19"
which = "6.0.3"
zip = { version = "2.2.0", default-features = false, features = ["aes-crypto"] }

[target.'cfg(unix)'.dependencies]
stdio-override = "0.1.3"
Expand Down
30 changes: 23 additions & 7 deletions src/boxops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ pub fn file_manager() -> Result<()> {

pub fn edit_box(cur_box: &str, diffwith: Option<String>) {
let boxpath = get_inbox_file(cur_box);
_ = TaskBox::new(boxpath.clone()); // only touch file
let tb = TaskBox::new(boxpath.clone());
if tb.encrypted {
println!("cannot edit {}box, plz decrypt first", S_failure!(LOCKED));
std::process::exit(1);
}

if let Some(other) = diffwith {
let otherf = if other.ends_with(".md") {
Expand Down Expand Up @@ -102,14 +106,26 @@ pub fn list_boxes() {
let mut boxes = Vec::new();
for entry in std::fs::read_dir(&basedir).expect("cannot read dir") {
let path = entry.expect("cannot get entry").path();
if path.is_file() && path.extension() == Some(OsStr::new("md")) {
boxes.push(String::from(path.file_stem().unwrap().to_str().unwrap()))
if path.is_file() {
if path.extension() == Some(OsStr::new("md")) {
boxes.push((String::from(path.file_stem().unwrap().to_str().unwrap()), false))
} else if path.extension() == Some(OsStr::new("mdx")) {
boxes.push((String::from(path.file_stem().unwrap().to_str().unwrap()), true))
}
}
}
boxes.sort(); boxes.reverse(); boxes.into_iter().for_each(
|b| {
print!("{} {}",S_checkbox!(TASKBOX), b);
if let Some(alias) = get_box_alias(&b) {
boxes.sort_by(|a,b| b.0.cmp(&a.0));
boxes.into_iter().for_each(
|(boxname, encrypted)| {
if encrypted {
print!("{} ", S_warning!(LOCKED));
} else {
print!(" ");

}
print!("{} {}",S_checkbox!(TASKBOX), boxname);
let alias = get_box_alias(&boxname);
if alias != boxname {
println!(" ({})", S_hints!(alias))
} else {
println!()
Expand Down
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ pub enum Commands {
#[clap(visible_aliases(["lb"]))]
Listbox,

/// -> encrypt todo box file
Enc,

/// -> decrypt todo box file
Dec,

/// -> edit todo inbox file
#[clap(visible_aliases(["e", "ed"]))]
Edit {
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ fn main() {
match args.command {
Some(Commands::List) | None => TaskBox::new(inbox_path).list(false),
Some(Commands::Listall) => TaskBox::new(inbox_path).list(true),
Some(Commands::Enc) => TaskBox::new(inbox_path).encrypt().unwrap(),
Some(Commands::Dec) => TaskBox::new(inbox_path).decrypt().unwrap(),
Some(Commands::Routines) => TaskBox::new(get_inbox_file(ROUTINE_BOXNAME)).list(true),

Some(Commands::Count) => {
Expand Down Expand Up @@ -151,6 +153,7 @@ fn main() {
inbox_path = get_inbox_file("routine")
}
let mut todo = TaskBox::new(inbox_path);
todo.load();

#[allow(clippy::redundant_closure)]
let input = what.unwrap_or_else(|| i_gettext());
Expand Down
16 changes: 16 additions & 0 deletions src/styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub const QUESTION: &str = "󱜹";
pub const ROUTINES: &str = "󰃯";
pub const DATESTAMP: &str = "󰴹"; // 󰃵
pub const WEEKLINE: &str = "󰕶";
pub const LOCKED: &str = "󰍁";
// S means Style
#[macro_export]
macro_rules! S_fpath { ($e:expr) => { $e.to_string().purple() }; }
Expand Down Expand Up @@ -68,6 +69,21 @@ pub fn get_confirm_style() -> RenderConfig<'static> {
.with_attr(Attributes::BOLD)
)
}
pub fn get_pass_input_style() -> RenderConfig<'static> {
RenderConfig::default()
.with_prompt_prefix(LOCKED.into())
.with_answered_prompt_prefix(LOCKED.into())
.with_help_message(
StyleSheet::default()
.with_fg(Color::DarkGrey)
.with_attr(Attributes::ITALIC)
)
.with_answer(
StyleSheet::default()
.with_fg(Color::DarkGreen)
.with_attr(Attributes::BOLD)
)
}
pub fn get_text_input_style() -> RenderConfig<'static> {
RenderConfig::default()
.with_prompt_prefix(CHECKBOX.into())
Expand Down
Loading
Loading