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

Co-authored-by: Niklas Mohrin <dev@niklasmohrin.de>
  • Loading branch information
lengyijun and niklasmohrin committed Nov 10, 2024
1 parent d44613c commit c757300
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 12 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, requires = "command", conflicts_with = "edit_page")]
pub edit_patch: bool,

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

/// Render a specific markdown file
#[arg(
short = 'f',
Expand Down
67 changes: 55 additions & 12 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;
use app_dirs::AppInfo;
use clap::Parser;

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

let custom_pages_dir = config
.directories
.custom_pages_dir
.as_ref()
.map(PathWithSource::path);

// Note: According to the TLDR client spec, page names must be transparently
// lowercased before lookup:
// https://github.com/tldr-pages/tldr/blob/main/CLIENT-SPECIFICATION.md#page-names
let command = args.command.join("-").to_lowercase();

if args.edit_patch || args.edit_page {
let Some(custom_pages_dir) = custom_pages_dir else {
print_error(enable_styles, &anyhow!("Fail to get custom page dir"));
process::exit(1);
};
let _ = create_dir_all(custom_pages_dir);

let file_name = if args.edit_patch {
format!("{command}.patch.md")
} else {
format!("{command}.page.md")
};
let custom_page_path = custom_pages_dir.join(file_name);
let Ok(editor) = env::var("EDITOR") else {
print_error(
enable_styles,
&anyhow!("`EDITOR` not set. Please set `EDITOR` by `export EDITOR=vim`."),
);
process::exit(1);
};
println!("Editing {custom_page_path:?}");
match std::process::Command::new(&editor)
.arg(custom_page_path.to_str().unwrap())
.status()
{
Ok(status) => {
if !status.success() {
print_error(
enable_styles,
&anyhow!("{editor} exit with code {:?}", status.code()),
);
}
}
Err(e) => {
print_error(enable_styles, &e.into());
}
}
return;
}

// Show various paths
if args.show_paths {
show_paths(&config);
Expand Down Expand Up @@ -321,19 +374,14 @@ fn main() {

// Check cache presence and freshness
if !cache_updated
&& (args.list || !args.command.is_empty())
&& (args.list || !command.is_empty())
&& check_cache(&cache, &args, enable_styles) == CheckCacheResult::CacheMissing
{
process::exit(1);
}

// List cached commands and exit
if args.list {
let custom_pages_dir = config
.directories
.custom_pages_dir
.as_ref()
.map(PathWithSource::path);
println!(
"{}",
cache.list_pages(custom_pages_dir, platforms).join("\n")
Expand All @@ -342,12 +390,7 @@ fn main() {
}

// Show command from cache
if !args.command.is_empty() {
// Note: According to the TLDR client spec, page names must be transparently
// lowercased before lookup:
// https://github.com/tldr-pages/tldr/blob/main/CLIENT-SPECIFICATION.md#page-names
let command = args.command.join("-").to_lowercase();

if !command.is_empty() {
// Collect languages
let languages = args
.language
Expand Down
19 changes: 19 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,3 +959,22 @@ fn test_raw_render_file() {
.success()
.stdout(diff(include_str!("inkscape-v1.md")));
}

#[test]
fn test_edit_page() {
let testenv = TestEnv::new();
testenv.write_config(format!(
"[directories]\ncustom_pages_dir = '{}'",
testenv.custom_pages_dir.path().to_str().unwrap()
));

let args = vec!["--edit-page", "foo"];

testenv
.command()
.args(&args)
.env("EDITOR", "touch")
.assert()
.success();
assert!(testenv.custom_pages_dir.path().join("foo.page.md").exists());
}

0 comments on commit c757300

Please sign in to comment.