commit c5e2010d94e724a23a9857a7ff923ad836627981 Author: Abreu Date: Mon Aug 9 23:04:01 2021 -0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..666b69b --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +* +!README.md +!LICENSE +!*/ +!**.fish +!.gitignore +dependency.fish \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0b67191 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 {{USER}} + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..8e8b1f1 --- /dev/null +++ b/README.md @@ -0,0 +1,58 @@ +[![GPL License](https://img.shields.io/badge/license-GPL-blue.svg?longCache=true&style=flat-square)](/LICENSE) +[![Fish Shell Version](https://img.shields.io/badge/fish-v3.0.1-blue.svg?style=flat-square)](https://fishshell.com) +[![Oh My Fish Framework](https://img.shields.io/badge/Oh%20My%20Fish-Framework-blue.svg?style=flat-square)](https://www.github.com/oh-my-fish/oh-my-fish) + +# uc + +> A plugin for [Oh My Fish](https://www.github.com/oh-my-fish/oh-my-fish) + +uc (unsplash collections) allows one to list photo collections on unsplash from where to draw a wallpaper at random. + +## Options + +``` +uc [collection number] +Get a random image from a collection and set it as the new background and screensaver wallpaper. If no collection is specified, a random listed collection will be used instead. + +uc -a/--add [collection number] ... +Add a new collection to the list. + +uc -r/--remove [collection number] ... +Remove a collection from the list. + +uc -u/--url [collection number] ... +Present the urls from specified collections. + +uc -f/-folder [directory] +Set where images are stored. If no directory is passed, display where they're currently being stored. + +uc -c/--cache [number] +Set the size of the image cache. + +uc -l/--list [collection/size/description] +Show listed collections. + +uc -h/--help +Display these instructions. +``` + +## Install + +```fish +omf repositories add https://gitlab.com/argonautica/argonautica +omf install uc +``` + +### Dependencies + +This plugin uses [unsplash_wallpaper](https://github.com/cuth/unsplash-wallpaper) by [cuth](https://github.com/cuth) to get wallpapers. If you don't have it installed, you'll be prompted to install it upon installing of this plugin. + +## Configuration + +This script is optimized to work with environment variables available to [cron](https://en.wikipedia.org/wiki/Cron), so that you can change your background and lockscreen wallpapers periodically and automatically. As an example, to change them every 3 hours, use the command `crontab -e` and append the following line: + +``` +0 */3 * * * .local/share/omf/pkg/uc/functions/uc.fish +``` + +For more information on how to schedule tasks using cron, consult this [video](https://www.youtube.com/watch?v=8j0SWYNglcw). diff --git a/completions/uc.fish b/completions/uc.fish new file mode 100644 index 0000000..97b59a9 --- /dev/null +++ b/completions/uc.fish @@ -0,0 +1,28 @@ +# Load dependency +source -- (command dirname (status -f))/../dependency.fish -P https://gitlab.com/argonautica/contains_opts + +# Set variables +set -l cmd (command basename (status -f) | command cut -f 1 -d '.') +set -l flags u url a add r remove f folder c cache l list h help +set -l list (eval $cmd -l | tail +2 | string match -ar '\S+') +set -l numbers $list[(command seq 1 3 (count $list))] +set -l names $list[(command seq 3 3 (count $list))] + +# Add completions +complete -xc $cmd -n 'not contains_opts' -s u -l url \ +-d "Present the urls of specified collections." +complete -xc $cmd -n 'not contains_opts' -s a -l add \ +-d 'Add a new collection to the list' +complete -xc $cmd -n 'not contains_opts' -s r -l remove \ +-d 'Remove a collection from the list' +complete -rc $cmd -n 'not contains_opts' -s f -l folder \ +-d 'Set where images are stored' +complete -xc $cmd -n 'not contains_opts' -s c -l cache \ +-d 'Set the size of the image cache' +complete -c $cmd -n 'not contains_opts' -s l -l list \ +-d 'Show listed collections' +complete -c $cmd -n 'not contains_opts' -s h -l help \ +-d 'Display instructions' +for i in (command seq (count $numbers)) + complete -fc $cmd -n "not contains_opts (string match -rv -- '^(u|url|r|remove)\$' $flags)" -a "$numbers[$i]" -d "$names[$i]" +end diff --git a/functions/uc.fish b/functions/uc.fish new file mode 100755 index 0000000..43020a8 --- /dev/null +++ b/functions/uc.fish @@ -0,0 +1,172 @@ +#!/usr/bin/fish + +# Load dependencies +set -l cmd (command basename (status -f) | command cut -f 1 -d '.') +set -l path (status filename | command xargs dirname)/.. +source $path/dependency.fish \ +-n $cmd -N unsplash-wallpaper grep sed curl +or exit 1 + +# Set variables +set -l collections_list $path/collections_list +set --query wallpapers_folder +or set -l wallpapers_folder $HOME/Pictures/wallpapers +set --query wallpaper_cache +or set -l wallpaper_cache 10 + +# Parse arguments +if argparse -n $cmd -x a,r,u,f,c,l,h 'a/add' 'r/remove' 'u/url' 'f/folder=' 'c/cache=' 'l/list' 'h/help' -- $argv 2>&1 | read err + err $err + reg "Use |$cmd -h| to see examples of valid syntaxes" + exit 1 +end + +# Check for flags, arguments, and collection lists +set -l flag (set --name | string match -r '(?<=^_flag_).{2,}') +if string match -qr '^(add|remove|url)$' $flag + if test -z "$argv" + err "$cmd: Missing argument" + source "$path/instructions.fish $cmd -a/--\S+" + exit 1 + end +end +if string match -qr '^(remove|url|list|)$' $flag + if not test -s "$collections_list" + err "$cmd: No collections list currently exists" + reg "Start one using |$cmd --add|" + exit 1 + end +end + +switch "$flag" + case help + source "$path/instructions.fish $cmd -a/--\S+" + test -z "$argv" + + case list + command cat $collections_list + test -z "$argv" + + case url + command printf '%s\n' https://unsplash.com/collections/{(string join , $argv)} + + case folder + if test -z "$argv" + echo "$wallpapers_folder" + exit 0 + end + string match "$wallpapers_folder" "$argv" + and exit 0 + if command mkdir -p "$argv" 2>&1 | read err + err "$cmd: "(string match -r '(?<=mkdir: ).+'$err) + exit 1 + end + command mv $wallpapers_folder/* "$argv" 2>/dev/null + set -U wallpapers_folder "$argv" + win "Wallpapers folder set to |$wallpapers_folder|" + + case cache + if string match -qvr '\d' $_flag_cache + err "$cmd: $_flag_cache: Invalid value" + source "$path/instructions.fish $cmd -a/--\S+" + exit 1 + end + set -U wallpaper_cache $_flag_cache + win "Wallpaper cache size se to |$wallpaper_cache|" + + case remove + set -l list_length (cat $collections_list | command wc -l) + for collection in $argv + command sed -ie "2,\${ /^$collection\b/d;/\b$collection\$/d }" $collections_list + end + test (command cat $collections_list | command wc -l) -eq $list_length + and dim "None of the collections passed is currently listed" + or win "Collections removed" + + case add + + # Verify argument validity + if echo (math (count $argv) / 3) $argv[(command seq 2 3 (count $argv))] \ + | string match -qvr '\d' + err "$cmd: Invalid syntax" + source "$path/instructions.fish $cmd -a/--\S+" + exit 1 + end + + # Check collection availability + for i in (command seq 1 3 (count $argv) | command sort -r) + if not contains $argv[$i] \ + $collections[(command seq 1 3 (count $collections))] + command curl --connect-timeout 60 -o /dev/null \ + -sIf https://unsplash.com/collections/$argv[$i] + and continue + err "$cmd: $argv[$i]: Collection was not found. Check its number or your connection." + else + dim "Collection |$argv[$i]| already present in the collection list" + end + set --erase argv[$i..(math $i + 2)] + end + test "$argv" + or exit 1 + + # Add contents to collection list + set -l parameters Collection Photocount Name + set -l collections (command tail +2 "$collections_list" 2>/dev/null \ + | string match -ar '\S+') + for i in (command seq (count $parameters)) + echo $parameters[$i] > "$PREFIX"/tmp/$parameters[$i] + command printf '%s\n' $collections[(seq $i 3 (count $collections))] \ + $argv[(command seq $i 3 (count $argv))] \ + >> "$PREFIX"/tmp/$parameters[$i] + end + command pr -mt "$PREFIX"/tmp/{(string join , $parameters)} > "$collections_list" + command rm "$PREFIX"/tmp/{(string join , $parameters)} + + case '' + + # Select a listed collection at random if none was passed + if test -z "$argv" + set argv (tail +2 "$collections_list" 2>/dev/null \ + | string match -ar '\S+') + if test -z "$argv" + err "$cmd: No collection described and no collection list available" + exit 1 + end + set argv $argv[(command seq 2 3 (count $argv))] + for i in (command seq 2 (count $argv)) + set argv[$i] (math $argv[$i] + $argv[(math $i - 1)]) + end + set -l lottery (random 1 $argv[-1]) + for i in (command seq (count $argv)) + test $lottery -le $argv[$i] + or continue + set -l tmp (grep -oP '^\d+' "$collections_list") + set argv $tmp[$i] + break + end + end + + # Retrieve wallpaper and, if possible, set it as the current wallpaper + command mkdir -p $wallpapers_folder + cd $wallpapers_folder + eval (command whereis unsplash-wallpaper | awk '{print $2}') -o $argv 1>&2 + or exit 1 + set wallpapers (ls -t | string match -ar '^wallpaper-.+\.jpe?g$') + if type -qf gsettings + set -l PID (command pgrep gnome-session) + set -x DBUS_SESSION_BUS_ADDRESS \ + (command grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ | command cut -d= -f2-) + command gsettings set org.gnome.desktop.background picture-uri \ + file://(command realpath $wallpapers[1]) + command gsettings set org.gnome.desktop.screensaver picture-uri \ + file://(command realpath $wallpapers[1]) + else if type -qf termux-wallpaper + command termux-wallpaper -f $wallpapers[1] >/dev/null 2>&1 + command termux-wallpaper -lf $wallpapers[1] + end + + # Delete old wallpapers according to cache size + test (count $wallpapers) -gt $wallpaper_cache + and command rm $wallpapers[(math $wallpaper_cache + 1)..-1] + prevd +end diff --git a/hooks/install.fish b/hooks/install.fish new file mode 100644 index 0000000..c8b1cee --- /dev/null +++ b/hooks/install.fish @@ -0,0 +1,3 @@ +command wget -qO $path/dependency.fish \ +https://gitlab.com/argonautica/dependency/raw/master/dependency.fish +source "$path/dependency.fish" -n $package -N unsplash-wallpaper grep sed curl diff --git a/hooks/uninstall.fish b/hooks/uninstall.fish new file mode 100644 index 0000000..d20099e --- /dev/null +++ b/hooks/uninstall.fish @@ -0,0 +1 @@ +source "$path/dependency.fish" -rN unsplash-wallpaper grep sed curl \ No newline at end of file diff --git a/init.fish b/init.fish new file mode 100644 index 0000000..3cfcbd6 --- /dev/null +++ b/init.fish @@ -0,0 +1 @@ +alias uc "fish $OMF_PATH/pkg/uc/functions/uc.fish" \ No newline at end of file diff --git a/instructions.fish b/instructions.fish new file mode 100644 index 0000000..7713851 --- /dev/null +++ b/instructions.fish @@ -0,0 +1,37 @@ +set -l bld (set_color 00afff -o) +set -l reg (set_color normal) +set -l instructions $bld"uc - unsplash collections + +"$bld"DECRIPTION + +List unsplash photo collections from where to get a random wallpaper. + +"$bld"OPTIONS + +"$bld"uc"$reg" [collection number] +Get a random image from a collection and set it as the new background and screensaver wallpaper. If no collection is specified, a random listed collection will be used instead. + +"$bld"uc"$reg" -a/--add [name] [collection number] [photocount] ... +Add a new collection to the list. Its name must not contain whitespaces. + +"$bld"uc"$reg" -r/--remove [collection number] ... +Remove a collection from the list. + +"$bld"uc"$reg" -u/--url [collection number] ... +Present the urls of specified collections. + +"$bld"uc"$reg" -f/-folder [directory] +Set where images are stored. If no directory is passed, display where they are currently being stored. + +"$bld"uc"$reg" -c/--cache [number] +Set the size of the image cache. + +"$bld"uc"$reg" -l/--list [collection/size/description] +Show listed collections. + +"$bld"uc"$reg" -h/--help +Display these instructions. +" +string match -- '*/--\S+' "$argv" +and echo $instructions | grep -A 1 -E "$argv" 1>&2 +or echo $instructions | less -R