diff --git a/doc/phetch.1.md b/doc/phetch.1.md index ae01803..7742033 100644 --- a/doc/phetch.1.md +++ b/doc/phetch.1.md @@ -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. diff --git a/src/args.rs b/src/args.rs index a6541b9..4f62d18 100644 --- a/src/args.rs +++ b/src/args.rs @@ -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>(args: &[T]) -> Result { 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))); diff --git a/src/encoding.rs b/src/encoding.rs index 4abed2e..238c7c6 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -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 { + 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")), + } + } +}