Skip to content

Instantly share code, notes, and snippets.

View nkh's full-sized avatar
💭
I may be slow to respond.

Nadim Khemir nkh

💭
I may be slow to respond.
View GitHub Profile
@nkh
nkh / tmsu_scim
Created October 25, 2022 12:31
tmsu sc-im interface for bash
L=({A..Z})
ToAA() { AA= ; local i=$1 ; (($i == 0)) && AA='@' ; while ((i)) ; do AA="${L[((--i % 26))]}$AA" ; ((i /= 26)) ; done ; }
st()
{
cat <<EOF
color "type=DEFAULT fg=WHITE bg=DEFAULT_COLOR"
color "type=HEADINGS fg=CYAN bg=BLACK bold=0"
color "type=HEADINGS_ODD fg=CYAN bg=BLACK bold=0"
#!/bin/bash
file_tree()
{
pid="$1"
indentation=$(printf "%${indent}s")
[[ "$$" -eq "$pid" ]] && printf "$yellow"
echo "$indentation"$(ps -o pid= -o cmd= -p $pid | perl -pe 's/^\s+// ; s/\s+/,/')
#!/usr/bin/env bash
declare -A git_status
while IFS= read -r -d $'\0' line ; do git_status[${line:3}]="${line:0:2}" ; done < <(git status $@ --porcelain -z)
while IFS= read line ; do [ ! "${git_status[$line]}" ] && git_status[$line]=" " ; done
for file in "${!git_status[@]}" ; do
echo "$file" >&3
echo "${git_status[$file]}" >&4
#!/usr/bin/env bash
git_tree_status_entries=$(mktemp -p /tmp/$USER/ git_tree_status_entries_XXXXXXXX)
git_tree_status_status=$(mktemp -p /tmp/$USER/ git_tree_status_status_XXXXXXXX)
# find, output must be like git-status, IE: no . at beginning
# git ls-files -z | tr '\0' '\n'
# git status --porcelain -z | tr '\0' '\n' | cut -c4-
[[ "$1" == '--ignored' ]] && extra_options=" --ignored"
@nkh
nkh / 0_n_0
Created June 21, 2023 12:21
bash read loop and zero-ing scrip
#!/bin/bash
[[ $1 == -0 ]] && { shift ; while read -d $'\0' -r I ; do eval "$1" ; done ; } || { while read -r I ; do eval "$1" ; done ; }
#!/usr/bin/env -S perl -p
chomp ; $_.="\0"
# example where everything is zero ended
find -size +5M -mtime -700 -not -path "*/video_backup/*" -group nadim -print0 | \
perl -0ne 'chomp ; !-e "$_.sha256" && print "$_\n"' | +0 | \
@nkh
nkh / option_pipe
Last active June 14, 2023 18:15
how to dynamically create a pipeline that handles options
#!/bin/bash
echo process a given input depending on options in a pipeline
echo --------------------------------------------------------------------------------
echo
inputs=("2 xxx" "1 hi" "3 abc")
# options to sort or limit the number of answers
opt_sort=1
package AI::Pathfinding::AStar::Test;
use base AI::Pathfinding::AStar;
use strict ;
use warnings ;
use feature qw/say/ ;
sub new
{
my $invocant = shift;
@nkh
nkh / get_key
Created September 20, 2022 13:14
Bash code to handle keyboard input.
#!/bin/bash
get_key()
{
OIFS="$IFS" ; IFS=
[[ "$1" ]] && { read -rsn 1 -t "$1" || REPLY=ERROR_$? ; } || { read -rsn 1 || REPLY=ERROR_$? ; }
read -rsn 4 -t 0.004 E1 E2 E3 E4
case "$REPLY$E1$E2$E3$E4" in
@nkh
nkh / bff.sh
Last active June 13, 2022 18:25
#!/bin/env bash
bff() # Inspired by fff. Run: bbf 3> some_file, to get exit-time selection
{
shopt -s checkwinsize ; trap 'rdir' SIGWINCH ;
declare -A dir_file filters marks tags ; show_dirs=1 ;kolumn=1
local BFF=(/ /home/nadim /home/nadim/nadim /home/nadim/nadim/downloads)
pushd "$1" &>/dev/null ; tabs+=("$PWD") ; tab=0 ; rdir
@nkh
nkh / color_pipe
Created September 29, 2016 20:24
put colors in pipes
#!/usr/bin/env perl
use strict ;
use warnings ;
use Term::ANSIColor ;
if (grep {/^--help/} @ARGV)
{
print <<'EOH' ;
Usage: $> command | color_pipe regex color [regex color ...] | command ...