Skip to content

Commit

Permalink
Add args: --edit-page and --edit-patch
Browse files Browse the repository at this point in the history
Fix #383
  • Loading branch information
Your Name committed Nov 2, 2024
1 parent d44613c commit c54276c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ pub(crate) struct Cli {
#[arg(short = 'l', long = "list")]
pub list: bool,

/// Edit custom patch with `EDITOR`
#[arg(long = "edit-patch")]
pub edit_patch: bool,

/// Edit custom page with `EDITOR`
#[arg(long = "edit-page")]
pub edit_page: bool,

/// Render a specific markdown file
#[arg(
short = 'f',
Expand Down
64 changes: 64 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ compile_error!(

use std::{
env,
fs::create_dir_all,
io::{self, IsTerminal},
process,
};

use anyhow::{anyhow, Context};
use app_dirs::AppInfo;
use clap::Parser;

Expand Down Expand Up @@ -276,6 +278,68 @@ fn main() {
}
};

if args.edit_page && args.edit_patch {
print_error(
enable_styles,
&anyhow!("Don't edit-patch and edit-page at the same time"),
);
process::exit(1);
}

if args.edit_patch || args.edit_page {
if args.command.is_empty() {
if args.edit_patch {
print_error(
enable_styles,
&anyhow!(
"
No command given
Sample usage :
tldr --edit-patch <cmd>",
),
);
}
if args.edit_page {
print_error(
enable_styles,
&anyhow!(
"
No command given
Sample usage :
tldr --edit-page <cmd>",
),
);
}
process::exit(1);
}
let command = args.command.join("-").to_lowercase();
let custom_page_dir = config
.directories
.custom_pages_dir
.as_ref()
.map(PathWithSource::path);
if let Some(custom_page_dir) = custom_page_dir {
let _ = create_dir_all(custom_page_dir);

let file_name = if args.edit_patch {
format!("{command}.patch.md")
} else {
format!("{command}.page.md")
};
let custom_page_path = custom_page_dir.join(file_name);
println!("Editing {custom_page_path:?}");
let editor = env::var("EDITOR").context("env `EDITOR` not set").unwrap();
let _ = std::process::Command::new(editor)
.arg(custom_page_path.to_str().unwrap())
.status();
} else {
print_error(enable_styles, &anyhow!("Fail to get custom page dir"));
process::exit(1);
}
}

// Show various paths
if args.show_paths {
show_paths(&config);
Expand Down

0 comments on commit c54276c

Please sign in to comment.