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
#!/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 / 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"
@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 / ts
Last active September 13, 2021 10:14
{
# find which repositories have changes, input: lib local_commit repo_commit
while IFS=$'\t' read -r -a revs ; do [[ "${revs[1]}" == "${revs[2]}" ]] || changed[${revs[0]}]=1 ; done < revisions
# find what test they trigger, input: repo test1 test2 ... and tests list
while IFS=$'\t' read -r -a deps ; do ((changed[${deps[0]}])) && { printf "%s\n" "${deps[@]:1}" | grep -f tests ; } ; done < dependencies
# add previously failed tests, input: faile test1 tes2 ...
awk '{ OFS="\n" ; /failed/ ; $1="" }1' < status
} | sed '/^$/d' | sort -u
@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
use Data::TreeDumper ;
@a = (1, 3, 5, 6, 7, 8 );
@b = ( 2, 3, 5, 7, 9);
# one loop
@union = @isect = @diff = ();
%union = %isect = %diff = ();
foreach $e (@a, @b)