Coder Social home page Coder Social logo

sad's Introduction

Space Age seD

What does it do?

Basically sad is a Batch File Edit tool.

It will show you a really nice diff of proposed changes before you commit them.

Unlike sed, you can double check before you fat finger your edit.

Preview (with fzf)

Selectively replace std -> joseph joestar in the sad repo.

You can pick and choose which changes to apply.

You can also choose the clustering factor for changes using --unified=<n>. (Same as in GNU diff)

preview1

If you have delta installed, try --pager 'delta -s' for side by side view

Preview (no fzf)

Replace all'"(\d+)"' -> '๐ŸŒˆ$1๐ŸŒˆ' in the chromium repo.

use --commit or -k to commit changes all at once.

-c is taken because sad has to trick fzf into thinking it's bash :)

preview2

How to use sad?

with fzf

export GIT_PAGER='<highlighter-of-your-choice>'
# ^ can be done in your bash/zsh/rc file.
find "$FIND_ARGS" | sad '<pattern>' '<replacement>'

without fzf

find "$FIND_ARGS" | sad '<pattern>' '<replacement>' | highlighter-of-your-choice

or

find "$FIND_ARGS" | sad '<pattern>' '<replacement>' --pager=<highlighter-of-your-choice>

or

export GIT_PAGER='<highlighter-of-your-choice>'
find "$FIND_ARGS" | sad '<pattern>' '<replacement>'

gotta go fast

If you wanna go fast.

  • preview to verify you really want the changes.

  • run with --commit, and redirect stdout to a file or /dev/null


Requirements

Technically none of these are "required", but they make sad so much happier.

If you install the things below, sad will automatically use them. It's progressive enhancement!

Commandline fuzzer

fzf

sad does not come with a UI, it uses fzf to perform selection.

Diff Colorizer

Any git compatible colourizer would work. I prefer these two:

delta

fd <files> | sad <pattern> <replacement> | delta

diff-so-fancy

fd <files> | sad <pattern> <replacement> | diff-so-fancy | less

Environmental Variables

Name Function
GIT_PAGER sad will use the same pager as git

Flags

Name Function
-f --flags Regex flags, see below
-k --commit No preview, write changes to file
-0 --read0 Use \x00 as stdin delimiter
-e --exact String literal mode
-p --pager Colourizing program, disable = never
--fzf Additional Fzf options, disable = never
-u --unified Same as in GNU diff, affects hunk size

Regex Flags

By default, sad uses smartcase, and multiline matching.

For each options, lowercase toggles on and uppercase toggles off.

ie. i => on, I => off

Name Function
i case insensitive (works for --exact mode as well)
m multiline: ^ $ match each line
s allow . match \n
u swap the meaning of * and *? patterns, (normally * is lazy and *? is greedy)
x ignore whitespace and allow # comments

Exit Codes

Code Meaning
0 Good
1 Bad
130 Interrupted (ie. user cancel), or if using fzf, it will always exit 130.

GET SAD NOW!

For scripting, use the following format:

# replace `x86_64-unknown-linux-gnu.deb` with your desired package
wget 'https://github.com/ms-jpq/sad/releases/latest/download/x86_64-unknown-linux-gnu.deb'

Homebrew:

brew install sad

Scoop:

scoop install sad

Snap Store:

coming soon...

Distribution packages:

Debian/Ubuntu:

You can download sad deb packages from the github release page.

Arch Linux:

There is an official Arch Linux package that can be installed via pacman:

pacman -Syu sad
Other:

Missing a package for your favourite distribution? Let us know!

Compile from source:

Requirements:

To compile sad yourself you'll have to make sure you have Rust and cargo installed.

Install instructions:

To install cargo from source you can run the following commands:

cargo install --locked --all-features \
  --git https://github.com/ms-jpq/sad --branch senpai

If you want to install it in a specific directory you can provide the --root flag, like so:

cargo install --locked --all-features --root="/usr/bin/" \
  --git https://github.com/ms-jpq/sad --branch senpai

What about stdin -> stdout

If you just want to edit the shell stream, I would recommend sd, it uses the same concept, but its more for in stream edits. sad was inspired by my initial usage of sd.

command1 | sd '<pattern>' '<replacement>' | command2

ripgrep with --replace also works

command1 | rg --passthru --replace '<replacement>' -- '<pattern>' | command2

Take note however, rg will exit 1, it it finds no matches.

Thank yous

Special thanks to MadeOfMagicAndWires for their generous contribution for maintaining the AUR package.

Bugs

Please file an issue if you see one <3

sad's People

Contributors

0xflotus avatar alerque avatar aliesbelik avatar chenrui333 avatar dependabot[bot] avatar gelio avatar grawlinson avatar kianmeng avatar madeofmagicandwires avatar ms-jpq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sad's Issues

How do you pass arguments to fzf

Hi,

I see in the manual you can use --fzf to pass params to fzf.

I can't get this to work.

These are the commands I've been trying to run:

With double quotes

โฏ rg smrc --files-with-matches | sad 'smrc' 'smc' --fzf "--bind ctrl-a:select-all"
error: unexpected argument '--bind ctrl-a:select-all' found

  tip: to pass '--bind ctrl-a:select-all' as a value, use '-- --bind ctrl-a:select-all'

Usage: sad <PATTERN|REPLACE|--read0|--commit|--exact|--flags <FLAGS>|--pager <PAGER>|--fzf <FZF>|--unified <UNIFIED>>

For more information, try '--help'.

No dashes before bind

 rg smrc --files-with-matches | sad 'smrc' 'smc' --fzf "bind ctrl-a:select-all"
unknown option: bind
Error:
IO(
    "/usr/bin/fzf",
    BrokenPipe,
)
Error:
BadExit(
    "/usr/bin/fzf",
    2,
)

No double quotes

โฏ rg smrc --files-with-matches | sad 'smrc' 'smc' --fzf --bind=ctrl-a:select-all
error: unexpected argument '--bind' found

  tip: to pass '--bind' as a value, use '-- --bind'

Usage: sad <PATTERN|REPLACE|--read0|--commit|--exact|--flags <FLAGS>|--pager <PAGER>|--fzf <FZF>|--unified <UNIFIED>>

For more information, try '--help'.

Any ideas what's going on?

Looks like it should work reading the source code. Something that has me worrying about bind specifically is the function you run around the --bind args in the code though.

sad/src/fzf.rs

Line 142 in a0dc8b3

format!("--bind=enter:{execute}"),

Thanks!

I can't figure out how to install for linux

I am new to neovim and I am using WSL linux. I only know how to install via apt or npm and I don't know what folder I would need to install sad in. Can someone help me with the steps I need to take to install sad I am installing it so I can use the x-ray find and replace package.

0.4.23: Build fails with NotFound, message: "No such file or directory"

     Running `/usr/ports/textproc/sad/work/target/release/build/sad-d56d8c2582aab3f5/build-script-build`
[sad 0.4.23] Error: Os { code: 2, kind: NotFound, message: "No such file or directory" }
error: failed to run custom build command for `sad v0.4.23 (/usr/ports/textproc/sad/work/sad-0.4.23)`

Caused by:
  process didn't exit successfully: `/usr/ports/textproc/sad/work/target/release/build/sad-d56d8c2582aab3f5/build-script-build` (exit status: 1)
  --- stderr
  Error: Os { code: 2, kind: NotFound, message: "No such file or directory" }
warning: build failed, waiting for other jobs to finish...
warning: variable does not need to be mutable
   --> /usr/ports/textproc/sad/work/sad-0.4.23/cargo-crates/which-4.4.0/src/lib.rs:308:18
    |
308 |     pub fn regex(mut self, regex: Regex) -> Self {
    |                  ----^^^^
    |                  |
    |                  help: remove this `mut`
    |
    = note: `#[warn(unused_mut)]` on by default

warning: `which` (lib) generated 1 warning
*** Error code 101

rust-1.69.0
FreeBSD 13.2

I added tab completions for fish shell

Hi, cool project!
I added tab completions for fish shell, here they are:

complete sad    -s 0 -l read0                    -d 'Use \0 as stdin delimiter'
complete sad    -s e -l exact                    -d 'String literal mode'
complete sad -x -s f -l flags                    -d "Regex flags: use `--help` instead of `-h` to see details"
complete sad -x      -l fzf     -a "never"       -d "Additional Fzf options, disable = never"
complete sad    -s h -l help                     -d 'Print help information'
complete sad    -s k -l commit                   -d 'No preview, write changes to file'
complete sad -x -s p -l pager   -a "never"       -d 'Colourizing program, disable = never, default = $GIT_PAGER'
complete sad -x -s u -l unified -a "0 1 2 3 4 5" -d 'Same as in GNU diff --unified={size}, affects aggregate size'
complete sad -x -s V -l version                  -d 'Print version information'

Do you want to bundle them with the project, or would you like me to create a PR to have them included in the fish shell repo instead?

Easy installation via crates.io

After futzing around for a little bit, I determined that the easiest way right now to install this package is to clone it, check out the latest release tag, and then run the cargo install command described in the readme. I actually ran through this twice because I also use ASDF and I wanted to use the same version of rust that you used to build sad so I could install from a clean repo before trying again with the version of rust I generally use as my toolchain in a global scope (1.49.0).

A couple of things I also wanted to check on before I just opened an issue on someone else's repo. I wanted to get a feel for how open to contributions you are and if there's anything that you'd rather someone take a look at first than publishing to crates.io. I generally try to open issues to see how open an author is to them (unless they're bugs) so that I can contribute.

The other option that can be floated if crates.io is an absolute no go is to update the README to include steps for checking out the latest tagged version

Build fails on Windows

Currently trying to build on Windows fails with the following errors:

error[E0433]: failed to resolve: could not find `unix` in `os`
  --> src\input.rs:14:7
   |
14 |   os::unix::ffi::OsStringExt,
   |       ^^^^ could not find `unix` in `os`

error[E0599]: no function or associated item named `from_vec` found for struct `OsString` in the current scope
   --> src\input.rs:165:48
    |
165 |             let path = PathBuf::from(OsString::from_vec(buf));
    |                                                ^^^^^^^^ function or associated item not found in `OsString`

Building from source with:

cargo install --locked --all-features --git https://github.com/ms-jpq/sad --branch senpai

Missing License

Hey there,

I was thinking of creating an AUR package for this but I see that you have not specified a software license yet.

Could you let me know if I could package this and if so, under what license?

(You would probably have to provide it with your project as well)

Cheers

How to set delta theme

I'm struggling to find a way to set the delta theme. I have two different delta themes for light and dark mode.
My git config looks like this:

[core]
    editor = nvim
    pager = delta --features "$(defaults read -globalDomain AppleInterfaceStyle &> /dev/null && echo dark-mode || echo light-mode)"

[delta "dark-mode"]
    dark = true
    syntax-theme = bluloco-dark
    ; additional theme stuff below
    ; ...
    ; ...

[delta "light-mode"]
    light = true
    syntax-theme = bluloco-light
    ; additional theme stuff below
    ; ...
    ; ...

My delta theme is set correctly in git but I don't know how to theme the output of sad.
Is there any way to do this right now?

Snapstore

need to figure out snap store permissions

Use skim instead of fzf

Can you add support or a config option to add the rust implemented fuzzy finder "skim"? I believe it uses the same flags and syntax.

Lockfile out of sync

Building with cargo build --locked fails. Seems like the lockfile has somehow managed to get out of sync.

no behavior at all on windows?

Doing this exact series of commands (or any commands at all) does not work on windows. I'm sure sad is functional (--version, --help both work), fzf and delta are both installed as well. But no actual sad (haha) commands work, just print \n and exit. Is this me being dumb??

Plain text, tex file as an example:
image

os + shell
Nu โฏ sys | get host:
Windows - 10 (19043) | using nushell 0.38 here, but powershell gives similarly depressing results

Package for Void-Linux

Took the freedom to create a PR over at void-packages

If it gets merged, you can add xbps-install -S sad to your README to install it on void-linux systems ;).

Missing Cargo.lock file

Hey there, it's me again.

This is not required to create a package, but using a Cargo.lock file over a regular Cargo.toml file helps creating reproducible builds of packages.

I've never used CI myself but I assume you can easily use it there as well.

Failed to work on linux /dev/stdin error

It seems not working on linux (archlinux)
I tried the latest version and release version. Both have the following errors:

Error:
ArgumentError(
    "/dev/stdin connected to tty",
)

IO error `NotFound` when using `fzf`

Thanks for this package, it's awesome!

I'm unable to use sad with the fzf preview window, I always get an error like so:

image

Here's what my env looks like for fzf:

~/dotfiles debian-buster *% [1]  env | rg fzf
FZF_COMPLETION_OPTS=-i
FZF_DEFAULT_COMMAND=bash -c "fdfind --type file --follow . \$(git rev-parse --show-cdup 2>/dev/null && echo --hidden)"
FZF_CTRL_T_COMMAND=fd_with_git --color always
FZF_CTRL_T_OPTS=--ansi --preview-window 'right:50%' --preview 'bat --color always --style=grid,changes --line-range :300 {}'
FZF_DEFAULT_OPTS=--height 60% --extended --bind ctrl-alt-a:select-all,ctrl-alt-d:deselect-all,F1:toggle-preview

fzf always exits with code 130

the only way to exit fzf while executing sad is using abort, however this always results in fzf exiting with code 130.

with a stateless architecture, i don't have a good way to know if fzf exiting with 130 is from user interrupts or from intended action.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.