Skip to content

Commit 4242bc9

Browse files
authored
Merge pull request #12 from gorbit99/master
Support non-utf8 encoding
2 parents 97e90b1 + 9e0151c commit 4242bc9

File tree

11 files changed

+93
-17
lines changed

11 files changed

+93
-17
lines changed

completions/bash/code-minimap.bash

+5-1
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

+1
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

+1
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

+1
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

+1
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]' \

rustfmt.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
edition = "2018"
2+
struct_field_align_threshold = 40
3+
max_width = 120
4+
comment_width = 120
5+
reorder_imports = true
6+
fn_single_line = false

src/bin/code-minimap/cli.rs

+12-1
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

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
mod cli;
2-
use cli::{CompletionOpt, Opt, StructOpt, Subcommand};
3-
use std::fs::File;
4-
use std::io::{self, BufRead, BufReader};
5-
use std::process;
2+
use std::{
3+
fs::File,
4+
io::{self, BufRead, BufReader, Read},
5+
process,
6+
};
7+
8+
use cli::{CompletionOpt, Encoding, Opt, StructOpt, Subcommand};
9+
use code_minimap::lossy_reader::LossyReader;
610

711
fn main() {
812
if let Err(e) = try_main() {
@@ -24,12 +28,19 @@ fn try_main() -> anyhow::Result<()> {
2428
}
2529
None => {
2630
let stdin = io::stdin();
27-
let reader: Box<dyn BufRead> = match &opt.file {
28-
Some(path) => Box::new(BufReader::new(File::open(path)?)),
29-
None => Box::new(stdin.lock()),
31+
let reader = match &opt.file {
32+
Some(path) => buf_reader(&opt.encoding, File::open(path)?),
33+
None => buf_reader(&opt.encoding, stdin),
3034
};
3135
code_minimap::print(reader, opt.hscale, opt.vscale, opt.padding)?;
3236
}
3337
}
3438
Ok(())
3539
}
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+
}

src/core.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
use std::{
2+
io::{self, BufRead, Write},
3+
ops::Range,
4+
};
5+
16
use itertools::Itertools;
2-
use std::io::{self, BufRead, Write};
3-
use std::ops::Range;
47

58
/// Write minimap to the writer.
69
pub fn write(
@@ -50,8 +53,7 @@ pub fn write(
5053
/// Basic usage:
5154
///
5255
/// ```
53-
/// use std::io;
54-
/// use std::io::BufReader;
56+
/// use std::{io, io::BufReader};
5557
///
5658
/// let stdin = io::stdin();
5759
/// code_minimap::print(stdin.lock(), 1.0, 1.0, None).unwrap();
@@ -67,11 +69,11 @@ pub fn print(reader: impl BufRead, hscale: f64, vscale: f64, padding: Option<usi
6769
/// Basic usage:
6870
///
6971
/// ```
70-
/// use std::io;
71-
/// use std::io::BufReader;
72+
/// use std::{io, io::BufReader};
7273
///
7374
/// let stdin = io::stdin();
74-
/// let s = code_minimap::write_to_string(stdin.lock(), 1.0, 1.0, None).unwrap();
75+
/// let s =
76+
/// code_minimap::write_to_string(stdin.lock(), 1.0, 1.0, None).unwrap();
7577
/// print!("{}", s);
7678
/// ```
7779
pub fn write_to_string(reader: impl BufRead, hscale: f64, vscale: f64, padding: Option<usize>) -> io::Result<String> {
@@ -130,9 +132,10 @@ const BRAILLE_MATRIX : [char; 256] = [
130132

131133
#[cfg(test)]
132134
mod test {
133-
use super::*;
134135
use rstest::*;
135136

137+
use super::*;
138+
136139
#[rstest(
137140
input,
138141
expected,

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub(crate) mod core;
2+
pub mod lossy_reader;
23
pub use crate::core::*;

src/lossy_reader.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::io::{self, BufRead, BufReader, Read};
2+
3+
pub struct LossyReader<R> {
4+
inner: BufReader<R>,
5+
}
6+
7+
impl<R: Read> LossyReader<R> {
8+
pub fn new(inner: R) -> Self {
9+
Self {
10+
inner: BufReader::new(inner),
11+
}
12+
}
13+
}
14+
15+
impl<R: Read> Read for LossyReader<R> {
16+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
17+
self.inner.read(buf)
18+
}
19+
}
20+
21+
impl<R: Read> BufRead for LossyReader<R> {
22+
fn fill_buf(&mut self) -> io::Result<&[u8]> {
23+
self.inner.fill_buf()
24+
}
25+
26+
fn consume(&mut self, amt: usize) {
27+
self.inner.consume(amt)
28+
}
29+
30+
fn read_line(&mut self, buf: &mut String) -> std::io::Result<usize> {
31+
let mut bytes = Vec::new();
32+
let len = self.read_until(b'\n', &mut bytes)?;
33+
buf.push_str(&String::from_utf8_lossy(&bytes));
34+
Ok(len)
35+
}
36+
}

0 commit comments

Comments
 (0)