Skip to content

Commit

Permalink
Add option to override language detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ollej committed Jul 3, 2022
1 parent f548aa7 commit 05018a3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ OPTIONS:
-c, --code <code> Code to display, overrides both `filename` and `gist`
-f, --filename <filename> Path to sourcecode file to display [default: assets/helloworld.rs]
-g, --gist <gist> Gist id to display, if set, will override `filename` option
-l, --language <language> Language of the code, if empty defaults to file extension
-t, --theme <theme> Path to theme.json file [default: assets/theme.json]
```

Expand Down
16 changes: 13 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ async fn load_sourcecode(
};
}

fn language_from_extension(filename: String) -> Option<String> {
detect_lang::from_path(filename).map(|lang| lang.id().to_string())
}

async fn build_codebox(opt: &CliOptions, theme: &Theme) -> TextBox {
let font_bold = load_ttf_font(&theme.font_bold)
.await
Expand All @@ -82,7 +86,10 @@ async fn build_codebox(opt: &CliOptions, theme: &Theme) -> TextBox {
load_sourcecode(opt.gist.clone(), opt.filename.clone(), opt.code.clone())
.await
.expect("Couldn't load sourcecode!");
let language = detect_lang::from_path(filename.clone()).map(|lang| lang.id().to_string());
let language = opt
.language
.clone()
.or_else(|| language_from_extension(filename));

let code_box_builder = CodeBoxBuilder::new(theme.clone(), font_code, font_bold, font_italic);

Expand All @@ -95,15 +102,18 @@ async fn build_codebox(opt: &CliOptions, theme: &Theme) -> TextBox {
about = "A small tool to display sourcecode files"
)]
struct CliOptions {
/// Code to display, overrides both `filename` and `gist`
#[structopt(short, long)]
pub code: Option<String>,
/// Path to sourcecode file to display [default: assets/helloworld.rs]
#[structopt(short, long, parse(from_os_str))]
pub filename: Option<PathBuf>,
/// Gist id to display, if set, will override `filename` option
#[structopt(short, long)]
pub gist: Option<String>,
/// Code to display, overrides both `filename` and `gist`
/// Language of the code, if empty defaults to file extension.
#[structopt(short, long)]
pub code: Option<String>,
pub language: Option<String>,
/// Path to theme.json file
#[structopt(short, long, parse(from_os_str), default_value = "assets/theme.json")]
pub theme: PathBuf,
Expand Down

0 comments on commit 05018a3

Please # to comment.