--encoding cli flag

pull/22/head
chris west 4 years ago
parent 3d6e05c081
commit 4f652a7209

@ -63,6 +63,9 @@ If no URL is given, however, *phetch* will launch and open its default
*-C*, *--no-config*
Do not use any config file.
*-e*, *--encoding* _ENCODING_
Render text views in CP437 or UTF8 (default) encoding.
*-h*, *--help*
Print a help summary and exit.

@ -1,11 +1,14 @@
//! args::parse() is used to parse command line arguments into a
//! Config structure.
use crate::{
config::{self, Config},
ui::Mode,
use {
crate::{
config::{self, Config},
encoding::Encoding,
ui::Mode,
},
std::{error::Error, fmt, result::Result},
};
use std::{error::Error, fmt, result::Result};
/// The error returned if something goes awry while parsing the
/// command line arguments.
@ -182,6 +185,14 @@ pub fn parse<T: AsRef<str>>(args: &[T]) -> Result<Config, ArgError> {
set_nomedia = true;
cfg.media = None;
}
"-e" | "--encoding" | "-encoding" => {
if let Some(encoding) = iter.next() {
cfg.encoding = Encoding::from_str(encoding.as_ref())
.map_err(|e| ArgError::new(e.to_string()))?;
} else {
return Err(ArgError::new("--encoding expects an ENCODING arg"));
}
}
arg => {
if arg.starts_with('-') {
return Err(ArgError::new(format!("unknown flag: {}", arg)));

@ -1,5 +1,7 @@
use std::io::Result;
/// Encoding of Gopher response. Only UTF8 and CP437 are supported.
#[derive(Copy, Clone)]
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Encoding {
/// Unicode
UTF8,
@ -12,3 +14,17 @@ impl Default for Encoding {
Encoding::UTF8
}
}
impl Encoding {
/// Accepts a string like "UTF8" or "CP437" and returns the
/// appropriate `Encoding`, or an `Err`.
pub fn from_str(s: &str) -> Result<Self> {
match s.to_lowercase().as_ref() {
"utf8" | "utf-8" | "utf 8" => Ok(Encoding::UTF8),
"cp437" | "cp-437" | "cp 437" | "pc8" | "pc-8" | "oem us" | "oem-us" => {
Ok(Encoding::CP437)
}
_ => Err(error!("Expected CP437 or UTF8 encoding")),
}
}
}

Loading…
Cancel
Save