#!/usr/bin/env zsh # -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*- # Copyright (c) 2020 Sebastian Gniazdowski # An example of type-agnostic script/function, i.e.: the file can be run as a +x # script or as an autoload function. # Set the base and typically useful options emulate -LR zsh setopt extendedglob typesetsilent warncreateglobal noshortloops rcquotes # Run as script? ZSH_SCRIPT is a Zsh 5.3 addition if [[ $0 != */ghquery || -n $ZSH_SCRIPT ]] { # Handle $0 according to the Zsh Plugin Standard: # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html#zero-handling 0=${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}} 0=${${(M)0##/*}:-$PWD/$0} # Such global variable is expected to be typeset'd -g in the plugin.zsh # file. Here it's restored in case of the function being run as a script. typeset -gA Plugins Plugins[UNSCOPE_DIR]=${0:h} # In case of the script using other scripts from the plugin in the future path+=( $Plugins[UNSCOPE_DIR] ) } typeset -g name=$1 typeset -ga reply full_names typeset -g URL="https://api.github.com/search/repositories?q=$name+in:name+fork:false" #typeset -g CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/zinit-annex-unscope" #command mkdir -p "$CACHE_DIR" download() { local url=$URL"+$1&sort=stars&order=desc" reply=( "${(@f)"$(curl --silent -i "$url")"}" ) } integer forks for forks ( 10 2 0 ) { download "forks:>=$forks" full_names=( ${(M)reply[@]:#[[:space:]]#\"full_name\":[[:space:]]#*,} ) full_names=( "${${(M)full_names[@]//(#b)[[:space:]]#\"full_name\":[[:space:]]#\"(*)\",/${match[1]}}[@]}" ) full_names=( ${(M)full_names[@]:#*/$name} ) if (( ${#full_names} )) { print -r -- $forks: $full_names[1] exit 0 } } exit 1 # vim:ft=zsh:tw=80:sw=4:sts=4:et:foldmarker=[[[,]]]