Skip to content

Commit

Permalink
Merge pull request #12 from gorbit99/master
Browse files Browse the repository at this point in the history
Support non-utf8 encoding
  • Loading branch information
wfxr authored May 8, 2021
2 parents 97e90b1 + 9e0151c commit 4242bc9
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 17 deletions.
6 changes: 5 additions & 1 deletion completions/bash/code-minimap.bash
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ _code-minimap() {

case "${cmd}" in
code-minimap)
opts=" -h -H -V --help --version --horizontal-scale --vertical-scale --padding <FILE> completion help"
opts=" -h -H -V --help --version --horizontal-scale --vertical-scale --padding --encoding <FILE> completion help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand All @@ -53,6 +53,10 @@ _code-minimap() {
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
--encoding)
COMPREPLY=($(compgen -W "UTF8 UTF8Lossy" -- "${cur}"))
return 0
;;
*)
COMPREPLY=()
;;
Expand Down
1 change: 1 addition & 0 deletions completions/elvish/code-minimap.elv
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ edit:completion:arg-completer[code-minimap] = [@words]{
cand -V 'Specify vertical scale factor'
cand --vertical-scale 'Specify vertical scale factor'
cand --padding 'Specify padding width'
cand --encoding 'Specify input encoding'
cand -h 'Prints help information'
cand --help 'Prints help information'
cand --version 'Prints version information'
Expand Down
1 change: 1 addition & 0 deletions completions/fish/code-minimap.fish
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
complete -c code-minimap -n "__fish_use_subcommand" -s H -l horizontal-scale -d 'Specify horizontal scale factor'
complete -c code-minimap -n "__fish_use_subcommand" -s V -l vertical-scale -d 'Specify vertical scale factor'
complete -c code-minimap -n "__fish_use_subcommand" -l padding -d 'Specify padding width'
complete -c code-minimap -n "__fish_use_subcommand" -l encoding -d 'Specify input encoding' -r -f -a "UTF8 UTF8Lossy"
complete -c code-minimap -n "__fish_use_subcommand" -s h -l help -d 'Prints help information'
complete -c code-minimap -n "__fish_use_subcommand" -l version -d 'Prints version information'
complete -c code-minimap -n "__fish_use_subcommand" -f -a "completion" -d 'Generate shell completion file'
Expand Down
1 change: 1 addition & 0 deletions completions/powershell/_code-minimap.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Register-ArgumentCompleter -Native -CommandName 'code-minimap' -ScriptBlock {
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Specify vertical scale factor')
[CompletionResult]::new('--vertical-scale', 'vertical-scale', [CompletionResultType]::ParameterName, 'Specify vertical scale factor')
[CompletionResult]::new('--padding', 'padding', [CompletionResultType]::ParameterName, 'Specify padding width')
[CompletionResult]::new('--encoding', 'encoding', [CompletionResultType]::ParameterName, 'Specify input encoding')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
Expand Down
1 change: 1 addition & 0 deletions completions/zsh/_code-minimap
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ _code-minimap() {
'-V+[Specify vertical scale factor]' \
'--vertical-scale=[Specify vertical scale factor]' \
'--padding=[Specify padding width]' \
'--encoding=[Specify input encoding]: :(UTF8 UTF8Lossy)' \
'-h[Prints help information]' \
'--help[Prints help information]' \
'--version[Prints version information]' \
Expand Down
6 changes: 6 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
edition = "2018"
struct_field_align_threshold = 40
max_width = 120
comment_width = 120
reorder_imports = true
fn_single_line = false
13 changes: 12 additions & 1 deletion src/bin/code-minimap/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::PathBuf;

use structopt::clap::{self, AppSettings};
use structopt::clap::{self, arg_enum, AppSettings};
pub use structopt::StructOpt;

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

/// Specify input encoding
#[structopt(long = "encoding", default_value = "UTF8Lossy", possible_values = &Encoding::variants(), case_insensitive = true)]
pub encoding: Encoding,

/// Subcommand
#[structopt(subcommand)]
pub subcommand: Option<Subcommand>,
Expand All @@ -42,3 +46,10 @@ pub struct CompletionOpt {
#[structopt(possible_values = &clap::Shell::variants())]
pub shell: clap::Shell,
}

arg_enum! {
pub enum Encoding {
UTF8,
UTF8Lossy,
}
}
25 changes: 18 additions & 7 deletions src/bin/code-minimap/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
mod cli;
use cli::{CompletionOpt, Opt, StructOpt, Subcommand};
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::process;
use std::{
fs::File,
io::{self, BufRead, BufReader, Read},
process,
};

use cli::{CompletionOpt, Encoding, Opt, StructOpt, Subcommand};
use code_minimap::lossy_reader::LossyReader;

fn main() {
if let Err(e) = try_main() {
Expand All @@ -24,12 +28,19 @@ fn try_main() -> anyhow::Result<()> {
}
None => {
let stdin = io::stdin();
let reader: Box<dyn BufRead> = match &opt.file {
Some(path) => Box::new(BufReader::new(File::open(path)?)),
None => Box::new(stdin.lock()),
let reader = match &opt.file {
Some(path) => buf_reader(&opt.encoding, File::open(path)?),
None => buf_reader(&opt.encoding, stdin),
};
code_minimap::print(reader, opt.hscale, opt.vscale, opt.padding)?;
}
}
Ok(())
}

fn buf_reader<R: 'static + Read>(encoding: &Encoding, reader: R) -> Box<dyn BufRead> {
match encoding {
Encoding::UTF8 => Box::new(BufReader::new(reader)),
Encoding::UTF8Lossy => Box::new(LossyReader::new(reader)),
}
}
19 changes: 11 additions & 8 deletions src/core.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{
io::{self, BufRead, Write},
ops::Range,
};

use itertools::Itertools;
use std::io::{self, BufRead, Write};
use std::ops::Range;

/// Write minimap to the writer.
pub fn write(
Expand Down Expand Up @@ -50,8 +53,7 @@ pub fn write(
/// Basic usage:
///
/// ```
/// use std::io;
/// use std::io::BufReader;
/// use std::{io, io::BufReader};
///
/// let stdin = io::stdin();
/// code_minimap::print(stdin.lock(), 1.0, 1.0, None).unwrap();
Expand All @@ -67,11 +69,11 @@ pub fn print(reader: impl BufRead, hscale: f64, vscale: f64, padding: Option<usi
/// Basic usage:
///
/// ```
/// use std::io;
/// use std::io::BufReader;
/// use std::{io, io::BufReader};
///
/// let stdin = io::stdin();
/// let s = code_minimap::write_to_string(stdin.lock(), 1.0, 1.0, None).unwrap();
/// let s =
/// code_minimap::write_to_string(stdin.lock(), 1.0, 1.0, None).unwrap();
/// print!("{}", s);
/// ```
pub fn write_to_string(reader: impl BufRead, hscale: f64, vscale: f64, padding: Option<usize>) -> io::Result<String> {
Expand Down Expand Up @@ -130,9 +132,10 @@ const BRAILLE_MATRIX : [char; 256] = [

#[cfg(test)]
mod test {
use super::*;
use rstest::*;

use super::*;

#[rstest(
input,
expected,
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub(crate) mod core;
pub mod lossy_reader;
pub use crate::core::*;
36 changes: 36 additions & 0 deletions src/lossy_reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::io::{self, BufRead, BufReader, Read};

pub struct LossyReader<R> {
inner: BufReader<R>,
}

impl<R: Read> LossyReader<R> {
pub fn new(inner: R) -> Self {
Self {
inner: BufReader::new(inner),
}
}
}

impl<R: Read> Read for LossyReader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}
}

impl<R: Read> BufRead for LossyReader<R> {
fn fill_buf(&mut self) -> io::Result<&[u8]> {
self.inner.fill_buf()
}

fn consume(&mut self, amt: usize) {
self.inner.consume(amt)
}

fn read_line(&mut self, buf: &mut String) -> std::io::Result<usize> {
let mut bytes = Vec::new();
let len = self.read_until(b'\n', &mut bytes)?;
buf.push_str(&String::from_utf8_lossy(&bytes));
Ok(len)
}
}

0 comments on commit 4242bc9

Please sign in to comment.