sd icon indicating copy to clipboard operation
sd copied to clipboard

Preview mode

Open ricochet1k opened this issue 6 years ago • 8 comments

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

ricochet1k avatar Apr 25 '19 19:04 ricochet1k

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?

antonholmberg avatar Apr 25 '19 22:04 antonholmberg

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.

ricochet1k avatar Apr 27 '19 22:04 ricochet1k

@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.

antonholmberg avatar Apr 29 '19 06:04 antonholmberg

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

chmln avatar May 03 '19 19:05 chmln

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

antonholmberg avatar May 06 '19 07:05 antonholmberg

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.

ismay avatar Jul 22 '19 14:07 ismay

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

sergeevabc avatar Apr 02 '21 01:04 sergeevabc

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...

alecandido avatar Nov 16 '22 13:11 alecandido