diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rwxr-xr-x | tama | 644 |
2 files changed, 644 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 4a1e02a..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/tama @@ -0,0 +1,644 @@ +#!/usr/bin/env bash +# This script was generated by bashly 1.0.5 (https://bashly.dannyb.co) +# Modifying it manually is not recommended + +# :wrapper.bash3_bouncer +if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then + printf "bash version 4 or higher is required\n" >&2 + exit 1 +fi + +# :command.master_script + +# :command.version_command +version_command() { + echo "$version" +} + +# :command.usage +tama_usage() { + if [[ -n $long_usage ]]; then + printf "tama - Command-line Hiruki Client\n" + echo + + else + printf "tama - Command-line Hiruki Client\n" + echo + + fi + + printf "%s\n" "Usage:" + printf " tama COMMAND\n" + printf " tama [COMMAND] --help | -h\n" + printf " tama --version | -v\n" + echo + # :command.usage_commands + printf "%s\n" "Commands:" + printf " %s \n" "trending" + printf " %s \n" "popular " + printf " %s \n" "upcoming" + printf " %s \n" "search " + echo + + # :command.long_usage + if [[ -n $long_usage ]]; then + printf "%s\n" "Options:" + + # :command.usage_fixed_flags + printf " %s\n" "--help, -h" + printf " Show this help\n" + echo + printf " %s\n" "--version, -v" + printf " Show version number\n" + echo + + fi +} + +# :command.usage +tama_trending_usage() { + if [[ -n $long_usage ]]; then + printf "tama trending\n" + echo + + else + printf "tama trending\n" + echo + + fi + + printf "Alias: t\n" + echo + + printf "%s\n" "Usage:" + printf " tama trending\n" + printf " tama trending --help | -h\n" + echo + + # :command.long_usage + if [[ -n $long_usage ]]; then + printf "%s\n" "Options:" + + # :command.usage_fixed_flags + printf " %s\n" "--help, -h" + printf " Show this help\n" + echo + + fi +} + +# :command.usage +tama_popular_usage() { + if [[ -n $long_usage ]]; then + printf "tama popular\n" + echo + + else + printf "tama popular\n" + echo + + fi + + printf "Alias: p\n" + echo + + printf "%s\n" "Usage:" + printf " tama popular\n" + printf " tama popular --help | -h\n" + echo + + # :command.long_usage + if [[ -n $long_usage ]]; then + printf "%s\n" "Options:" + + # :command.usage_fixed_flags + printf " %s\n" "--help, -h" + printf " Show this help\n" + echo + + fi +} + +# :command.usage +tama_upcoming_usage() { + if [[ -n $long_usage ]]; then + printf "tama upcoming\n" + echo + + else + printf "tama upcoming\n" + echo + + fi + + printf "Alias: u\n" + echo + + printf "%s\n" "Usage:" + printf " tama upcoming\n" + printf " tama upcoming --help | -h\n" + echo + + # :command.long_usage + if [[ -n $long_usage ]]; then + printf "%s\n" "Options:" + + # :command.usage_fixed_flags + printf " %s\n" "--help, -h" + printf " Show this help\n" + echo + + fi +} + +# :command.usage +tama_search_usage() { + if [[ -n $long_usage ]]; then + printf "tama search\n" + echo + + else + printf "tama search\n" + echo + + fi + + printf "Alias: s\n" + echo + + printf "%s\n" "Usage:" + printf " tama search [QUERY...]\n" + printf " tama search --help | -h\n" + echo + + # :command.long_usage + if [[ -n $long_usage ]]; then + printf "%s\n" "Options:" + + # :command.usage_fixed_flags + printf " %s\n" "--help, -h" + printf " Show this help\n" + echo + + # :command.usage_args + printf "%s\n" "Arguments:" + + # :argument.usage + printf " %s\n" "QUERY..." + printf "\n" + echo + + fi +} + +# :command.normalize_input +normalize_input() { + local arg flags + + while [[ $# -gt 0 ]]; do + arg="$1" + if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then + input+=("${BASH_REMATCH[1]}") + input+=("${BASH_REMATCH[2]}") + elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then + input+=("${BASH_REMATCH[1]}") + input+=("${BASH_REMATCH[2]}") + elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then + flags="${BASH_REMATCH[1]}" + for ((i = 0; i < ${#flags}; i++)); do + input+=("-${flags:i:1}") + done + else + input+=("$arg") + fi + + shift + done +} +# :command.inspect_args +inspect_args() { + if ((${#args[@]})); then + readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort) + echo args: + for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done + else + echo args: none + fi + + if ((${#other_args[@]})); then + echo + echo other_args: + echo "- \${other_args[*]} = ${other_args[*]}" + for i in "${!other_args[@]}"; do + echo "- \${other_args[$i]} = ${other_args[$i]}" + done + fi + + if ((${#deps[@]})); then + readarray -t sorted_keys < <(printf '%s\n' "${!deps[@]}" | sort) + echo + echo deps: + for k in "${sorted_keys[@]}"; do echo "- \${deps[$k]} = ${deps[$k]}"; done + fi + +} + +# :command.user_lib +# src/lib/get.sh +get() { + curl --silent --get "https://api-hiruki-xyz.vercel.app${1}" +} + +# src/lib/join.sh +join() { + ARRAY="${1}" + + echo "${ARRAY[*]// / }" | + tr -d '"' +} + +# src/lib/print.sh +function print() { + echo + + get "${1}" | + jq -r '.[] | ["[", .title.romaji, "](https://hiruki.xyz/i/", .id, ")"] | join("")' | + sed 's/^/* /' | + mdcat | + sed 's/^/ /' +} + +# :command.command_functions +# :command.function +tama_trending_command() { + # src/trending_command.sh + print '/trending' + +} + +# :command.function +tama_popular_command() { + # src/popular_command.sh + print '/popular' + +} + +# :command.function +tama_upcoming_command() { + # src/upcoming_command.sh + print '/upcoming' + +} + +# :command.function +tama_search_command() { + # src/search_command.sh + print "/search/$(jq -rn --arg x "$(join "${args[query]}")" '$x | @uri')" + +} + +# :command.parse_requirements +parse_requirements() { + # :command.fixed_flags_filter + while [[ $# -gt 0 ]]; do + case "${1:-}" in + --version | -v) + version_command + exit + ;; + + --help | -h) + long_usage=yes + tama_usage + exit + ;; + + *) + break + ;; + + esac + done + + # :command.dependencies_filter + if command -v curl >/dev/null 2>&1; then + deps['curl']="$(command -v curl | head -n1)" + else + printf "missing dependency: curl\n" >&2 + exit 1 + fi + + if command -v echo >/dev/null 2>&1; then + deps['echo']="$(command -v echo | head -n1)" + else + printf "missing dependency: echo\n" >&2 + exit 1 + fi + + if command -v tr >/dev/null 2>&1; then + deps['tr']="$(command -v tr | head -n1)" + else + printf "missing dependency: tr\n" >&2 + exit 1 + fi + + if command -v jq >/dev/null 2>&1; then + deps['jq']="$(command -v jq | head -n1)" + else + printf "missing dependency: jq\n" >&2 + exit 1 + fi + + if command -v sed >/dev/null 2>&1; then + deps['sed']="$(command -v sed | head -n1)" + else + printf "missing dependency: sed\n" >&2 + exit 1 + fi + + if command -v mdcat >/dev/null 2>&1; then + deps['mdcat']="$(command -v mdcat | head -n1)" + else + printf "missing dependency: mdcat\n" >&2 + exit 1 + fi + + # :command.command_filter + action=${1:-} + + case $action in + -*) ;; + + trending | t) + action="trending" + shift + tama_trending_parse_requirements "$@" + shift $# + ;; + + popular | p) + action="popular" + shift + tama_popular_parse_requirements "$@" + shift $# + ;; + + upcoming | u) + action="upcoming" + shift + tama_upcoming_parse_requirements "$@" + shift $# + ;; + + search | s) + action="search" + shift + tama_search_parse_requirements "$@" + shift $# + ;; + + # :command.command_fallback + "") + tama_usage >&2 + exit 1 + ;; + + *) + printf "invalid command: %s\n" "$action" >&2 + exit 1 + ;; + + esac + + # :command.parse_requirements_while + while [[ $# -gt 0 ]]; do + key="$1" + case "$key" in + + -?*) + printf "invalid option: %s\n" "$key" >&2 + exit 1 + ;; + + *) + # :command.parse_requirements_case + # :command.parse_requirements_case_simple + printf "invalid argument: %s\n" "$key" >&2 + exit 1 + + ;; + + esac + done + +} + +# :command.parse_requirements +tama_trending_parse_requirements() { + # :command.fixed_flags_filter + while [[ $# -gt 0 ]]; do + case "${1:-}" in + --help | -h) + long_usage=yes + tama_trending_usage + exit + ;; + + *) + break + ;; + + esac + done + + # :command.command_filter + action="trending" + + # :command.parse_requirements_while + while [[ $# -gt 0 ]]; do + key="$1" + case "$key" in + + -?*) + printf "invalid option: %s\n" "$key" >&2 + exit 1 + ;; + + *) + # :command.parse_requirements_case + # :command.parse_requirements_case_simple + printf "invalid argument: %s\n" "$key" >&2 + exit 1 + + ;; + + esac + done + +} + +# :command.parse_requirements +tama_popular_parse_requirements() { + # :command.fixed_flags_filter + while [[ $# -gt 0 ]]; do + case "${1:-}" in + --help | -h) + long_usage=yes + tama_popular_usage + exit + ;; + + *) + break + ;; + + esac + done + + # :command.command_filter + action="popular" + + # :command.parse_requirements_while + while [[ $# -gt 0 ]]; do + key="$1" + case "$key" in + + -?*) + printf "invalid option: %s\n" "$key" >&2 + exit 1 + ;; + + *) + # :command.parse_requirements_case + # :command.parse_requirements_case_simple + printf "invalid argument: %s\n" "$key" >&2 + exit 1 + + ;; + + esac + done + +} + +# :command.parse_requirements +tama_upcoming_parse_requirements() { + # :command.fixed_flags_filter + while [[ $# -gt 0 ]]; do + case "${1:-}" in + --help | -h) + long_usage=yes + tama_upcoming_usage + exit + ;; + + *) + break + ;; + + esac + done + + # :command.command_filter + action="upcoming" + + # :command.parse_requirements_while + while [[ $# -gt 0 ]]; do + key="$1" + case "$key" in + + -?*) + printf "invalid option: %s\n" "$key" >&2 + exit 1 + ;; + + *) + # :command.parse_requirements_case + # :command.parse_requirements_case_simple + printf "invalid argument: %s\n" "$key" >&2 + exit 1 + + ;; + + esac + done + +} + +# :command.parse_requirements +tama_search_parse_requirements() { + # :command.fixed_flags_filter + while [[ $# -gt 0 ]]; do + case "${1:-}" in + --help | -h) + long_usage=yes + tama_search_usage + exit + ;; + + *) + break + ;; + + esac + done + + # :command.command_filter + action="search" + + # :command.parse_requirements_while + while [[ $# -gt 0 ]]; do + key="$1" + case "$key" in + + -?*) + printf "invalid option: %s\n" "$key" >&2 + exit 1 + ;; + + *) + # :command.parse_requirements_case + # :command.parse_requirements_case_repeatable + + if [[ -z ${args['query']+x} ]]; then + args['query']="\"$1\"" + shift + else + args['query']="${args[query]} \"$1\"" + shift + fi + + ;; + + esac + done + +} + +# :command.initialize +initialize() { + version="0.1.0" + long_usage='' + set -e + +} + +# :command.run +run() { + declare -A args=() + declare -A deps=() + declare -a other_args=() + declare -a input=() + normalize_input "$@" + parse_requirements "${input[@]}" + + case "$action" in + "trending") tama_trending_command ;; + "popular") tama_popular_command ;; + "upcoming") tama_upcoming_command ;; + "search") tama_search_command ;; + esac +} + +initialize +run "$@" |