chmln / sd

Intuitive find & replace CLI (sed alternative)
MIT License
5.86k stars 138 forks source link

Preview mode #31

Open ricochet1k opened 5 years ago

ricochet1k commented 5 years ago

I would love a better way to preview changes, something more than just not using the -i flag.

Use case: sd 'find' 'replace' $(fd -e rs)

This just dumps all of my files un-colored to the terminal which isn't helpful.

I propose there be a -p flag for preview, which only shows lines that match along with file and some short amount of context. Like what grep -C3 prints out. For each of the lines that match, show the matched part in red, and the replacement right next to it in green, inline. Or maybe use strikethrough and bold, although those are less widely supported.

Bonus points if the styles are configurable somehow. Even more bonus points for coloring the output like bat

antonholmberg commented 5 years ago

I like this idea too. I'm just thinking about edge cases here.

How would you imagine:

sd -i -p

to work? Should it be in place and print the status afterwards or should -p override the -i? By order? Personally I would find it handy to actually print what it did in place too but that is just me.

What do you think?

ricochet1k commented 5 years ago

I imagine it would do exactly as you say, print out what it is replacing and also do the replacing. Thinking of it as "print" instead of "preview" makes more sense there.

antonholmberg commented 5 years ago

@ricochet1k Cool. I am not a rust expert but I think that this wouldn't be too hard to implement. If I find the time I will give it a shot.

chmln commented 5 years ago

@ricochet1k I like the idea. Feel free to make a PR @antonholmberg if you can implement this without adding too much code

antonholmberg commented 5 years ago

@chmln Will try. Time is always the issue though.

ismay commented 5 years ago

I'd like this as well. It'd make the changes easier to scan. As a partial example, the way ripgrep handles displaying matches file-by-file is nice:

Screenshot 2019-07-22 at 16 28 18

Though sd would also need to display the diff of course.

Bonus points if the styles are configurable somehow.

I'd say just use the terminal colors for red and green. Those are configurable and they'd match the terminal theme automatically.

sergeevabc commented 3 years ago

Replacing a couple of matches within 100 kB file. -p outputs everything, so changes are not noticable.

alecandido commented 1 year ago

I'd like this as well. It'd make the changes easier to scan. As a partial example, the way ripgrep handles displaying matches file-by-file is nice:

I would like this as everyone else here, however, in case you need a temporary solution, what I'm doing in Git repos is to stage/commit any other change, and then use git diff on the result.

It is definitely not the ideal solution, just a workaround, but still better than nothing...