Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview #127

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 50 additions & 53 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@ license = "MIT"
homepage = "https://github.com/chmln/sd"
repository = "https://github.com/chmln/sd.git"
categories = ["command-line-utilities", "text-processing", "development-tools"]
resolver = "2"

[dependencies]
regex = "1.4.3"
regex = "1.5.4"
structopt = "0.3.21"
rayon = "1.5.0"
unescape = "0.1.0"
memmap = "0.7.0"
tempfile = "3.2.0"
thiserror = "1.0.24"
globwalk = "0.8.1"
atty = "0.2.14"
ignore = "0.4.17"
ansi_term = "0.12.1"
itertools = "0.10.0"

[dev-dependencies]
assert_cmd = "1.0.3"
anyhow = "1.0.38"
anyhow = "1.0.40"

[build-dependencies]
structopt = "0.3.21"
Expand Down
4 changes: 0 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ pub(crate) struct Options {
/// Treat expressions as non-regex strings.
pub literal_mode: bool,

#[structopt(short = "r")]
/// Recursively replace files
pub recursive: bool,

#[structopt(short = "n")]
/// Limit the number of replacements
pub replacements: Option<usize>,
Expand Down
25 changes: 17 additions & 8 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl App {
(Source::Files(paths), true) => {
let stdout = std::io::stdout();
let mut handle = stdout.lock();
let print_path = paths.len() > 1;
let separator = "─".repeat(72);

paths.iter().try_for_each(|path| {
if let Err(_) = Replacer::check_not_empty(File::open(path)?)
Expand All @@ -78,18 +78,27 @@ impl App {
}
let file =
unsafe { memmap::Mmap::map(&File::open(path)?)? };

if self.replacer.has_matches(&file) {
if print_path {
writeln!(
handle,
"----- FILE {} -----",
path.display()
)?;
if paths.len() > 1 {
ansi_term::Color::Blue
.paint(path.display().to_string().as_bytes())
.write_to(&mut handle)?;

handle.write(b"\n")?;
ansi_term::Color::Blue
.paint(separator.as_bytes())
.write_to(&mut handle)?;

handle.write(b"\n")?;
}

handle
.write_all(&self.replacer.replace_preview(&file))?;
writeln!(handle)?;

if paths.len() > 1 {
handle.write(b"\n")?;
}
}

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ fn main() -> Result<()> {
use structopt::StructOpt;
let options = cli::Options::from_args();

let source = if options.recursive {
Source::recursive()?
} else if options.files.len() > 0 {
let source = if options.files.len() > 0 {
Source::Files(options.files)
} else if atty::is(atty::Stream::Stdin) {
Source::recursive()?
} else {
Source::Stdin
};
Expand Down
Loading