Declare variables more appropriately

This commit is contained in:
Kevin MacMartin 2023-08-24 17:26:41 -04:00
parent 940cb59e00
commit 29b50cc2b9

22
vnotes
View file

@ -17,13 +17,13 @@ VNOTES_FOLDER=${VNOTES_FOLDER:="$HOME/Notes"}
VNOTES_EXTENSION=${NOTES_SUFFIX:='md'} VNOTES_EXTENSION=${NOTES_SUFFIX:='md'}
# Dependencies # Dependencies
deps=('dd' 'getkeycodes' 'od' 'stty' 'tput') declare -a deps=('dd' 'getkeycodes' 'od' 'stty' 'tput')
# Declare global variables
declare -a documents=() declare -a documents=()
declare one_display='' declare -i max_docwidth=0 upto=0
declare upto='' one_display=''
declare tty_state='' tty_state=''
declare max_docwidth=0
# Colour Scheme # Colour Scheme
[[ -t 1 ]] && { [[ -t 1 ]] && {
@ -100,7 +100,7 @@ function prompt {
# Calculates a number to the power of another # Calculates a number to the power of another
function power { function power {
local number="$1" power="$2" total=1 local -i number="$1" power="$2" total=1
for (( x=0; x<"$power"; x++ )); do for (( x=0; x<"$power"; x++ )); do
total=$(( total * number )) total=$(( total * number ))
@ -128,7 +128,8 @@ function search_notes {
local key="$1" local key="$1"
# Declare local variables we'll be using # Declare local variables we'll be using
local screen_width maxcolumns digits digits_power cnt count_display value keypress local -i screen_width=0 maxcolumns=0 digits=1 digits_power cnt=1
local count_display value keypress
# Declare variables with the keycodes for "Q", "q", return/enter and 0-9 # Declare variables with the keycodes for "Q", "q", return/enter and 0-9
local alpha_Q alpha_q key_cr key_0 key_1 key_2 key_3 key_4 key_5 key_6 key_7 key_8 key_9 local alpha_Q alpha_q key_cr key_0 key_1 key_2 key_3 key_4 key_5 key_6 key_7 key_8 key_9
@ -185,9 +186,7 @@ function search_notes {
# Display the multiple matching documents (or the full list if no term was passed) # Display the multiple matching documents (or the full list if no term was passed)
screen_width=$(tput cols) screen_width=$(tput cols)
if [[ -n "$screen_width" ]]; then if (( screen_width > 0 )); then
maxcolumns=0
while (( 1 )); do while (( 1 )); do
(( ((max_docwidth + 8) * maxcolumns) > screen_width )) && { (( ((max_docwidth + 8) * maxcolumns) > screen_width )) && {
(( maxcolumns-- )) (( maxcolumns-- ))
@ -204,8 +203,6 @@ function search_notes {
upto=${#documents[@]} upto=${#documents[@]}
# Calculate the number of digits in $upto # Calculate the number of digits in $upto
digits=1
while (( 1 )); do while (( 1 )); do
digits_power=$(power 10 $digits) digits_power=$(power 10 $digits)
(( (upto / digits_power) < 1 )) && break (( (upto / digits_power) < 1 )) && break
@ -214,7 +211,6 @@ function search_notes {
# Draw the list # Draw the list
printf '\n' printf '\n'
cnt=1
for (( x=0; x<${#documents[*]}; x++ )); do for (( x=0; x<${#documents[*]}; x++ )); do
count_display=$(printf "%${digits}s" "$(( x + 1 ))") count_display=$(printf "%${digits}s" "$(( x + 1 ))")