ianthehenry / dotfiles

My dotfiles. For easy clonin'.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Double dash to create one liners

eralmansouri opened this issue · comments

Hello,

I'm trying to modify sd new to allow creating one-liner scripts quickly.

I've made this modification to new.sh:

#!/usr/bin/env bash

set -euo pipefail

script=$(sd which "$@")

mkdir -p "$(dirname "$script")"

count=0
for var in "$@"
do
    case "$var" in
    (--) break;;
    (*) (( count ++ ));;
    esac
done

INITIAL_SCRIPT=${@:(( count + 2 ))}

cat > "$script" <<EOF
#!/usr/bin/env bash

set -euo pipefail

$INITIAL_SCRIPT
EOF

chmod +x "$script"
sd edit "$@"

And which.sh

#!/usr/bin/env bash

set -euo pipefail

root=${SD_ROOT:-$HOME/sd}

count=0
for var in "$@"
do
    case "$var" in
    (--) break;;
    (*) (( count ++ ));;
    esac
done

script=$(echo "${@:1:$count}" | tr ' ' /)
echo "$root/$script"

I'm not very good with this so I'd love any feedback you might have. I wanted to create a pull request but apparently I can't do that without a fork. Hopefully this is okay.

Oh hi! Someone is actually using sd. Neat.

Umm so I actually just split sd into its own repo, which I should have done forever ago, but didn't until just now. I have no excuse. But take a look here:

https://github.com/ianthehenry/sd/

I went ahead and implemented the double-dash escape there, too:

ianthehenry/sd@4b33e67

The approach I took was building up the command array separately and shifting as way to basically split $@ on --. That way there's no need to modify the which script, which feels better to me. It looks like a bigger diff than yours, but it does a few more things as well. I also chose not to call edit if you already supplied a body with --. If you feel that was a mistake, let me know.

Oh. That's pretty cool. I'll go ahead and start using that instead. Thank you. FYI I'm not the only one, I personally convinced a couple of coworkers to use it too. In the spirit of thanksgiving I've sent you a little token of gratitude to your e-mail.

Oh, well thank you very much! That's very unexpected and much appreciated.