Skip to content

Commit 5a3035b

Browse files
committed
feat(preview): add --no-color flag to disable syntax highlighting
- Added --no-color option to preview subcommands for gitignore, issue, license, and PR templates - Updated preview functions to accept no_color flag and fall back to plain println! when set - Ensures compatibility with non-ANSI terminals and respects user preference for plain output
1 parent 1abd8be commit 5a3035b

4 files changed

Lines changed: 42 additions & 14 deletions

File tree

src/commands/gitignore/preview.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::utils::pretty_print;
33
use crate::utils::progress;
44
use crate::utils::remote::Fetcher;
55

6-
use super::{GITHUB_RAW_BASE, ensure_gitignore_cache, find_template_in_cache};
6+
use super::{ensure_gitignore_cache, find_template_in_cache, GITHUB_RAW_BASE};
77

88
#[derive(clap::Args, Debug, Clone)]
99
pub struct PreviewArgs {
@@ -14,6 +14,10 @@ pub struct PreviewArgs {
1414
/// Update the gitignore cache
1515
#[arg(long = "update-cache")]
1616
pub update_cache: bool,
17+
18+
/// Disable colored output
19+
#[arg(long = "no-color")]
20+
pub no_color: bool,
1721
}
1822

1923
impl super::Runnable for PreviewArgs {
@@ -28,17 +32,21 @@ impl super::Runnable for PreviewArgs {
2832
let cache = ensure_gitignore_cache(&mut cache_manager, self.update_cache)?;
2933

3034
for template_name in &self.args {
31-
preview_single_template(template_name, &cache)?;
35+
preview_single_template(template_name, &cache, self.no_color)?;
3236
}
3337

3438
Ok(())
3539
}
3640
}
3741

38-
fn preview_single_template(template: &str, cache: &super::Cache<String>) -> anyhow::Result<()> {
42+
fn preview_single_template(
43+
template: &str,
44+
cache: &super::Cache<String>,
45+
no_color: bool,
46+
) -> anyhow::Result<()> {
3947
// normalize template if it has the .gitignore ext
4048
let template = template.strip_suffix(".gitignore").unwrap_or(template);
41-
49+
4250
// Find the template path in cache
4351
let template_path = find_template_in_cache(template, cache)?;
4452

@@ -51,7 +59,10 @@ fn preview_single_template(template: &str, cache: &super::Cache<String>) -> anyh
5159
pb.set_message(msg);
5260
pb.finish_and_clear();
5361

54-
println!("\n === Preview: {} === \n", template);
55-
pretty_print::print_highlighted("gitignore", &content);
62+
if no_color {
63+
println!("{}", content);
64+
} else {
65+
pretty_print::print_highlighted("gitignore", &content);
66+
}
5667
Ok(())
5768
}

src/commands/issue/preview.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ use super::GITHUB_RAW_BASE;
88
pub struct PreviewArgs {
99
#[arg(allow_hyphen_values = true)]
1010
pub templates: Vec<String>,
11+
12+
/// Disable colored output
13+
#[arg(long = "no-color")]
14+
pub no_color: bool,
1115
}
1216

1317
impl super::Runnable for PreviewArgs {
@@ -19,14 +23,14 @@ impl super::Runnable for PreviewArgs {
1923
}
2024

2125
for template_name in &self.templates {
22-
preview_single_template(template_name)?;
26+
preview_single_template(template_name, self.no_color)?;
2327
}
2428

2529
Ok(())
2630
}
2731
}
2832

29-
fn preview_single_template(template: &str) -> anyhow::Result<()> {
33+
fn preview_single_template(template: &str, no_color: bool) -> anyhow::Result<()> {
3034
let fetcher = Fetcher::new();
3135
let url = format!("{}/issue-templates/{}.yml", GITHUB_RAW_BASE, template);
3236

@@ -36,6 +40,10 @@ fn preview_single_template(template: &str) -> anyhow::Result<()> {
3640
pb.set_message(msg);
3741
pb.finish_and_clear();
3842

39-
pretty_print::print_highlighted("yml", &content);
43+
if no_color {
44+
println!("{}", content);
45+
} else {
46+
pretty_print::print_highlighted("yml", &content);
47+
}
4048
Ok(())
4149
}

src/commands/license/preview.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use colored::*;
22

33
use super::{
4-
CHOOSEALICENSE_RAW_BASE_URL, SPDX_LICENSE_DETAILS_BASE_URL, SPDX_LICENSE_LIST_URL,
5-
ensure_spdx_license_cache,
4+
ensure_spdx_license_cache, CHOOSEALICENSE_RAW_BASE_URL, SPDX_LICENSE_DETAILS_BASE_URL,
5+
SPDX_LICENSE_LIST_URL,
66
};
77

88
use crate::utils::cache::{Cache, CacheManager};

src/commands/pr/preview.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ use super::GITHUB_RAW_BASE;
88
pub struct PreviewArgs {
99
#[arg(help = "PR template names to preview")]
1010
pub args: Vec<String>,
11+
12+
/// Disable colored output
13+
#[arg(long = "no-color")]
14+
pub no_color: bool,
1115
}
1216

1317
impl super::Runnable for PreviewArgs {
@@ -19,14 +23,14 @@ impl super::Runnable for PreviewArgs {
1923
}
2024

2125
for template_name in &self.args {
22-
preview_single_template(template_name)?;
26+
preview_single_template(template_name, self.no_color)?;
2327
}
2428

2529
Ok(())
2630
}
2731
}
2832

29-
fn preview_single_template(template: &str) -> anyhow::Result<()> {
33+
fn preview_single_template(template: &str, no_color: bool) -> anyhow::Result<()> {
3034
let fetcher = Fetcher::new();
3135
let url = format!("{}/pr-templates/{}.md", GITHUB_RAW_BASE, template);
3236

@@ -36,6 +40,11 @@ fn preview_single_template(template: &str) -> anyhow::Result<()> {
3640
pb.set_message(msg);
3741
pb.finish_and_clear();
3842

39-
pretty_print::print_highlighted("md", &content);
43+
println!("\n === Preview: {} === \n", template);
44+
if no_color {
45+
println!("{}", content);
46+
} else {
47+
pretty_print::print_highlighted("md", &content);
48+
}
4049
Ok(())
4150
}

0 commit comments

Comments
 (0)