addet bash scripts
This commit is contained in:
parent
a0258295b0
commit
5e9b9c9755
4 changed files with 780 additions and 0 deletions
106
bash_tui.sh
Executable file
106
bash_tui.sh
Executable file
|
@ -0,0 +1,106 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Colors
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[0;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Menu options
|
||||
declare -a options=(
|
||||
"Dotfiles"
|
||||
"CLI Tool Installation"
|
||||
"Sudo-Options"
|
||||
"Optimizations"
|
||||
"Exit"
|
||||
)
|
||||
# declare -a options="Dotfiles CLI_Tool_Installation Sudo-Options Optimizations Exit"
|
||||
|
||||
# Function to print colored text
|
||||
print_color() {
|
||||
printf "%b%s%b\n" "$1" "$2" "$NC"
|
||||
}
|
||||
|
||||
# Function to display the menu
|
||||
display_menu() {
|
||||
clear
|
||||
print_color "$BLUE" "=== Environment Setup Menu ==="
|
||||
for i in "${!options[@]}"; do
|
||||
if [[ $i -eq $selected ]]; then
|
||||
print_color "$GREEN" "> ${options[$i]}"
|
||||
else
|
||||
echo " ${options[$i]}"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Function to handle dotfiles setup
|
||||
dotfiles_setup() {
|
||||
print_color "$YELLOW" "Setting up dotfiles..."
|
||||
# Add your dotfiles setup logic here
|
||||
sleep 2
|
||||
}
|
||||
|
||||
# Function to handle CLI tool installation
|
||||
cli_tool_installation() {
|
||||
print_color "$YELLOW" "Installing CLI tools..."
|
||||
# Add your CLI tool installation logic here
|
||||
sleep 2
|
||||
}
|
||||
|
||||
# Function to handle optimizations
|
||||
optimizations() {
|
||||
print_color "$YELLOW" "Performing optimizations..."
|
||||
# Add your optimization logic here
|
||||
sleep 2
|
||||
}
|
||||
|
||||
sudo_options() {
|
||||
if [[ -e /etc/sudoers ]]; then
|
||||
echo "Defaults pwfeedback" | tee -a /etc/sudoers
|
||||
echo "Defaults insults" | tee -a /etc/sudoers
|
||||
else
|
||||
echo_error "There is no /etc/sudoers file."
|
||||
fi
|
||||
}
|
||||
|
||||
# Main menu loop
|
||||
main_menu() {
|
||||
local selected=0
|
||||
local key=""
|
||||
|
||||
while true; do
|
||||
display_menu
|
||||
|
||||
# Read a single character
|
||||
read -rsn1 key
|
||||
|
||||
case $key in
|
||||
A | k) # Up arrow or k
|
||||
((selected--))
|
||||
if [[ $selected -lt 0 ]]; then
|
||||
selected=$((${#options[@]} - 1))
|
||||
fi
|
||||
;;
|
||||
B | j) # Down arrow or j
|
||||
((selected++))
|
||||
if [[ $selected -ge ${#options[@]} ]]; then
|
||||
selected=0
|
||||
fi
|
||||
;;
|
||||
"") # Enter key
|
||||
case $selected in
|
||||
0) dotfiles_setup ;;
|
||||
1) cli_tool_installation ;;
|
||||
2) sudo_options ;;
|
||||
3) optimizations ;;
|
||||
4) exit 0 ;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
# Start the main menu
|
||||
main_menu </dev/tty
|
378
games/2048.sh
Executable file
378
games/2048.sh
Executable file
|
@ -0,0 +1,378 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
#important variables
|
||||
declare -ia board # array that keeps track of game status
|
||||
declare -i pieces # number of pieces present on board
|
||||
declare -i score=0 # score variable
|
||||
declare -i flag_skip # flag that prevents doing more than one operation on
|
||||
# single field in one step
|
||||
declare -i moves # stores number of possible moves to determine if player lost
|
||||
# the game
|
||||
declare ESC=$'\e' # escape byte
|
||||
declare header="Bash 2048 v1.1 (https://github.com/mydzor/bash2048)"
|
||||
|
||||
declare -i start_time=$(date +%s)
|
||||
|
||||
#default config
|
||||
declare -i board_size=4
|
||||
declare -i target=2048
|
||||
declare -i reload_flag=0
|
||||
declare config_dir="$HOME/.bash2048"
|
||||
|
||||
#for colorizing numbers
|
||||
declare -a colors
|
||||
colors[2]=33 # yellow text
|
||||
colors[4]=32 # green text
|
||||
colors[8]=34 # blue text
|
||||
colors[16]=36 # cyan text
|
||||
colors[32]=35 # purple text
|
||||
colors[64]="33m\033[7" # yellow background
|
||||
colors[128]="32m\033[7" # green background
|
||||
colors[256]="34m\033[7" # blue background
|
||||
colors[512]="36m\033[7" # cyan background
|
||||
colors[1024]="35m\033[7" # purple background
|
||||
colors[2048]="31m\033[7" # red background (won with default target)
|
||||
|
||||
exec 3>/dev/null # no logging by default
|
||||
|
||||
trap "end_game 0 1" INT #handle INT signal
|
||||
|
||||
#simplified replacement of seq command
|
||||
function _seq {
|
||||
local cur=1
|
||||
local max
|
||||
local inc=1
|
||||
case $# in
|
||||
1) let max=$1 ;;
|
||||
2)
|
||||
let cur=$1
|
||||
let max=$2
|
||||
;;
|
||||
3)
|
||||
let cur=$1
|
||||
let inc=$2
|
||||
let max=$3
|
||||
;;
|
||||
esac
|
||||
while test $max -ge $cur; do
|
||||
printf "$cur "
|
||||
let cur+=inc
|
||||
done
|
||||
}
|
||||
|
||||
# print currect status of the game, last added pieces are marked red
|
||||
function print_board {
|
||||
clear
|
||||
printf "$header pieces=$pieces target=$target score=$score\n"
|
||||
printf "Board status:\n" >&3
|
||||
printf "\n"
|
||||
printf '/------'
|
||||
for l in $(_seq 1 $index_max); do
|
||||
printf '+------'
|
||||
done
|
||||
printf '\\\n'
|
||||
for l in $(_seq 0 $index_max); do
|
||||
printf '|'
|
||||
for m in $(_seq 0 $index_max); do
|
||||
if let ${board[l * $board_size + m]}; then
|
||||
if let '(last_added==(l*board_size+m))|(first_round==(l*board_size+m))'; then
|
||||
printf '\033[1m\033[31m %4d \033[0m|' ${board[l * $board_size + m]}
|
||||
else
|
||||
printf "\033[1m\033[${colors[${board[l * $board_size + m]}]}m %4d\033[0m |" ${board[l * $board_size + m]}
|
||||
fi
|
||||
printf " %4d |" ${board[l * $board_size + m]} >&3
|
||||
else
|
||||
printf ' |'
|
||||
printf ' |' >&3
|
||||
fi
|
||||
done
|
||||
let l==$index_max || {
|
||||
printf '\n|------'
|
||||
for l in $(_seq 1 $index_max); do
|
||||
printf '+------'
|
||||
done
|
||||
printf '|\n'
|
||||
printf '\n' >&3
|
||||
}
|
||||
done
|
||||
printf '\n\\------'
|
||||
for l in $(_seq 1 $index_max); do
|
||||
printf '+------'
|
||||
done
|
||||
printf '/\n'
|
||||
}
|
||||
|
||||
# Generate new piece on the board
|
||||
# inputs:
|
||||
# $board - original state of the game board
|
||||
# $pieces - original number of pieces
|
||||
# outputs:
|
||||
# $board - new state of the game board
|
||||
# $pieces - new number of pieces
|
||||
function generate_piece {
|
||||
while true; do
|
||||
let pos=RANDOM%fields_total
|
||||
let board[$pos] || {
|
||||
let value=RANDOM%10?2:4
|
||||
board[$pos]=$value
|
||||
last_added=$pos
|
||||
printf "Generated new piece with value $value at position [$pos]\n" >&3
|
||||
break
|
||||
}
|
||||
done
|
||||
let pieces++
|
||||
}
|
||||
|
||||
# perform push operation between two pieces
|
||||
# inputs:
|
||||
# $1 - push position, for horizontal push this is row, for vertical column
|
||||
# $2 - recipient piece, this will hold result if moving or joining
|
||||
# $3 - originator piece, after moving or joining this will be left empty
|
||||
# $4 - direction of push, can be either "up", "down", "left" or "right"
|
||||
# $5 - if anything is passed, do not perform the push, only update number
|
||||
# of valid moves
|
||||
# $board - original state of the game board
|
||||
# outputs:
|
||||
# $change - indicates if the board was changed this round
|
||||
# $flag_skip - indicates that recipient piece cannot be modified further
|
||||
# $board - new state of the game board
|
||||
function push_pieces {
|
||||
case $4 in
|
||||
"up")
|
||||
let "first=$2*$board_size+$1"
|
||||
let "second=($2+$3)*$board_size+$1"
|
||||
;;
|
||||
"down")
|
||||
let "first=(index_max-$2)*$board_size+$1"
|
||||
let "second=(index_max-$2-$3)*$board_size+$1"
|
||||
;;
|
||||
"left")
|
||||
let "first=$1*$board_size+$2"
|
||||
let "second=$1*$board_size+($2+$3)"
|
||||
;;
|
||||
"right")
|
||||
let "first=$1*$board_size+(index_max-$2)"
|
||||
let "second=$1*$board_size+(index_max-$2-$3)"
|
||||
;;
|
||||
esac
|
||||
let ${board[$first]} || {
|
||||
let ${board[$second]} && {
|
||||
if test -z $5; then
|
||||
board[$first]=${board[$second]}
|
||||
let board[$second]=0
|
||||
let change=1
|
||||
printf "move piece with value ${board[$first]} from [$second] to [$first]\n" >&3
|
||||
else
|
||||
let moves++
|
||||
fi
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
let ${board[$second]} && let flag_skip=1
|
||||
let "${board[$first]}==${board[second]}" && {
|
||||
if test -z $5; then
|
||||
let board[$first]*=2
|
||||
let "board[$first]==$target" && end_game 1
|
||||
let board[$second]=0
|
||||
let pieces-=1
|
||||
let change=1
|
||||
let score+=${board[$first]}
|
||||
printf "joined piece from [$second] with [$first], new value=${board[$first]}\n" >&3
|
||||
else
|
||||
let moves++
|
||||
fi
|
||||
}
|
||||
}
|
||||
|
||||
function apply_push {
|
||||
printf "\n\ninput: $1 key\n" >&3
|
||||
for i in $(_seq 0 $index_max); do
|
||||
for j in $(_seq 0 $index_max); do
|
||||
flag_skip=0
|
||||
let increment_max=index_max-j
|
||||
for k in $(_seq 1 $increment_max); do
|
||||
let flag_skip && break
|
||||
push_pieces $i $j $k $1 $2
|
||||
done
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
function check_moves {
|
||||
let moves=0
|
||||
apply_push up fake
|
||||
apply_push down fake
|
||||
apply_push left fake
|
||||
apply_push right fake
|
||||
}
|
||||
|
||||
function key_react {
|
||||
let change=0
|
||||
read -d '' -sn 1
|
||||
test "$REPLY" = "$ESC" && {
|
||||
read -d '' -sn 1 -t1
|
||||
test "$REPLY" = "[" && {
|
||||
read -d '' -sn 1 -t1
|
||||
case $REPLY in
|
||||
A) apply_push up ;;
|
||||
B) apply_push down ;;
|
||||
C) apply_push right ;;
|
||||
D) apply_push left ;;
|
||||
esac
|
||||
}
|
||||
} || {
|
||||
case $REPLY in
|
||||
k) apply_push up ;;
|
||||
j) apply_push down ;;
|
||||
l) apply_push right ;;
|
||||
h) apply_push left ;;
|
||||
|
||||
w) apply_push up ;;
|
||||
s) apply_push down ;;
|
||||
d) apply_push right ;;
|
||||
a) apply_push left ;;
|
||||
esac
|
||||
}
|
||||
}
|
||||
|
||||
function save_game {
|
||||
rm -rf "$config_dir"
|
||||
mkdir "$config_dir"
|
||||
echo "${board[@]}" >"$config_dir/board"
|
||||
echo "$board_size" >"$config_dir/board_size"
|
||||
echo "$pieces" >"$config_dir/pieces"
|
||||
echo "$target" >"$config_dir/target"
|
||||
# echo "$log_file" > "$config_dir/log_file"
|
||||
echo "$score" >"$config_dir/score"
|
||||
echo "$first_round" >"$config_dir/first_round"
|
||||
}
|
||||
|
||||
function reload_game {
|
||||
printf "Loading saved game...\n" >&3
|
||||
|
||||
if test ! -d "$config_dir"; then
|
||||
return
|
||||
fi
|
||||
board=($(cat "$config_dir/board"))
|
||||
board_size=($(cat "$config_dir/board_size"))
|
||||
board=($(cat "$config_dir/board"))
|
||||
pieces=($(cat "$config_dir/pieces"))
|
||||
first_round=($(cat "$config_dir/first_round"))
|
||||
target=($(cat "$config_dir/target"))
|
||||
score=($(cat "$config_dir/score"))
|
||||
|
||||
fields_total=board_size*board_size
|
||||
index_max=board_size-1
|
||||
}
|
||||
|
||||
function end_game {
|
||||
# count game duration
|
||||
end_time=$(date +%s)
|
||||
let total_time=end_time-start_time
|
||||
|
||||
print_board
|
||||
printf "Your score: $score\n"
|
||||
|
||||
printf "This game lasted "
|
||||
|
||||
$(date --version >/dev/null 2>&1)
|
||||
if [[ "$?" -eq 0 ]]; then
|
||||
date -u -d @${total_time} +%T
|
||||
else
|
||||
date -u -r ${total_time} +%T
|
||||
fi
|
||||
|
||||
stty echo
|
||||
let $1 && {
|
||||
printf "Congratulations you have achieved $target\n"
|
||||
exit 0
|
||||
}
|
||||
let test -z $2 && {
|
||||
read -n1 -p "Do you want to overwrite saved game? [y|N]: "
|
||||
test "$REPLY" = "Y" || test "$REPLY" = "y" && {
|
||||
save_game
|
||||
printf "\nGame saved. Use -r option next to load this game.\n"
|
||||
exit 0
|
||||
}
|
||||
test "$REPLY" = "" && {
|
||||
printf "\nGame not saved.\n"
|
||||
exit 0
|
||||
}
|
||||
}
|
||||
printf "\nYou have lost, better luck next time.\033[0m\n"
|
||||
exit 0
|
||||
}
|
||||
|
||||
function help {
|
||||
cat <<END_HELP
|
||||
Usage: $1 [-b INTEGER] [-t INTEGER] [-l FILE] [-r] [-h]
|
||||
|
||||
-b specify game board size (sizes 3-9 allowed)
|
||||
-t specify target score to win (needs to be power of 2)
|
||||
-l log debug info into specified file
|
||||
-r reload the previous game
|
||||
-h this help
|
||||
|
||||
END_HELP
|
||||
}
|
||||
|
||||
#parse commandline options
|
||||
while getopts "b:t:l:rh" opt; do
|
||||
case $opt in
|
||||
b)
|
||||
board_size="$OPTARG"
|
||||
let '(board_size>=3)&(board_size<=9)' || {
|
||||
printf "Invalid board size, please choose size between 3 and 9\n"
|
||||
exit -1
|
||||
}
|
||||
;;
|
||||
t)
|
||||
target="$OPTARG"
|
||||
printf "obase=2;$target\n" | bc | grep -e '^1[^1]*$'
|
||||
let $? && {
|
||||
printf "Invalid target, has to be power of two\n"
|
||||
exit -1
|
||||
}
|
||||
;;
|
||||
r) reload_flag="1" ;;
|
||||
h)
|
||||
help $0
|
||||
exit 0
|
||||
;;
|
||||
l) exec 3>$OPTARG ;;
|
||||
\?)
|
||||
printf "Invalid option: -"$opt", try $0 -h\n" >&2
|
||||
exit 1
|
||||
;;
|
||||
:)
|
||||
printf "Option -"$opt" requires an argument, try $0 -h\n" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
#init board
|
||||
let fields_total=board_size*board_size
|
||||
let index_max=board_size-1
|
||||
for i in $(_seq 0 $fields_total); do board[$i]="0"; done
|
||||
let pieces=0
|
||||
generate_piece
|
||||
first_round=$last_added
|
||||
generate_piece
|
||||
|
||||
#load saved game if flag is set
|
||||
if test $reload_flag = "1"; then
|
||||
reload_game
|
||||
fi
|
||||
|
||||
while true; do
|
||||
print_board
|
||||
key_react
|
||||
let change && generate_piece
|
||||
first_round=-1
|
||||
let pieces==fields_total && {
|
||||
check_moves
|
||||
let moves==0 && end_game 0 #lose the game
|
||||
}
|
||||
done
|
50
games/wordle.sh
Executable file
50
games/wordle.sh
Executable file
|
@ -0,0 +1,50 @@
|
|||
words=($(grep '^\w\w\w\w\w$' /usr/share/dict/words | tr '[a-z]' '[A-Z]'))
|
||||
actual=${words[$(($RANDOM % ${#words[@]}))]} end=false guess_count=0 max_guess=6
|
||||
if [[ $1 == "unlimit" ]]; then
|
||||
max_guess=999999
|
||||
fi
|
||||
while [[ $end != true ]]; do
|
||||
guess_count=$(($guess_count + 1))
|
||||
if [[ $guess_count -le $max_guess ]]; then
|
||||
echo "Enter your guess ($guess_count / $max_guess):"
|
||||
read guess
|
||||
guess=$(echo $guess | tr '[a-z]' '[A-Z]')
|
||||
if [[ " ${words[*]} " =~ " $guess " ]]; then
|
||||
output="" remaining=""
|
||||
if [[ $actual == $guess ]]; then
|
||||
echo "You guessed right!"
|
||||
for ((i = 0; i < ${#actual}; i++)); do
|
||||
output+="\033[30;102m ${guess:$i:1} \033[0m"
|
||||
done
|
||||
printf "$output\n"
|
||||
end=true
|
||||
else
|
||||
for ((i = 0; i < ${#actual}; i++)); do
|
||||
if [[ "${actual:$i:1}" != "${guess:$i:1}" ]]; then
|
||||
remaining+=${actual:$i:1}
|
||||
fi
|
||||
done
|
||||
for ((i = 0; i < ${#actual}; i++)); do
|
||||
if [[ "${actual:$i:1}" != "${guess:$i:1}" ]]; then
|
||||
if [[ "$remaining" == *"${guess:$i:1}"* ]]; then
|
||||
output+="\033[30;103m ${guess:$i:1} \033[0m"
|
||||
remaining=${remaining/"${guess:$i:1}"/}
|
||||
else
|
||||
output+="\033[30;107m ${guess:$i:1} \033[0m"
|
||||
fi
|
||||
else
|
||||
output+="\033[30;102m ${guess:$i:1} \033[0m"
|
||||
fi
|
||||
done
|
||||
printf "$output\n"
|
||||
fi
|
||||
else
|
||||
echo "Please enter a valid word with 5 letters!"
|
||||
guess_count=$(($guess_count - 1))
|
||||
fi
|
||||
else
|
||||
echo "You lose! The word is:"
|
||||
echo $actual
|
||||
end=true
|
||||
fi
|
||||
done
|
246
gitsetup.sh
Executable file
246
gitsetup.sh
Executable file
|
@ -0,0 +1,246 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Get the current git config list
|
||||
gc_ls="$(git config --list)"
|
||||
|
||||
# ANSI color codes
|
||||
RED='\033[0;31m'
|
||||
CYAN='\033[0;36m'
|
||||
YELLOW='\033[0;33m'
|
||||
GREEN='\033[0;32m'
|
||||
BLUE='\033[0;34m'
|
||||
LIGHT_GREEN='\033[0;92m'
|
||||
BOLD='\033[1m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
echo_error() {
|
||||
printf "${BOLD}${RED}ERROR: ${NC}${RED}%s${NC}\n" "$1" >&2
|
||||
}
|
||||
|
||||
echo_warning() {
|
||||
printf "${BOLD}${YELLOW}WARNING: ${NC}${YELLOW}%s${NC}\n" "$1"
|
||||
}
|
||||
|
||||
echo_gls() {
|
||||
printf "${BOLD}${YELLOW}%s${NC}\n" "$1"
|
||||
}
|
||||
|
||||
echo_note() {
|
||||
printf "${BOLD}${LIGHT_GREEN}NOTE: ${NC}${LIGHT_GREEN}%s${NC}\n" "$1"
|
||||
}
|
||||
|
||||
# Function to print colored text
|
||||
print_color() {
|
||||
printf "%b%s%b\n" "$1" "$2" "$NC"
|
||||
}
|
||||
|
||||
# Check if the given command exists silently
|
||||
command_exists() {
|
||||
command -v "$@" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
# Function to install a package if `curl` is available
|
||||
if command_exists curl; then
|
||||
install_pkg() {
|
||||
sh -c "$(curl -sSL https://git.k4li.de/pika/scripts/raw/branch/main/bash/snippets/install_pkg.sh)" -- "$@"
|
||||
}
|
||||
else
|
||||
echo_error "curl is not installed, universal install disabled!"
|
||||
fi
|
||||
|
||||
# Function for barebones Git setup
|
||||
gitBareBonesSetup() {
|
||||
echo_gls "Please enter your username: "
|
||||
read -r g_username
|
||||
echo_gls "Please enter your email: "
|
||||
read -r g_mail
|
||||
echo_gls "Please enter your default branch name <e.g. main>"
|
||||
read -r g_branch
|
||||
git config --global user.name "$g_username"
|
||||
git config --global user.email "$g_mail"
|
||||
git config --global init.defaultBranch "$g_branch"
|
||||
echo_note "Barebones Git setup complete!"
|
||||
read </dev/tty
|
||||
}
|
||||
|
||||
# Function for extended Git configuration
|
||||
gitExtendedSetup() {
|
||||
echo_gls "Please enter the default branch name (e.g., main): "
|
||||
read -r g_branch
|
||||
echo_gls "Please enter your preferred text editor (e.g., vim, nano, code): "
|
||||
read -r g_editor
|
||||
|
||||
# Set default branch name
|
||||
git config --global init.defaultBranch "$g_branch"
|
||||
|
||||
# Set preferred text editor
|
||||
git config --global core.editor "$g_editor"
|
||||
|
||||
# Set some common aliases
|
||||
git config --global alias.co "checkout"
|
||||
git config --global alias.br "branch"
|
||||
git config --global alias.ci "commit"
|
||||
git config --global alias.st "status"
|
||||
|
||||
# Set default push behavior to simple (to avoid issues with push)
|
||||
git config --global push.default simple
|
||||
|
||||
# Enable credential caching for 15 minutes (900 seconds)
|
||||
git config --global credential.helper "cache --timeout=900"
|
||||
|
||||
# Add a global .gitignore file (optional)
|
||||
echo_gls "Do you have a global .gitignore file? If so, please provide the path (or leave blank to skip): "
|
||||
read -r g_gitignore
|
||||
if [ -n "$g_gitignore" ] && [ -f "$g_gitignore" ]; then
|
||||
git config --global core.excludesfile "$g_gitignore"
|
||||
echo_note "Global .gitignore file set to $g_gitignore"
|
||||
else
|
||||
echo_warning "Skipping global .gitignore configuration!"
|
||||
fi
|
||||
|
||||
echo_note "Extended Git setup complete!"
|
||||
read </dev/tty
|
||||
}
|
||||
|
||||
gitChangeMail() {
|
||||
echo_gls "Please enter the new e-Mail: "
|
||||
read -e -r new_mail
|
||||
git config --global user.email "$new_mail"
|
||||
echo_note "Email updated to $new_mail"
|
||||
read </dev/tty
|
||||
}
|
||||
|
||||
gitChangeUsername() {
|
||||
currentName="$(git config --get user.name)"
|
||||
echo_warning "Do you really want to change your username? (current username is $currentName) (y/N): "
|
||||
read -e -r confirm
|
||||
if [[ "$currentName" -eq ^"yY" ]]; then
|
||||
echo_gls "Please enter the new Username: "
|
||||
read -r new_username
|
||||
git config --global user.name "$new_username"
|
||||
echo_note "Username updated to $new_username"
|
||||
read </dev/tty
|
||||
fi
|
||||
}
|
||||
|
||||
gitCredentialsAddAuthToken() {
|
||||
local CREDENTIALS_FILE="$HOME/.git-credentials"
|
||||
echo_gls "Enter the domain/server (e.g. git.k4li.de):"
|
||||
read -r g_domain
|
||||
|
||||
echo_gls "Enter your username:"
|
||||
read -r g_user
|
||||
|
||||
echo_gls "Enter your authentication token:"
|
||||
read -r g_token # The '-s' flag hides input for privacy
|
||||
|
||||
# Append the new credentials to the file
|
||||
echo "https://$g_user:$g_token@$g_domain" >>"$CREDENTIALS_FILE"
|
||||
|
||||
echo "Credentials added for $g_domain in $CREDENTIALS_FILE"
|
||||
|
||||
git config --global credential.helper store
|
||||
|
||||
chmod 600 "$HOME/.git-credentials"
|
||||
|
||||
read </dev/tty
|
||||
}
|
||||
|
||||
# Display the current Git configuration
|
||||
showGitConfig() {
|
||||
echo_gls "This is your current git-config:"
|
||||
echo_gls "─────────────────────────────────────────────────"
|
||||
|
||||
# Loop through each line of the git config list and format it nicely
|
||||
printf "%s\n" "$gc_ls" | while IFS='=' read -r key value; do
|
||||
# Extract section/category name (before the first period)
|
||||
category="${key%%.*}"
|
||||
# Extract setting name (after the first period)
|
||||
setting="${key#*.}"
|
||||
|
||||
# Use different colors and styles for category and setting names
|
||||
printf "${BOLD}${CYAN}%-20s${NC}: ${LIGHT_GREEN}%-30s${NC} ${YELLOW}%s${NC}\n" "$category" "$setting" "$value"
|
||||
done
|
||||
|
||||
echo_gls "─────────────────────────────────────────────────"
|
||||
|
||||
read </dev/tty
|
||||
}
|
||||
|
||||
# Main menu loop
|
||||
main_menu() {
|
||||
local selected=0
|
||||
local key=""
|
||||
|
||||
while true; do
|
||||
display_menu
|
||||
|
||||
# Read a single character
|
||||
read -rsn1 key
|
||||
|
||||
case $key in
|
||||
A | k) # Up arrow or k
|
||||
((selected--))
|
||||
if [[ $selected -lt 0 ]]; then
|
||||
selected=$((${#options[@]} - 1))
|
||||
fi
|
||||
;;
|
||||
B | j) # Down arrow or j
|
||||
((selected++))
|
||||
if [[ $selected -ge ${#options[@]} ]]; then
|
||||
selected=0
|
||||
fi
|
||||
;;
|
||||
q) # q for quit
|
||||
exit 0
|
||||
;;
|
||||
"") # Enter key
|
||||
case $selected in
|
||||
0) gitBareBonesSetup ;;
|
||||
1) gitExtendedSetup ;;
|
||||
2) gitChangeMail ;;
|
||||
3) gitChangeUsername ;;
|
||||
4) gitCredentialsAddAuthToken ;;
|
||||
5) showGitConfig ;;
|
||||
6) exit 0 ;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
# Menu options
|
||||
declare -a options=(
|
||||
"BareBones setup"
|
||||
"Extended setup"
|
||||
"Change e-Mail"
|
||||
"Change username"
|
||||
"Activate git-credentials and add Authentication token for any git server"
|
||||
"Show the current git config"
|
||||
"Exit"
|
||||
)
|
||||
|
||||
# Function to display the menu
|
||||
display_menu() {
|
||||
clear
|
||||
print_color "$BLUE" "=== Git setup tui ==="
|
||||
echo_note "(navigate with j/k or up/down arrows - q to quit)"
|
||||
for i in "${!options[@]}"; do
|
||||
if [[ $i -eq $selected ]]; then
|
||||
print_color "$GREEN" "> ${options[$i]}"
|
||||
else
|
||||
echo " ${options[$i]}"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Main function
|
||||
main() {
|
||||
if command_exists git; then
|
||||
main_menu </dev/tty
|
||||
else
|
||||
install_pkg git && main
|
||||
fi
|
||||
}
|
||||
|
||||
main
|
Loading…
Add table
Add a link
Reference in a new issue