Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load timer #405

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 37 additions & 22 deletions bin/armbian-config
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
#!/bin/bash

# initializes the terminal from TERM if stdin is a terminal
[[ -t 0 ]] && tput init

# Language-based variable assignment for script directory path
# This serves as a Rosetta Stone for developers,
# allowing them to use the variable name they are most comfortable with.

unset UXMODE
[[ -t 0 && "$1" =~ ^(|--cmd|--help)$ ]] && UXMODE="true" && tput init
# allows CTRL c to exit
trap "exit" INT TERM

[[ $EUID != 0 ]] && exec sudo "$0" "$@"
#
# Get the script directory
script_dir="$(dirname "$0")"

declare -A module_options

# Load the initialize modules
source "$script_dir/../lib/armbian-config/config.initialize.sh"

# Start loading messages
set_checkpoint start
set_checkpoint mark "Initializing script"

[[ -d "$script_dir/../tools" ]] && tools_dir="$script_dir/../tools"
[[ ! -d "$script_dir/../lib" && -n "$tools_dir" ]] && die -e "Please run\nbash "$tools_dir/config-assemble.sh" to build the lib directory\n"

Expand All @@ -29,32 +35,33 @@ json_file="$lib_dir/config.jobs.json"
# Load The Bash procedure Objects
json_data=$(<"$json_file")

#
# Prepare the module options array
declare -A module_options

#
# Load configng core functions and module options array

source "$lib_dir/config.functions.sh"
set_runtime_variables
check_distro_status
echo "Loaded Runtime variables..." #| show_infobox ;
#set_newt_colors 2
echo "Loaded Dialog..." #| show_infobox ;
set_checkpoint mark "Loaded Runtime variables..."
# checks for supported os
set_checkpoint mark "$(check_distro_status)"

set_checkpoint mark "Loaded Dialog..."
source "$lib_dir/config.docs.sh"
echo "Loaded Docs..." #| show_infobox ;
set_checkpoint mark "Loaded Docs..."
source "$lib_dir/config.system.sh"
echo "Loaded System helpers..." #| show_infobox ;
set_checkpoint mark "Loaded System helpers..."
source "$lib_dir/config.network.sh"
echo "Loaded Network helpers..." #| show_infobox ;
set_checkpoint mark "Loaded Network helpers..."
source "$lib_dir/config.software.sh"
echo "Loaded Software helpers..." #| show_infobox ;
set_checkpoint mark "Loaded Software helpers..."
#
# Loads the variables from beta armbian-config for runtime handling

source "$lib_dir/config.runtime.sh"
echo "Loaded Runtime conditions..." #| show_infobox ;
set_checkpoint mark "Loaded Runtime conditions..."

# usage: sudo DEBUG=1 ./config-time/bin/armbian-config $@
# place one in a section with a quoted "about message"
[[ -n "$DEBUG" ]] && set_checkpoint mark "DEBUG Messages on" ;

case "$1" in
"--help")
Expand All @@ -63,7 +70,7 @@ case "$1" in
echo ""
else
echo "Usage: armbian-config --[option]
Options:
Options:
--help [category] Use [category] to filter specific menu options.
--cmd [option] Run a command from the menu (simple)
--api [option] Run a helper command (advanced)
Expand All @@ -82,6 +89,7 @@ case "$1" in
;;

"--cmd")
checkpoint debug "Starting Command line UX option"
INPUTMODE="cmd"
case "$2" in
""|"help")
Expand All @@ -90,11 +98,13 @@ case "$1" in
*)
cmd=$(sanitize "$2") || die
execute_command "$cmd"
checkpoint debug "Starting Command line options UX"
;;
esac
;;

"--api")
checkpoint debug "Starting --api options"
case "$2" in
""|"help")
see_use
Expand All @@ -103,6 +113,7 @@ case "$1" in
fn=$(sanitize "$2") || die
shift 2
"$fn" "$@"
checkpoint debug "Exiting --api"
;;
esac
;;
Expand Down Expand Up @@ -145,9 +156,13 @@ case "$1" in
*)
# Generate the top menu with the modified Object data
set_colors 4
checkpoint debug "Starting Text User Interface (TUI)"
generate_top_menu "$json_data"

# Show about this tool on exit
checkpoint debug "Exiting TUI"
# Show about this tool on exit
about_armbian_configng

;;
esac

[[ -n "$DEBUG" && -n $tools_dir ]] && set_checkpoint show
104 changes: 104 additions & 0 deletions tools/modules/initialize/message_checkpoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
module_options+=(
["set_checkpoint,author"]="@armbian"
["set_checkpoint,maintainer"]="@igorpecovnik"
["set_checkpoint,feature"]="set_checkpoint"
["set_checkpoint,example"]="help start mark stop show"
["set_checkpoint,desc"]="Helper module for timing code execution"
["set_checkpoint,status"]=""
["set_checkpoint,doc_link"]=""
["set_checkpoint,group"]="Development"
["set_checkpoint,port"]=""
["set_checkpoint,arch"]="x86-64 arm64"
)

module_options+=(
["debug_checkpoint,author"]="@armbian"
["debug_checkpoint,maintainer"]="@igorpecovnik"
["debug_checkpoint,feature"]="debug_checkpoint"
["debug_checkpoint,example"]="help start mark stop show"
["debug_checkpoint,desc"]="Helper module for debug info"
["debug_checkpoint,status"]=""
["debug_checkpoint,doc_link"]=""
["debug_checkpoint,group"]="Development"
["debug_checkpoint,port"]=""
["debug_checkpoint,arch"]="x86-64 arm64"
)
#
# Function to manage timer with multiple checkpoints
function set_checkpoint() {
case "$1" in
help)
echo "Usage: set_checkpoint <start|stop|mark|show> [description] [show]"
echo "Commands:"
echo " start Start the timer."
echo " stop Stop the timer."
echo " mark [description] [time] Mark a checkpoint with an optional description and an optional flag to show the output."
echo " show Show the total elapsed time and checkpoints."
;;
start)
set_checkpoint_START=$(date +%s)
set_checkpoint_CHECKPOINTS=()
set_checkpoint_DESCRIPTIONS=()
set_checkpoint_PREV=$set_checkpoint_START
;;
stop)
set_checkpoint_STOP=$(date +%s)
;;
mark)
if [[ "$UXMODE" == "true" || -n "$DEBUG" ]]; then
local checkpoint_time=$(date +%s)
local checkpoint_duration=$((checkpoint_time - set_checkpoint_PREV))
set_checkpoint_PREV=$checkpoint_time
set_checkpoint_CHECKPOINTS+=($checkpoint_time)
set_checkpoint_DESCRIPTIONS+=("$2")
local count=${#set_checkpoint_DESCRIPTIONS[@]}
# No debug UX mode debug level 1 defaulted here
[[ -z "$DEBUG" ]] && printf "%-30s %10d seconds\n" "$2 " "${checkpoint_duration}"

# debug level 2 shows all message
[[ "$DEBUG" -eq 2 ]] && printf "%-30s %10d seconds\n" "$2 " "${checkpoint_duration}"
fi
;;
show)
[[ -z "$set_checkpoint_STOP" ]] && set_checkpoint stop
if [[ -n "$set_checkpoint_START" && -n "$set_checkpoint_STOP" ]]; then
set_checkpoint_DURATION=$((set_checkpoint_STOP - set_checkpoint_START))
printf "%-30s: %d seconds\n" "Total elapsed time" "${set_checkpoint_DURATION}"

local previous_time=$set_checkpoint_START
for i in "${!set_checkpoint_CHECKPOINTS[@]}"; do
local checkpoint_time=${set_checkpoint_CHECKPOINTS[$i]}
local checkpoint_duration=$((checkpoint_time - previous_time))
local description=${set_checkpoint_DESCRIPTIONS[$i]}
printf "%-30s: %d seconds\n" "${description:-Checkpoint $((i+1))}" "${checkpoint_duration}"
previous_time=$checkpoint_time
done
else
echo "Timer has not been started and stopped properly."
fi
;;
*)
echo "Usage: set_checkpoint <start|stop|mark|show> [description]"
;;
esac
}

# Example usage
# set_checkpoint aluse with debugging
checkpoint() {
case "$1" in
start|stop|mark|show)
set_checkpoint "$1" "$2"
;;
debug)
[[ -n "$DEBUG" && -n $tools_dir ]] && set_checkpoint mark "$2"
;;
help)
set_checkpoint help
echo " debug DEBUG checkpoint message"
;;
*)
echo "Invalid command. Use: help, start, stop, mark, debug, show"
;;
esac
}
Loading