Skip to content

Commit

Permalink
Bug fixes for menu navigation (#44)
Browse files Browse the repository at this point in the history
* Fix menu navigation

Menu navigation was broken and had following issues:
- selecting any option in submenu drops to main menu after operation finishes
- there was a broken implementation of having a submenu within a submenu that doesn't work due to buggy implementation
- there was a broken implementation of having conditions in json file to dynamically show and hide options but that doesn't work due to buggy implementation
- also main menu supported conditions while sub menu didn't which shows lack of consistency in design

Signed-off-by: Gunjan Gupta <[email protected]>

* migrate conditions to json file

Signed-off-by: Gunjan Gupta <[email protected]>

* Remove dead code

Signed-off-by: Gunjan Gupta <[email protected]>

* Update readme

Signed-off-by: Gunjan Gupta <[email protected]>

---------

Signed-off-by: Gunjan Gupta <[email protected]>
  • Loading branch information
viraniac authored Aug 29, 2024
1 parent d957dbe commit ffafd60
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 242 deletions.
45 changes: 40 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# Armbian Configuration Utility
Updated: Wed 31 Jul 2024 23:44:46 NZST
Updated: Thu Aug 29 11:45:21 AM UTC 2024

Utility for configuring your board, adjusting services, and installing applications. It comes with Armbian by default.

Expand All @@ -12,7 +12,7 @@ sudo armbian-config
- ## **System**
- **S01** - Enable Armbian kernel upgrades
- **S02** - Disable Armbian kernel upgrades
- **S03** - Edit the boot environment (WIP)
- **S03** - Edit the boot environment
- **S04** - Install Linux headers
- **S05** - Remove Linux headers

Expand All @@ -28,6 +28,8 @@ sudo armbian-config
- **N07** - Disconnect and forget all wifi connections (Advanced)
- **N08** - Toggle system IPv6/IPv4 internet protocol
- **N09** - (WIP) Setup Hotspot/Access point
- **N10** - Announce system in the network (Avahi)
- **N11** - Disable system announce in the network (Avahi)


- ## **Localisation**
Expand Down Expand Up @@ -83,7 +85,7 @@ Usage: armbian-configng [option] [arguments]
--cli S01 - Enable Armbian kernel upgrades
--cli S02 - Disable Armbian kernel upgrades
--cli S03 - Edit the boot environment (WIP)
--cli S03 - Edit the boot environment
--cli S04 - Install Linux headers
--cli S05 - Remove Linux headers
--cli N00 - Install Bluetooth support
Expand All @@ -96,6 +98,8 @@ Usage: armbian-configng [option] [arguments]
--cli N07 - Disconnect and forget all wifi connections (Advanced)
--cli N08 - Toggle system IPv6/IPv4 internet protocol
--cli N09 - (WIP) Setup Hotspot/Access point
--cli N10 - Announce system in the network (Avahi)
--cli N11 - Disable system announce in the network (Avahi)
--cli L00 - Change Global timezone (WIP)
--cli L01 - Change Locales reconfigure the language and character set
--cli L02 - Change Keyboard layout
Expand Down Expand Up @@ -158,7 +162,7 @@ set_safe_boot freeze

### S03

Edit the boot environment (WIP)
Edit the boot environment

Jobs:

Expand Down Expand Up @@ -306,6 +310,36 @@ Do you wish to continue?" process_input
hotspot_setup
~~~

### N10

Announce system in the network (Avahi)

Jobs:

~~~
get_user_continue "This operation will install avahi-daemon and add configuration files.
Do you wish to continue?" process_input
check_if_installed avahi-daemon
debconf-apt-progress -- apt-get -y install avahi-daemon libnss-mdns
cp /usr/share/doc/avahi-daemon/examples/sftp-ssh.service /etc/avahi/services/
cp /usr/share/doc/avahi-daemon/examples/ssh.service /etc/avahi/services/
service avahi-daemon restart
~~~

### N11

Disable system announce in the network (Avahi)

Jobs:

~~~
get_user_continue "This operation will purge avahi-daemon
Do you wish to continue?" process_input
check_if_installed avahi-daemon
systemctl stop avahi-daemon avahi-daemon.socket
debconf-apt-progress -- apt-get -y purge avahi-daemon
~~~

### L00

Change Global timezone (WIP)
Expand Down Expand Up @@ -414,11 +448,11 @@ These helper functions facilitate various operations related to job management,
| Display a message box | show_message <<< 'hello world' | Joey Turner
| Migrated procedures from Armbian config. | connect_bt_interface | Igor Pecovnik
| Freeze/unhold Migrated procedures from Armbian config. | set_safe_boot unhold or set_safe_boot freeze | Igor Pecovnik
| Check if kernel headers are installed | are_headers_installed | Gunjan Gupta
| Check when apt list was last updated | see_current_apt | Joey Turner
| Migrated procedures from Armbian config. | check_if_installed nano | Igor Pecovnik
| Generate 'Armbian CPU logo' SVG for document file. | generate_svg | Joey Turner
| Remove Linux headers | Headers_remove | Joey Turner
| Show or hide menu items based on conditions | toggle_menu_item | Joey Turner
| Update submenu descriptions based on conditions | update_submenu_data | Joey Turner
| sanitize input cli | sanitize_input |
| Check if a domain is reachable via IPv4 and IPv6 | check_ip_version google.com | Joey Turner
Expand All @@ -435,6 +469,7 @@ These helper functions facilitate various operations related to job management,
| Serve the edit and debug server. | serve_doc | Joey Turner
| Update JSON data with system information | update_json_data | Joey Turner
| pipeline strings to an infobox | show_infobox <<< 'hello world' ; | Joey Turner
| Parse json to get list of desired menu or submenu items | parse_menu_items 'menu_options_array' | Gunjan Gupta
| Show the usage of the functions. | see_use | Joey Turner
| Check the internet connection with fallback DNS | see_ping | Joey Turner
| Secure version of get_user_continue | get_user_continue_secure 'Do you wish to continue?' process_input | Joey Turner
Expand Down
9 changes: 2 additions & 7 deletions bin/armbian-configng
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ echo "Loaded Runtime conditions..." #| show_infobox ;
clear

case "$1" in
"--dev")
if [[ $EUID != 0 ]]; then
source "$lib_dir/config.ng.runtime.dev.sh"
echo "Loaded Development Runtime conditions..." | show_infobox
fi
;;
"--help")
see_cli_list
exit 0
Expand Down Expand Up @@ -145,7 +139,8 @@ esac

#
# Generate the top menu with the modified Object data
while generate_top_menu "$json_data"; do tput clear ; done
set_colors 4
generate_top_menu "$json_data"


#
Expand Down
158 changes: 93 additions & 65 deletions lib/armbian-configng/config.ng.functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -351,52 +351,67 @@ function reset_colors() {


module_options+=(
["generate_top_menu,author"]="Joey Turner"
["generate_top_menu,ref_link"]=""
["generate_top_menu,feature"]="generate_top_menu"
["generate_top_menu,desc"]="Build the main menu from a object"
["generate_top_menu,example"]="generate_top_menu 'json_data'"
["generate_top_menu,doc_link"]=""
["generate_top_menu,status"]="Active"
["parse_menu_items,author"]="Gunjan Gupta"
["parse_menu_items,ref_link"]=""
["parse_menu_items,feature"]="parse_menu_items"
["parse_menu_items,desc"]="Parse json to get list of desired menu or submenu items"
["parse_menu_items,example"]="parse_menu_items 'menu_options_array'"
["parse_menu_items,doc_link"]=""
["parse_menu_items,status"]="Active"
)
#
# Function to generate the main menu from a JSON object
# Function to parse the menu items
#
generate_top_menu() {
local json_data=$1
local menu_options=()
parse_menu_items() {
local -n options=$1
while IFS= read -r id
do
IFS= read -r description
IFS= read -r requirements
IFS= read -r condition
# If the condition field is not empty and not null, run the function specified in the condition
if [[ -n $requirements && $requirements != "null" ]]; then
local condition_result=$(eval $requirements)
if [[ -n $condition && $condition != "null" ]]; then
# If the function returns a truthy value, add the menu item to the menu
if [[ $condition_result ]]; then
menu_options+=("$id" " - $description ($something)")
if eval $condition; then
options+=("$id" " - $description")
fi
else
# If the condition field is empty or null, add the menu item to the menu
menu_options+=("$id" " - $description ")
options+=("$id" " - $description ")
fi
done < <(echo "$json_data" | jq -r '.menu[] | select(.show==true) | "\(.id)\n\(.description)\n\(.condition)"' || exit 1 )
done < <(echo "$json_data" | jq -r '.menu[] | '${parent_id:+".. | objects | select(.id==\"$parent_id\") | .sub[]? |"}' select(.disabled|not) | "\(.id)\n\(.description)\n\(.condition)"' || exit 1 )
}


set_colors 4
module_options+=(
["generate_top_menu,author"]="Joey Turner"
["generate_top_menu,ref_link"]=""
["generate_top_menu,feature"]="generate_top_menu"
["generate_top_menu,desc"]="Build the main menu from a object"
["generate_top_menu,example"]="generate_top_menu 'json_data'"
["generate_top_menu,doc_link"]=""
["generate_top_menu,status"]="Active"
)
#
# Function to generate the main menu from a JSON object
#
generate_top_menu() {
local json_data=$1

local OPTION=$($DIALOG --title "$TITLE" --menu "$BACKTITLE" 0 80 9 "${menu_options[@]}" 3>&1 1>&2 2>&3)
local exitstatus=$?
while true; do
local menu_options=()

if [ $exitstatus = 0 ]; then
if [ "$OPTION" == "" ]; then
exit 0
fi
[[ -n "$debug" ]] && echo "$OPTION"
generate_menu "$OPTION"
fi
parse_menu_items menu_options

# echo "Menu options: ${menu_options[@]}"
local OPTION=$($DIALOG --title "$TITLE" --menu "$BACKTITLE" 0 80 9 "${menu_options[@]}" \
--ok-button Select --cancel-button Exit 3>&1 1>&2 2>&3)
local exitstatus=$?

if [ $exitstatus = 0 ]; then
[ -z "$OPTION" ] && break
[[ -n "$debug" ]] && echo "$OPTION"
generate_menu "$OPTION"
fi
done
}


Expand All @@ -415,42 +430,33 @@ module_options+=(
function generate_menu() {
local parent_id=$1

# Get the submenu options for the current parent_id
local submenu_options=()
while IFS= read -r id
do
IFS= read -r description
submenu_options+=("$id" " - $description")
done < <(jq -r --arg parent_id "$parent_id" '.menu[] | select(.id==$parent_id) | .sub[]? | select(.show==true) | "\(.id)\n\(.description)"' <<< "$json_data")


local OPTION=$($DIALOG --title "$TITLE" --menu "$BACKTITLE" 0 80 9 "${submenu_options[@]}" \
--ok-button Select --cancel-button Back 3>&1 1>&2 2>&3)

local exitstatus=$?

if [ $exitstatus = 0 ]; then
if [ "$OPTION" == "" ]; then
generate_top_menu
fi
# Check if the selected option has a submenu
local submenu_count=$(jq -r --arg id "$OPTION" '.menu[] | .. | objects | select(.id==$id) | .sub[]? | length' "$json_file")
submenu_count=${submenu_count:-0} # If submenu_count is null or empty, set it to 0
if [ "$submenu_count" -gt 0 ]; then
# If it does, generate a new menu for the submenu
set_colors 2 # "$?"
[[ -n "$debug" ]] && echo "$OPTION"
generate_menu "$OPTION"
else
# If it doesn't, execute the command
[[ -n "$debug" ]] && echo "$OPTION"
execute_command "$OPTION"
#show_message <<< "$OPTION"
while true; do
# Get the submenu options for the current parent_id
local submenu_options=()
parse_menu_items submenu_options

local OPTION=$($DIALOG --title "$TITLE" --menu "$BACKTITLE" 0 80 9 "${submenu_options[@]}" \
--ok-button Select --cancel-button Back 3>&1 1>&2 2>&3)

local exitstatus=$?

if [ $exitstatus = 0 ]; then
[ -z "$OPTION" ] && break

# Check if the selected option has a submenu
local submenu_count=$(jq -r --arg id "$OPTION" '.menu[] | .. | objects | select(.id==$id) | .sub? | length' "$json_file")
submenu_count=${submenu_count:-0} # If submenu_count is null or empty, set it to 0
if [ "$submenu_count" -gt 0 ]; then
# If it does, generate a new menu for the submenu
[[ -n "$debug" ]] && echo "$OPTION"
generate_menu "$OPTION"
else
# If it doesn't, execute the command
[[ -n "$debug" ]] && echo "$OPTION"
execute_command "$OPTION"
fi
fi
fi

# echo "Submenu options: ${submenu_options[@]}"

done
}


Expand All @@ -474,7 +480,6 @@ function execute_command() {
[[ -n "$debug" ]] && echo "$command"
eval "$command"
done

}


Expand Down Expand Up @@ -746,6 +751,29 @@ see_current_apt() {
fi
}

module_options+=(
["are_headers_installed,author"]="Gunjan Gupta"
["are_headers_installed,ref_link"]=""
["are_headers_installed,feature"]="are_headers_installed"
["are_headers_installed,desc"]="Check if kernel headers are installed"
["are_headers_installed,example"]="are_headers_installed"
["are_headers_installed,status"]="Pending Review"
["are_headers_installed,doc_link"]=""
)
#
# @description Install kernel headers
#
function are_headers_installed () {
if [[ -f /etc/armbian-release ]]; then
PKG_NAME="linux-headers-${BRANCH}-${LINUXFAMILY}";
else
PKG_NAME="linux-headers-$(uname -r | sed 's/'-$(dpkg --print-architecture)'//')";
fi

check_if_installed ${PKG_NAME}
return $?
}


module_options+=(
["Headers_install,author"]="Joey Turner"
Expand Down
Loading

0 comments on commit ffafd60

Please sign in to comment.