Skip to content

Commit

Permalink
Merge pull request #8 from LamaAni/add_scripts_folder
Browse files Browse the repository at this point in the history
Add scripts folder
  • Loading branch information
LamaAni authored Feb 2, 2024
2 parents 768219d + 08e2bae commit 172d28c
Show file tree
Hide file tree
Showing 14 changed files with 333 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode/
.local/
# Logs
logs
Expand Down Expand Up @@ -105,4 +106,4 @@ dist
.tern-port

# local dev folder
local_dev/
local_dev/
4 changes: 4 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Folders

1. `scripts` - a collection of scripts to be added as sub commands to `zbash_config`
2. `lib` - a collection of zbash_config built in functions
84 changes: 75 additions & 9 deletions src/compile
Original file line number Diff line number Diff line change
@@ -1,17 +1,83 @@
#!/bin/bash
CUR_PATH="$(dirname "${BASH_SOURCE[0]}")"
LIB_PATH="$CUR_PATH/lib"
SCRITPS_PATH="$CUR_PATH/scripts"

source "$CUR_PATH/scripts/external.sh"
source "$LIB_PATH/external.sh"

load_zbash_commons || exit $?

LOAD_FILES=($(find $CUR_PATH/scripts -type f -name '*.sh'))
LOAD_FILES+=("$CUR_PATH/main.sh")
function get_files_list() {
local load_path="$1"
if [ -f "$load_path" ]; then
echo "$load_path"
else
find "$load_path" -type f -name '*.sh'
fi
}

COMPILED_SCRIPT_PARTS=()
for fpath in "${LOAD_FILES[@]}"; do
COMPILED_SCRIPT_PARTS+=("$(cat "$fpath")")
done
function compile_files_script() {
local compiled_script=()
log:info "Compiling $# script files" 1>&2
for fpath in "$@"; do
if [ -z "$fpath" ]; then
continue
fi
log:info "Loading $fpath" 1>&2
compiled_script+=("$(cat "$fpath")")
done
join_by $'\n' "${compiled_script[@]}"
}

COMPILED_SCRIPT="$(join_by $'\n' "${COMPILED_SCRIPT_PARTS[@]}")"
echo "$COMPILED_SCRIPT"
function compile_submenu_scripts() {
local load_path="$1"
local files=(
$(get_files_list "$load_path")
)
local help=()
local script_functions=()
local command_list=()
local function_name=""
local filename=""
log:info "Found ${#files[@]} submenu script files" 1>&2
for fpath in "${files[@]}"; do
log:info "Loading script $fpath" 1>&2
filename="$(basename "$fpath")"
function_name="$(regexp_replace "[^a-zA-Z0-9_]" "_" "${filename%.*}")"
assert $? "Failed to parse function name" || return $?
[ -n "$function_name" ]
assert $? "Failed to parse function name (empty)" || return $?

script_functions+=(
"function __zbash_script_$function_name(){"
"local ___internal_safe_dump=\"\""
"$(cat "$fpath")"
"}"
)
local help_text=""
help_text="$(regexp_replace "^\s*#-#" "" "$(grep "#-#" "$fpath")")"
help_text="$(regexp_replace "\\\"" "'" "$help_text")"
assert $? "Failed to parse help text" || return $?

help+=(
" $function_name"$'\t\t'"$help_text"
)
done

local script=(
"export ZBASH_SCRIPTS_SUBMENU_COMMANDS_HELP=\"$(join_by $'\n' "$help")\""
"function zbash_load_submenu_scripts(){"
"local ___internal_safe_dump=\"\""
"$(join_by $'\n' "${script_functions[@]}")"
"}"
)

join_by $'\n' "${script[@]}"
}

COMPILED_SCRIPTS=(
"$(compile_files_script $(get_files_list "$LIB_PATH") "$CUR_PATH/main.sh")"
"$(compile_submenu_scripts "$SCRITPS_PATH")"
)

join_by $'\n' "${COMPILED_SCRIPTS[@]}"
38 changes: 38 additions & 0 deletions src/lib/cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

function zbash() {
# ZBash configuration files with command implementation.
local help="
zbash collection of command scripts
USAGE: zbash [sub_command] [... args]
COMMANDS:
$ZBASH_SCRIPTS_SUBMENU_COMMANDS_HELP
FLAGS:
-h | --help Show this help menu.
"
local function_name=""
local dump=""
local command="$1"
shift

[ -n "$command" ]
assert $? "Please provide a command to execute, or --help for help" || return $?

case "$command" in
--help | -h)
echo "$help"
return 0
;;
*)
zbash_load_submenu_scripts
assert $? "Failed to load submenu scripts" || return $?
function_name="__zbash_script_$command"
dump="$(type -t "$function_name" 2>&1)"
assert $? "Command not found $command" || return $?
;;
esac

"$function_name" "$@"
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file added src/lib/k0s.sh
Empty file.
10 changes: 5 additions & 5 deletions src/scripts/prompts.sh → src/lib/prompts.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#!/bin/bash

function prompt_clock() {
function zbash_prompt_clock() {
local format="$ZBASH_CONFIG_CLOCK_FORMAT"
: "${format:="%H:%M"}"
date +"$format"
}

function prompt_venv() {
function zbash_prompt_venv() {
if [[ -n "$VIRTUAL_ENV" ]]; then
printf "%s" "$(basename "$VIRTUAL_ENV")"
fi
}

function prompt_path() {
function zbash_prompt_path() {
printf "%s" "$PWD"
}

function prompt_git() {
function zbash_prompt_git() {
zbash_config_is_git_repository || return 0

local name=""
Expand All @@ -29,7 +29,7 @@ function prompt_git() {
echo "$name"
}

function prompt_git_status() {
function zbash_prompt_git_status() {
zbash_config_is_git_repository || return 0

local number_of_changes=""
Expand Down
16 changes: 8 additions & 8 deletions src/scripts/shell.sh → src/lib/shell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function zbash_config_prompt_command() {
local user_print=""
local print_venv=""
local print_git=""
local prompt_git_status=""
local zbash_prompt_git_status=""
local line_marker="$ZBASH_CONFIG_COMMAND_LINE_MARKER"

: "${auto_append_hist:="true"}"
Expand All @@ -81,25 +81,25 @@ function zbash_config_prompt_command() {
history -a
fi

clock_print="$(zbash_config_create_show_param CLOCK "$(prompt_clock)")"
path_print="$(zbash_config_create_show_param PATH "$(prompt_path)")"
clock_print="$(zbash_config_create_show_param CLOCK "$(zbash_prompt_clock)")"
path_print="$(zbash_config_create_show_param PATH "$(zbash_prompt_path)")"
hostname_print="$(zbash_config_create_show_param HOSTNAME "\h")"
user_print="$(zbash_config_create_show_param USER "\u")"
print_venv="$(zbash_config_create_show_param VENV "$(prompt_venv)")"
print_venv="$(zbash_config_create_show_param VENV "$(zbash_prompt_venv)")"

# Since git is a slow command. IF not shouwn then ignore.
if [ "$ZBASH_CONFIG_SHOW_GIT_BRANCH" != "false" ]; then
print_git="$(zbash_config_create_show_param GIT_BRANCH "$(prompt_git)")"
print_git="$(zbash_config_create_show_param GIT_BRANCH "$(zbash_prompt_git)")"
fi
: "${ZBASH_CONFIG_SHOW_GIT_BRANCH_STATUS:="$ZBASH_CONFIG_SHOW_GIT_BRANCH"}"
if [ "$ZBASH_CONFIG_SHOW_GIT_BRANCH_STATUS" != "false" ]; then
prompt_git_status="$(zbash_config_create_show_param GIT_BRANCH_STATUS "$(prompt_git_status)")"
zbash_prompt_git_status="$(zbash_config_create_show_param GIT_BRANCH_STATUS "$(zbash_prompt_git_status)")"
fi

local print_args=(
"$clock_print"
"$print_venv"
"$prompt_git_status"
"$zbash_prompt_git_status"
"$print_git"
"$core_print"
"$path_print"
Expand All @@ -113,5 +113,5 @@ function zbash_config_prompt_command() {
function zbash_config_set_prompt_command() {
local cmnd_to_run="$1"
: "${cmnd_to_run:="zbash_config_prompt_command"}"
PROMPT_COMMAND="$cmnd_to_run"
zbash_prompt_COMMAND="$cmnd_to_run"
}
10 changes: 7 additions & 3 deletions src/scripts/test.sh → src/lib/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ function assert() {
local code="$1"
shift
if [ "$code" -ne 0 ]; then
echo "[ERROR]" "$@"
echo "[$(date +%Y-%m-%dT%H:%M:%S%z)][zbash][ERROR]" "$@"
fi
return $code
}

function log:info() {
echo "[$(date +%Y-%m-%dT%H:%M:%S%z)][zbash][INFO]" "$@"
}

function zbash_config_test_counts() {
local counts="$ZBASH_CONFIG_TEST_CYCLE_COUNT"
: "${counts:="100"}"
Expand All @@ -25,7 +29,7 @@ function zbash_config_test_method_call() {
function zbash_config_test_git_speed() {
local counts="$(zbash_config_test_counts)"
echo "Checking $counts times get git branch"
time zbash_config_test_method_call prompt_git || return $?
time zbash_config_test_method_call zbash_prompt_git || return $?
echo "Checking $counts times get git status"
time zbash_config_test_method_call prompt_git_status || return $?
time zbash_config_test_method_call zbash_prompt_git_status || return $?
}
6 changes: 3 additions & 3 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ if [ $? -ne 0 ]; then
}
fi

# Main file, used for bash entry.

function zbash_config_run_command() {
# Main file, used for bash entry.

# Loading helper methods.
load_zbash_commons

Expand Down Expand Up @@ -99,7 +99,7 @@ function zbash_config_configure_shell() {
}

function reset_errored_prompt() {
PROMPT_COMMAND=""
zbash_prompt_COMMAND=""
PS1="ERROR IN SHELL \h \u>"
}

Expand Down
Loading

0 comments on commit 172d28c

Please sign in to comment.