~strahinja/dotfiles

45693dca8b930b653fd395faa56d09df2e6874db — Страхиња Радић 13 days ago ae67bf4
Add support for TSV files; bin/tsv*: New file

Signed-off-by: Страхиња Радић <contact@strahinja.org>
9 files changed, 149 insertions(+), 12 deletions(-)

M .vimrc
M bin/csvins
M bin/text.sh
A bin/tsvdel
A bin/tsvedit
A bin/tsvins
M ranger/rifle.conf
M ranger/scope.sh
M vifm/vifmrc
M .vimrc => .vimrc +0 -1
@@ 45,4 45,3 @@ nnoremap <leader><Space> :nohlsearch<CR>
autocmd BufRead,BufNewFile *.pas set filetype=pascal
autocmd BufRead,BufNewFile *.PAS set filetype=pascal
autocmd BufRead,BufNewFile *.slw set filetype=markdown


M bin/csvins => bin/csvins +4 -7
@@ 33,17 33,14 @@ cp "$1" "$tmpf"
trap "rm -f \"$tmpf\"" HUP PIPE INT QUIT TERM EXIT

if [ $# -lt 2 ]; then
	sed -i -z 's/\(.*\)\n$/\1/' "$tmpf"
	vipe --suffix csv </dev/null | \
		sed -z -e's/\([^\n]*\)\n\+$/\"\1\"/g' | \
		sed -z -e"s/\\([^\\n]*\\)\\n/\\\"\\1\\\"$delim/g" >> "$tmpf"
	echo >> "$tmpf"
		sed -z -e"s/\\([^\\n]*\\)\\n/\\\"\\1\\\"$delim/g" | \
		sed -z -e"s/$delim\$/\\n/g" >> "$tmpf"
else
	head -n$(($2)) "$1" > "$tmpf"
	vipe --suffix csv </dev/null | \
		sed -z -e's/\([^\n]*\)\n\+$/\"\1\"/g' | \
		sed -z -e"s/\\([^\\n]*\\)\\n/\\\"\\1\\\"$delim/g" >> "$tmpf"
		echo >> "$tmpf"
		sed -z -e"s/\\([^\\n]*\\)\\n/\\\"\\1\\\"$delim/g" | \
		sed -z -e"s/$delim\$/\\n/g" >> "$tmpf"
	tail +$(($2+1)) "$1" >> "$tmpf"
fi


M bin/text.sh => bin/text.sh +4 -1
@@ 49,7 49,10 @@ do_view_action() {

    case "${filetype}" in
    csv)
        table -m -n -c 160 "${MC_EXT_FILENAME}"
        table -m -n -c 100 "${MC_EXT_FILENAME}"
        ;;
    tsv)
        table -d $'\t' -m -n -c 100 "${MC_EXT_FILENAME}"
        ;;
    md)
        markdown < "${MC_EXT_FILENAME}" | lynx -dump -raw -stdin

A bin/tsvdel => bin/tsvdel +37 -0
@@ 0,0 1,37 @@
#!/bin/sh

usage()
{
	cat <<EOT
Usage: $prog [tsvfile.tsv] [colno] [text]
EOT
}

error()
{
	status=$1
	shift
	echo $prog: $* >&2
	exit $status
}

if [ $# -lt 3 ]; then
	usage
	exit 1
fi

prog=$(echo $0 | sed 's@.*/\([^/]\+\)$@\1@g')
tmpf=$1~

[ -w "$1" ] || error 1 \`$1\' not writeable

cp "$1" "$tmpf"
trap "rm -f \"$tmpf\"" HUP PIPE INT QUIT TERM EXIT
lineno=$(cut -d\t -f$2 "$1" | grep -ni "$3" | sed 's/^\([0-9]\+\).*/\1/;1q')
[ -z "$lineno" ] && error 1 not found \`$3\' in column $2

head -n$((lineno-1)) "$1" > "$tmpf"
tail +$((lineno+1)) "$1" >> "$tmpf"

mv "$tmpf" "$1"


A bin/tsvedit => bin/tsvedit +44 -0
@@ 0,0 1,44 @@
#!/bin/sh

# Needs vipe from moreutils and $EDITOR https://joeyh.name/code/moreutils/

usage()
{
	cat <<EOT
Usage: $prog [tsvfile.tsv] [colno] [text]
EOT
}

error()
{
	status=$1
	shift
	echo $prog: $* >&2
	exit $status
}

if [ $# -lt 3 ]; then
	usage
	exit 1
fi

prog=$(echo $0 | sed 's@.*/\([^/]\+\)$@\1@g')
tmpf=$1~

[ -w "$1" ] || error 1 \`$1\' not writeable
which vipe &>/dev/null || error 1 vipe not found

cp "$1" "$tmpf"
trap "rm -f \"$tmpf\"" HUP PIPE INT QUIT TERM EXIT
lineno=$(cut -d\t -f$2 "$1" | grep -ni "$3" | sed 's/^\([0-9]\+\).*/\1/;1q')
[ -z "$lineno" ] && error 1 not found \`$3\' in column $2

head -n$((lineno-1)) "$1" > "$tmpf"
head -n$lineno "$1" | \
	tail -n1 | sed 's/\([^\t]\+\)\t/\1\n/g' | vipe | \
	sed -z -e's/\([^\n\t]*\)\n/\1\t/g' | \
	sed -z -e's/\t$/\n/g' >> "$tmpf"
tail +$((lineno+1)) "$1" >> "$tmpf"

mv "$tmpf" "$1"


A bin/tsvins => bin/tsvins +48 -0
@@ 0,0 1,48 @@
#!/bin/sh

# Needs vipe from moreutils and $EDITOR https://joeyh.name/code/moreutils/

usage()
{
	cat <<EOT
Usage: $prog [tsvfile.tsv] [lineno]
EOT
}

error()
{
	status=$1
	shift
	echo $prog: $* >&2
	exit $status
}

if [ $# -lt 1 -o $# -gt 2 ]; then
	usage
	exit 1
fi

prog=$(echo $0 | sed 's@.*/\([^/]\+\)$@\1@g')
tmpf=$1~

which vipe &>/dev/null || error 1 vipe not found

touch "$1"
cp "$1" "$tmpf"
trap "rm -f \"$tmpf\"" HUP PIPE INT QUIT TERM EXIT

if [ $# -lt 2 ]; then
	vipe --suffix tsv </dev/null | \
		sed -z -e's/\([^\n\t]*\)\n/\1\t/g' | \
		sed -z -e's/\t$/\n/g' >> "$tmpf"
	echo >> "$tmpf"
else
	head -n$(($2)) "$1" > "$tmpf"
	vipe --suffix csv </dev/null | \
		sed -z -e's/\([^\n\t]*\)\n/\1\t/g' | \
		sed -z -e's/\t$/\n/g' >> "$tmpf"
	tail +$(($2+1)) "$1" >> "$tmpf"
fi

mv "$tmpf" "$1"


M ranger/rifle.conf => ranger/rifle.conf +2 -2
@@ 87,8 87,8 @@ ext x?html?, has w3m,               terminal = w3m "$@"
# Define the "editor" for text files as first action
mime ^text,  label editor = ${VISUAL:-$EDITOR} -- "$@"
mime ^text,  label pager  = "$PAGER" -- "$@"
!mime ^text, label editor, ext xml|json|csv|tex|py|pl|rb|js|sh|php = ${VISUAL:-$EDITOR} -- "$@"
!mime ^text, label pager,  ext xml|json|csv|tex|py|pl|rb|js|sh|php = "$PAGER" -- "$@"
!mime ^text, label editor, ext xml|json|csv|tsv|tex|py|pl|rb|js|sh|php = ${VISUAL:-$EDITOR} -- "$@"
!mime ^text, label pager,  ext xml|json|csv|tsv|tex|py|pl|rb|js|sh|php = "$PAGER" -- "$@"

ext 1                         = man "$1"
ext s[wmf]c, has zsnes, X     = zsnes "$1"

M ranger/scope.sh => ranger/scope.sh +6 -1
@@ 109,7 109,7 @@ handle_extension() {
        xlsx)
            ## Preview as csv conversion
            ## Uses: https://github.com/dilshod/xlsx2csv
            xlsx2csv -- "${FILE_PATH}" && exit 5
            xlsx2csv -- "${FILE_PATH}" | table -m && exit 5
            exit 1;;

#	## Sqlite


@@ 122,6 122,11 @@ handle_extension() {
	    table -m "${FILE_PATH}" && exit 5
	    exit 1;;

	## TSV
	tsv)
	    table -d $'\t' -m "${FILE_PATH}" && exit 5
	    exit 1;;

        ## HTML
        htm|html|xhtml)
            ## Preview as text conversion

M vifm/vifmrc => vifm/vifmrc +4 -0
@@ 202,6 202,10 @@ fileviewer {*.flac},<audio/flac> soxi
fileviewer *.csv
	\ table -m %c

" TSV
fileviewer *.tsv
	\ table -d $'\t' -m %c

" Video
filextype {*.avi,*.mp4,*.wmv,*.dat,*.3gp,*.ogv,*.mkv,*.mpg,*.mpeg,*.vob,
          \*.fl[icv],*.m2v,*.mov,*.webm,*.ts,*.mts,*.m4v,*.r[am],*.qt,*.divx,