Nimbin[12]?Slackware / slackpkgExtend_dialog

slackpkgExtend_dialog Slackware

With this script you can search for installed packages.
It is an interactive script where you can select packages with "space".
With "arrow_right" you can get the information of a pkg.
The script will print the exact pkg name after pressing "Return".

i should checkout "whiptail" to improve this script

wget https://git.christianimmanuel.de/scripts/raw/Slackware/slackpkgExtend_dialog && chmod 740 slackpkgExtend_dialog
slackpkgExtend_dialog 6.1 KB · 299 lines raw
#!/bin/bash

#################################################
### With this script you can search for installed packages.
### It is an interactive script where you can select packages with "space".
### With "arrow_right" you can get the information of a pkg.
### The script will print the exact pkg name after pressing "Return".
###
### i should checkout "whiptail" to improve this script
clear -x
trap quit SIGINT
trap quit SIGTERM

slack() {
   if [[ -n $(whereis slackpkgExtend | awk '{print $2}') ]]; then
      slackpkgExtend $@
   else
      slackpkg $@
   fi
}

#stty -echo


quit() {
#	stty echo 
   clear
   echo "EXIT"
	exit 0
}

getSearch() {
	ALL="/var/log/packages"

	PARAMETER="$@"

	LISTV=""
	if [[ -n "$PARAMETER" ]]; then
		for name in $PARAMETER; do
			nameV="$(ls "$ALL" | grep "$name")"
			LISTV="$LISTV $nameV"
		done
	else
		LISTV="$(ls "$ALL")"
	fi
	LIST=""
	for nameV in $LISTV; do
		NAME="$(cat $ALL/$nameV | grep -A1 "^PACKAGE DESCRIPTION:" | tail -n1 | awk '{print $1}' | sed "s/:$//g")"
		LIST="$LIST $NAME"
	done

	LIST="$(echo "$LIST" | xargs | tr ' ' '\n' | sort)"
	echo "$LIST"
}
getPkgs() {
   names="$@"
   for name in ${names}; do
		pkg="$(cat "/var/lib/slackpkg/pkglist" | awk '{print $2 " " $6}' | grep "^$name " | head -n1 )"
		echo $pkg | awk '{print $2}'
	done
}

MAIN_LIST="$(getSearch $@)"

LIST="$MAIN_LIST"
LIST_LENGTH="$(echo "$LIST" | wc -w)"




DESCRIPTION=" "
DESCRIPTION_LINES="0"
MAX_LIST_X="$(tput cols)"
MAX_LIST_Y="$(tput lines)"
setMaxList() {
	MAX_LIST_Y_NEW="$(($(tput lines)-$DESCRIPTION_LINES))"
	[[ "$LIST_LENGTH" -lt "$MAX_LIST_Y_NEW" ]] && MAX_LIST_Y_NEW="$((LIST_LENGTH+1))"
	MAX_LIST_X="$(($(tput cols)/2))"
}
setCursor() {
	CURSOR_POSITION="$(($MAX_LIST_Y_NEW-($MAX_LIST_Y-CURSOR_POSITION)))"
	[[ $CURSOR_POSITION -le "0" ]] && CURSOR_POSITION="1"
	[[ "$FOCUS_ID" -lt "$CURSOR_POSITION" ]] && CURSOR_POSITION="$FOCUS_ID"
}
setOutputLayout() {
	setMaxList
	setCursor
	MAX_LIST_Y="$MAX_LIST_Y_NEW"
}
setOutputLayout

nthOfList() {
	echo $LIST | awk -v num=$1 '{print $num}'
}

selectName() {
	ID="$1"
	[[ "$ID" -ge "0" ]] && nthOfList "$ID"
}



FOCUS_ID="1"
FOCUS_NAME="$(selectName "$FOCUS_ID")"
CURSOR_POSITION="1"

SELECTED_LIST=" "

toggleSelectedList() {
	item="$1"
	if echo $SELECTED_LIST | grep -qw "$item"; then
		SELECTED_LIST="$(echo "$SELECTED_LIST" | sed "s/ $item //g")"
	else
		SELECTED_LIST="$SELECTED_LIST $item " 
	fi
	reprintFocus &
}

echoLine() {
	position="$1"
	TEXT='\033[00;37m'
	GREEN='\033[00;32m'
	BLUE='\033[01;34m'
   NC='\033[0m'
	input="$3"
	color="$GREEN"
	[[ "$2" = "text" ]] && color=$TEXT
	[[ "$2" = "default" ]] && color=$GREEN
	[[ "$2" = "focus" ]] && color=$BLUE

	inputLength="${#input}"
	spacesLength="$(($(tput cols)-$inputLength))"
	spaces="$(seq -s" " $spacesLength |tr -d '[:digit:]')"
   tput cup $position 0
   echo -en "$color$input$spaces$NC"
}

stateOf() {
	item="$1"
	if echo $SELECTED_LIST | grep -wq "$item"; then
		echo "*"
	else
		echo " "
	fi
}

reprintListPrev() {
	for prev in $(seq $((CURSOR_POSITION))); do
		ID="$(($FOCUS_ID-$prev+1))"
		NAME=$(selectName $ID)
		[[ "$ID" -gt "0" ]] && (
			[[ "$ID" -lt "$FOCUS_ID" ]] && 
				echoLine "$(($CURSOR_POSITION-prev+1))" "default" " [$(stateOf $ID)] $NAME"
		)
	done
}
reprintListNext() {
	for next in $(seq $(($MAX_LIST_Y - 1 - CURSOR_POSITION))); do
		ID="$(($FOCUS_ID+$next))"
		NAME=$(selectName $ID)
      echoLine "$(($CURSOR_POSITION+next))" "default" " [$(stateOf $ID)] $NAME"
	done
}
reprintFocus() {
	ID="$FOCUS_ID"
	echoLine "$CURSOR_POSITION" "focus" " [$(stateOf $ID)] $FOCUS_NAME"
}
reprintSpace() {
   [[ -n $DESCRIPTION ]] &&
      echoLine "$MAX_LIST_Y" "text" " "
      echoLine "0" "text" " "

}
reprintList() {
	reprintListPrev &
	reprintListNext &
	reprintFocus &
   reprintSpace &
}

printText() {
	c=0
	while read -r line; do
		c=$((c+1))
      position="$(($(tput lines)-$DESCRIPTION_LINES+c))"
		echoLine "$position" "text" " $line" &
	done < /tmp/slackdialog.log
	reprintList &
}
clearText() {
	c=0
	while read -r line; do
		c=$((c+1))
      position="$(($(tput lines)-$DESCRIPTION_LINES+c))"
		echoLine "$position" "text" " " &
	done < /tmp/slackdialog.log
	reprintList &
}



moveFocus_up() {
   FOCUS_ID="$((FOCUS_ID-1))"
   FOCUS_NAME="$(selectName "$FOCUS_ID")"
   [[ "$CURSOR_POSITION" -gt "1" ]] &&
      CURSOR_POSITION="$((CURSOR_POSITION-1))"
	reprintList &
}

moveFocus_down() {
   FOCUS_ID="$((FOCUS_ID+1))"
   FOCUS_NAME="$(selectName "$FOCUS_ID")"
   [[ "$CURSOR_POSITION" -lt "$((MAX_LIST_Y-1))" ]] &&
      CURSOR_POSITION="$((CURSOR_POSITION+1))"
	reprintList &
}

arrow_up() {
   [[ "$FOCUS_ID" -gt "1" ]] &&
      moveFocus_up
}

arrow_down() {
   [[ "$FOCUS_ID" -lt "$(($LIST_LENGTH))" ]] &&
      moveFocus_down
}

arrow_left() {
   clear -x
   DESCRIPTION=""
   DESCRIPTION_LINES="0"
	setOutputLayout
	reprintList &
}

arrow_right() {
   [[ -f /tmp/slackdialog.log ]] && clearText
   INFO="$(slack info $FOCUS_NAME)"
   echo "$FOCUS_NAME# $INFO" > /tmp/log.log
   if echo "$INFO" | grep -q "Get info of SBo package"; then
      sed '0,/Get info of SBo package/d;/^[[:blank:]]*$/ d' <<< "$INFO" > /tmp/slackdialog.log
   else
      grep "$FOCUS_NAME: " <<< "$INFO" | sed "s/$FOCUS_NAME: //g" > /tmp/slackdialog.log
   fi
	DESCRIPTION="$(cat /tmp/slackdialog.log)"
   DESCRIPTION_LINES="$(($(cat /tmp/slackdialog.log | wc -l)+1))"
	setOutputLayout
	printText "$DESCRIPTION"
}

writeSelected() {
	NAMES=""
	for ID in ${SELECTED_LIST}; do
		name="$(echo $LIST | awk -v id=$ID '{print $id}')"
		NAMES="$NAMES $name"
	done
	OUTPUT="$(getPkgs $NAMES)"
   clear
	echo "${OUTPUT//$'\n'/ }"
#	stty echo
	exit 0
}

readInput() {
	escape_char=$(printf "\u1b")
	OLDIFS="$IFS"
	IFS= 
	read -rsn1 mode
	if [[ $mode == $escape_char ]]; then
		read -rsn2 mode # read 2 more chars
	elif echo "$mode" | grep -q "^ "; then
		mode="$mode"
	elif echo $mode | grep -q ":"; then
		printf "\e\e[1;1f%1s\e[0m" | tr " " ":"
		read -rsn1 mode # read 2 more chars
		mode=":$mode"
	fi
	IFS="${OLDIFS}"

	case "$mode" in
		':q') clear; quit ;;
		':w') writeSelected ;;
		'[A') arrow_up ;;
		'[B') arrow_down ;;
		'[D') arrow_left ;;
		'[C') arrow_right ;;
		' ')  toggleSelectedList $FOCUS_ID ;;
		'')   writeSelected ;;
		*) >&2 return ;;
	esac
}

reprintList
while true; do
	readInput
done


#stty echo