fix main.sh
This commit is contained in:
+43
@@ -1,9 +1,23 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# wiki-cli - een kleine Wikipedia-zoektool voor de terminal.
|
||||||
|
# Werkt zowel direct met argumenten (zoals nmcli) als interactief
|
||||||
|
# in een shell-achtige loop wanneer je 'm zonder argumenten start.
|
||||||
|
#
|
||||||
|
# Gebruik:
|
||||||
|
# wiki-cli -> start interactieve modus
|
||||||
|
# wiki-cli search <term> -> zoek direct naar pagina's
|
||||||
|
# wiki-cli lang [nl|en|fr|de] -> toon of wijzig de interfacetaal
|
||||||
|
# wiki-cli help -> toon hulp
|
||||||
|
|
||||||
set -uo pipefail
|
set -uo pipefail
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Config
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
CONFIG_DIR="$HOME/.config/wiki-cli"
|
CONFIG_DIR="$HOME/.config/wiki-cli"
|
||||||
CONFIG_FILE="$CONFIG_DIR/config.conf"
|
CONFIG_FILE="$CONFIG_DIR/config.conf"
|
||||||
|
CACHE_FILE="$CONFIG_DIR/last_search"
|
||||||
LANG_CODE="en" # fallback totdat config geladen is
|
LANG_CODE="en" # fallback totdat config geladen is
|
||||||
SUPPORTED_LANGS=(nl en fr de)
|
SUPPORTED_LANGS=(nl en fr de)
|
||||||
|
|
||||||
@@ -48,6 +62,9 @@ first_run_setup() {
|
|||||||
save_config
|
save_config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Vertalingen
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
declare -A MSG
|
declare -A MSG
|
||||||
|
|
||||||
MSG["nl,search_prompt"]="Zoek naar pagina's:"
|
MSG["nl,search_prompt"]="Zoek naar pagina's:"
|
||||||
@@ -149,6 +166,9 @@ t() {
|
|||||||
echo "${MSG["$LANG_CODE,$1"]}"
|
echo "${MSG["$LANG_CODE,$1"]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Dependencies
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
ensure_dependency() {
|
ensure_dependency() {
|
||||||
local pkg="$1"
|
local pkg="$1"
|
||||||
if command -v "$pkg" >/dev/null 2>&1; then
|
if command -v "$pkg" >/dev/null 2>&1; then
|
||||||
@@ -168,8 +188,22 @@ check_dependencies() {
|
|||||||
ensure_dependency curl
|
ensure_dependency curl
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Wikipedia-functies
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
TITELS=()
|
TITELS=()
|
||||||
|
|
||||||
|
save_titles() {
|
||||||
|
mkdir -p "$CONFIG_DIR"
|
||||||
|
printf '%s\n' "${TITELS[@]}" > "$CACHE_FILE"
|
||||||
|
}
|
||||||
|
|
||||||
|
load_titles() {
|
||||||
|
if [ -f "$CACHE_FILE" ]; then
|
||||||
|
mapfile -t TITELS < "$CACHE_FILE"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
zoek() {
|
zoek() {
|
||||||
local term="$*"
|
local term="$*"
|
||||||
if [ -z "$term" ]; then
|
if [ -z "$term" ]; then
|
||||||
@@ -193,6 +227,8 @@ zoek() {
|
|||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
save_titles
|
||||||
|
|
||||||
for i in "${!TITELS[@]}"; do
|
for i in "${!TITELS[@]}"; do
|
||||||
echo "$((i + 1)). ${TITELS[$i]}"
|
echo "$((i + 1)). ${TITELS[$i]}"
|
||||||
done
|
done
|
||||||
@@ -247,6 +283,9 @@ set_lang() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Command dispatch (gebruikt door zowel interactieve modus als losse call)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
dispatch() {
|
dispatch() {
|
||||||
local cmd="$1"
|
local cmd="$1"
|
||||||
shift || true
|
shift || true
|
||||||
@@ -284,12 +323,16 @@ interactive_mode() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Main
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
load_config
|
load_config
|
||||||
if [ ! -f "$CONFIG_FILE" ]; then
|
if [ ! -f "$CONFIG_FILE" ]; then
|
||||||
first_run_setup
|
first_run_setup
|
||||||
fi
|
fi
|
||||||
|
|
||||||
check_dependencies
|
check_dependencies
|
||||||
|
load_titles
|
||||||
|
|
||||||
if [ "$#" -eq 0 ]; then
|
if [ "$#" -eq 0 ]; then
|
||||||
interactive_mode
|
interactive_mode
|
||||||
|
|||||||
Reference in New Issue
Block a user