deadpixi / sam

An updated version of the sam text editor.
Other
430 stars 46 forks source link

Add ssam script #104

Open TobiasKarnat opened 4 years ago

TobiasKarnat commented 4 years ago

Please add the ssam script as in the plan9port: https://9fans.github.io/plan9port/man/man1/ssam.html https://github.com/9fans/plan9port/blob/master/bin/ssam

I did a quick implementation in bash:

#!/bin/bash
if [[ ( -t 0 && "$#" -ge 2 ) || (! -t 0 && "$#" -eq 1) ]]; then
    cmd=$1; shift
    tmp=/tmp/ssam.tmp.$USER.$$
    cat $* > $tmp
else
    echo "Usage: ssam SCRIPT [FILE]..."
    exit
fi

{
    # select entire file
    echo ',{'
    echo k
    echo '}'
    echo 0k

    # sam commands
    [ -f $cmd ] && cat $cmd || echo $cmd
} | LANG=C.UTF-8 sam -d $tmp 2>/dev/null

rm -f $tmp
arizvisa commented 4 years ago

Added one that uses the same parameters as ssam from plan9ports, but it uses getopts which can probably be written out (but i need my quotez!). Can probably also use trap to ensure the file gets rm'd...

#!/bin/sh
arg0=`basename "$0"`

usage()
{
    printf "usage: %s [-n] [-e script] [-f sfile] [file ...]\n" "$1"
}

flagn=0
flage=""
flagf=""

while getopts ne:f:h OPT; do
    case "$OPT" in
        n)
            flagn=1
            ;;

        e)
            if [ -z "$OPTARG" ]; then
                usage "$arg0"
                exit 1
            fi
            flage="$OPTARG"
            ;;
        f)
            if [ -z "$OPTARG" ]; then
                usage "$arg0"
                exit 1
            fi
            flagf="$OPTARG"
            ;;
        h)
            usage "$arg0"
            exit 0
            ;;
    esac
done
shift `expr "$OPTIND" - 1`

if [ -z "$TMPDIR" ]; then
    TMPDIR="/tmp"
fi
tmp="$TMPDIR/ssam.tmp.$USER.$$"

cat "$@" >$tmp

input()
{
    # select entire file
    echo ',{'
    echo k
    echo '}'
    echo 0k

    # run scripts, print
    [ ! -z "$flagf" ] && cat "$flagf"
    [ ! -z "$flage" ] && echo "$flage"
    [ "$flagn" -eq 0 ] && echo ','
}

input | sam -d "$tmp" 2>/dev/null

rm -f "$tmp"
deadpixi commented 4 years ago

Hey Ali!

Added your ssam to the repo; let me know if it looks all right.

arizvisa commented 4 years ago

Hah! Heyo good sir.

Actually, let me tweak it a bit and submit you a PR. The only issue is that the "$tmp" file isn't guaranteed to be removed if some signal hits the shellscript which we can trap to do. I tried to keep its appearance exact same as the plan9ports' version, but rc has some implicit guarantees that bourne requires you to be explicit with.

arizvisa commented 4 years ago

Okay, sir. PR #112 handles those issues that I mentioned.

Thx for maintaining this btw, I'm trying to convince my coworkers how fucking awesome sam is. So I'm trying to get them to drop sed/awk for it (hence the need for ssam).

klpn commented 4 years ago

make install now complains about missing destination file operand for cp after ssam. The problem seems to be that the makefile now refers the BINDIR variable, which is defined in config.mk and not included in the top makefile.

klpn commented 4 years ago

I note that a ssam makefile has been added in e0a526e; I guess the top makefile should be updated with a ssamdir target and the additions to that file in 1dcf1de should be removed?