Skip to content

Commit 9e0151c

Browse files
committed
feat: make encoding configurable
1 parent 9df7978 commit 9e0151c

File tree

7 files changed

+33
-7
lines changed

7 files changed

+33
-7
lines changed

completions/bash/code-minimap.bash

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ _code-minimap() {
2626

2727
case "${cmd}" in
2828
code-minimap)
29-
opts=" -h -H -V --help --version --horizontal-scale --vertical-scale --padding <FILE> completion help"
29+
opts=" -h -H -V --help --version --horizontal-scale --vertical-scale --padding --encoding <FILE> completion help"
3030
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
3131
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
3232
return 0
@@ -53,6 +53,10 @@ _code-minimap() {
5353
COMPREPLY=($(compgen -f "${cur}"))
5454
return 0
5555
;;
56+
--encoding)
57+
COMPREPLY=($(compgen -W "UTF8 UTF8Lossy" -- "${cur}"))
58+
return 0
59+
;;
5660
*)
5761
COMPREPLY=()
5862
;;

completions/elvish/code-minimap.elv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ edit:completion:arg-completer[code-minimap] = [@words]{
2020
cand -V 'Specify vertical scale factor'
2121
cand --vertical-scale 'Specify vertical scale factor'
2222
cand --padding 'Specify padding width'
23+
cand --encoding 'Specify input encoding'
2324
cand -h 'Prints help information'
2425
cand --help 'Prints help information'
2526
cand --version 'Prints version information'

completions/fish/code-minimap.fish

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
complete -c code-minimap -n "__fish_use_subcommand" -s H -l horizontal-scale -d 'Specify horizontal scale factor'
22
complete -c code-minimap -n "__fish_use_subcommand" -s V -l vertical-scale -d 'Specify vertical scale factor'
33
complete -c code-minimap -n "__fish_use_subcommand" -l padding -d 'Specify padding width'
4+
complete -c code-minimap -n "__fish_use_subcommand" -l encoding -d 'Specify input encoding' -r -f -a "UTF8 UTF8Lossy"
45
complete -c code-minimap -n "__fish_use_subcommand" -s h -l help -d 'Prints help information'
56
complete -c code-minimap -n "__fish_use_subcommand" -l version -d 'Prints version information'
67
complete -c code-minimap -n "__fish_use_subcommand" -f -a "completion" -d 'Generate shell completion file'

completions/powershell/_code-minimap.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Register-ArgumentCompleter -Native -CommandName 'code-minimap' -ScriptBlock {
2525
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Specify vertical scale factor')
2626
[CompletionResult]::new('--vertical-scale', 'vertical-scale', [CompletionResultType]::ParameterName, 'Specify vertical scale factor')
2727
[CompletionResult]::new('--padding', 'padding', [CompletionResultType]::ParameterName, 'Specify padding width')
28+
[CompletionResult]::new('--encoding', 'encoding', [CompletionResultType]::ParameterName, 'Specify input encoding')
2829
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
2930
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
3031
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')

completions/zsh/_code-minimap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ _code-minimap() {
2020
'-V+[Specify vertical scale factor]' \
2121
'--vertical-scale=[Specify vertical scale factor]' \
2222
'--padding=[Specify padding width]' \
23+
'--encoding=[Specify input encoding]: :(UTF8 UTF8Lossy)' \
2324
'-h[Prints help information]' \
2425
'--help[Prints help information]' \
2526
'--version[Prints version information]' \

src/bin/code-minimap/cli.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::PathBuf;
22

3-
use structopt::clap::{self, AppSettings};
3+
use structopt::clap::{self, arg_enum, AppSettings};
44
pub use structopt::StructOpt;
55

66
#[derive(StructOpt)]
@@ -25,6 +25,10 @@ pub struct Opt {
2525
#[structopt(long = "padding")]
2626
pub padding: Option<usize>,
2727

28+
/// Specify input encoding
29+
#[structopt(long = "encoding", default_value = "UTF8Lossy", possible_values = &Encoding::variants(), case_insensitive = true)]
30+
pub encoding: Encoding,
31+
2832
/// Subcommand
2933
#[structopt(subcommand)]
3034
pub subcommand: Option<Subcommand>,
@@ -42,3 +46,10 @@ pub struct CompletionOpt {
4246
#[structopt(possible_values = &clap::Shell::variants())]
4347
pub shell: clap::Shell,
4448
}
49+
50+
arg_enum! {
51+
pub enum Encoding {
52+
UTF8,
53+
UTF8Lossy,
54+
}
55+
}

src/bin/code-minimap/main.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
mod cli;
22
use std::{
33
fs::File,
4-
io::{self, BufRead},
4+
io::{self, BufRead, BufReader, Read},
55
process,
66
};
77

8-
use cli::{CompletionOpt, Opt, StructOpt, Subcommand};
8+
use cli::{CompletionOpt, Encoding, Opt, StructOpt, Subcommand};
99
use code_minimap::lossy_reader::LossyReader;
1010

1111
fn main() {
@@ -28,12 +28,19 @@ fn try_main() -> anyhow::Result<()> {
2828
}
2929
None => {
3030
let stdin = io::stdin();
31-
let reader: Box<dyn BufRead> = match &opt.file {
32-
Some(path) => Box::new(LossyReader::new(File::open(path)?)),
33-
None => Box::new(LossyReader::new(stdin)),
31+
let reader = match &opt.file {
32+
Some(path) => buf_reader(&opt.encoding, File::open(path)?),
33+
None => buf_reader(&opt.encoding, stdin),
3434
};
3535
code_minimap::print(reader, opt.hscale, opt.vscale, opt.padding)?;
3636
}
3737
}
3838
Ok(())
3939
}
40+
41+
fn buf_reader<R: 'static + Read>(encoding: &Encoding, reader: R) -> Box<dyn BufRead> {
42+
match encoding {
43+
Encoding::UTF8 => Box::new(BufReader::new(reader)),
44+
Encoding::UTF8Lossy => Box::new(LossyReader::new(reader)),
45+
}
46+
}

0 commit comments

Comments
 (0)