Skip to content

Commit

Permalink
Fix output containing escaped chars
Browse files Browse the repository at this point in the history
  • Loading branch information
chmln committed Dec 23, 2018
1 parent 085d4ec commit 7bfe062
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 24 deletions.
23 changes: 15 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "re"
name = "sd"
version = "0.1.0"
edition = "2018"
authors = ["Gregory <gregory.mkv@gmail.com>"]
Expand All @@ -13,6 +13,7 @@ categories = ["command-line-utilities", "text-processing", "development-tools"]
[dependencies]
clap = "2.32.0"
regex = "1.1.0"
unescape = "0.1.0"

[profile.release]
opt-level = 3
Expand Down
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use {crate::{Error,Stream,Source}};
use crate::{Error, Source, Stream};

const VERSION: &'static str = env!("CARGO_PKG_VERSION");

Expand All @@ -7,7 +7,7 @@ pub(crate) struct App;
impl App {
pub(crate) fn run() -> Result<(), Error> {
use clap;
let app = clap::App::new("re")
let app = clap::App::new("sd")
.version(VERSION)
.setting(clap::AppSettings::ColoredHelp)
.setting(clap::AppSettings::NextLineHelp)
Expand Down
8 changes: 4 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub(crate) struct Error {
// low-level cause, used only for debugging
cause: String,
// user-facing error output
description: String,
message: String,
}

impl<T> From<T> for Error
Expand All @@ -14,19 +14,19 @@ where
cause: err
.source()
.map_or_else(|| "N/A".to_string(), |e| e.to_string()),
description: format!("{}", err),
message: format!("{}", err),
}
}
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.description)
write!(f, "{}", self.message)
}
}

impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Error: {}\nDetails: {}", self.description, self.cause)
write!(f, "Error: {}\nDetails: {}", self.message, self.cause)
}
}
17 changes: 9 additions & 8 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use {crate::Error, std::{fs::File, io::prelude::*}};
use {
crate::{utils, Error},
std::{fs::File, io::prelude::*},
};

pub(crate) enum Source {
Stdin,
Expand Down Expand Up @@ -31,7 +34,6 @@ impl Source {
}
}
}

}

pub(crate) struct Stream {
Expand All @@ -50,11 +52,12 @@ impl Stream {
replace_with: &str,
) -> Result<(), crate::Error> {
if is_regex {
self.data = regex::Regex::new(look_for)?
let replaced = regex::Regex::new(look_for)?
.replace_all(&self.data, replace_with)
.to_string()
.to_string();
self.data = utils::unescape(&replaced).unwrap_or_else(|| replaced);
} else {
self.data = self.data.replace(look_for, replace_with)
self.data = self.data.replace(look_for, replace_with);
}
Ok(())
}
Expand All @@ -64,9 +67,7 @@ impl Stream {
// Otherwise, pipe to stdout.
pub(crate) fn output(self, source: &Source) -> Result<(), crate::Error> {
match source {
Source::File(path) => {
Ok(std::fs::write(path, self.data)?)
},
Source::File(path) => Ok(std::fs::write(path, self.data)?),
Source::Stdin => {
let stdout = std::io::stdout();
let mut handle = stdout.lock();
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod app;
mod error;
mod input;
pub(crate) mod utils;

pub(crate) use {
crate::error::Error,
Expand Down
5 changes: 4 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@

pub(crate) fn unescape(s: &str) -> Option<String> {
use unescape::unescape;
unescape(s)
}

0 comments on commit 7bfe062

Please sign in to comment.