Skip to content

Commit

Permalink
Initial commit (so long...)
Browse files Browse the repository at this point in the history
  • Loading branch information
xezo360hye committed Mar 28, 2022
0 parents commit 5ff1b20
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
img*
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# What is Conics?

Conics `(CONsole comICS)` is a program that helps you watch comics from `xkcd.com` right in your TTY, without even starting the X server!


# Installation

## Step 1 - dependencies

Conics uses FBI (**F**rame**B**uffer **I**mageviewer) and cURL so install them

Arch (FBI is in AUR):

`yay -S curl fbida`

Debian, Ubuntu, Mint:

`sudo apt-get install fbi`

Other:

Idk, please tell me if you know

## Step 2 - main

Run the following commands to install this program:

```
# Download
git clone https://github.com/xezo360hye/Conics
# Make it executable
chmod +x Conics/conics
# Move to $PATH
sudo mv Conics/conics /usr/local/bin/
# Clean up
rm -rf Conics
```

And you are done!

# Usage

## General

To get help message use `conics -h` or `conics --help` - there's all you need to know about arguments

To navigate through downloaded images, use `J` and `K` keys, or `space` to move forward UNTIL THE LAST ONE REACHED. Press `H` in FBI to get more info

## Options

### Dir (-d)

Use to specify the directory where downloaded images will be stored - for example, `~/Pictures` or `.hidden_memes`. Default folder is *.* (current working directory)

### Format (-f)

Format is used in `mktemp` command, so check it for more information. It needs to end with 3 or more X's that will be replaced with random ~letters and numbers~ alnums. By default format is *img-XXXXX*

### Count (-c)

Count of comics you want to see. If not used, only one will be shown *(have you done your homework yet?)*

### Time (-t)

Great feature of FBI: if you don't press any key in <> seconds, it automatically goes to the next one. Closes when reached to the end

### Remove (-r)

Use it with or without argument. If next arg after -r is `true` or `false`, conics uses it, but if not it'll set it to *true*. However, the default value is *false*
106 changes: 106 additions & 0 deletions conics
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/bin/bash

usage() {
echo "\
Usage: conics [ OPTIONS ]
Available options:
-h --help Show this message and exit
-d --dir STR Directory where images will be saved
-f --format STR Format of files (must end with 3+ X)
-c --count INT Count of images to download
-t --time INT Next image if no user input within TIME seconds
-r --remove [B] Remove images after closing FBI (if no ARG after, set to True)
Default FORMAT is img-XXXXX, DIR is . (current) and COUNT is 1"

exit $1
}


# Default options
declare FORMAT="img-XXXXX" DIR="." REMOVE=false
declare -i COUNT=1 TIME=5


# Parse args
while [[ -n "$1" ]]; do
case "$1" in
-h|--help)
usage 0;;
-d|--dir)
if [[ -d "$DIR" ]]; then
DIR="$2"
shift 2
else
echo "Directory not found: $DIR"
exit 4
fi
;;
-f|--format)
if [[ "$2" =~ ^.*XXX+$ ]]; then
FORMAT="$2"
shift 2
else
echo "Invalid format: $2\nRun 'conics -h'"
exit 3
fi
;;
-c|--count)
if [[ "$2" =~ ^[0-9]+$ ]] && (( "$2" > 0 )); then
COUNT="$2"
shift 2
else
echo "Option 'count' requires INT > 0"
exit 2
fi
;;
-t|--time)
if [[ "$2" =~ ^[0-9]+$ ]] && (( "$2" > 0 )); then
TIME="$2"
shift 2
else
echo "Option 'time' requires INT > 0"
exit 5
fi
;;
-r|--remove)
if [[ "$2" =~ ^(true|false)$ ]] && (( "$2" > 0 )); then
REMOVE="$2"
shift 2
else
REMOVE=true
shift
fi
;;
--)
break;;
*)
echo "Unknown option: $1"
usage 1;;
esac
done


# Check if directory is writable
if [[ ! -w "$DIR" ]]; then
echo "Directory is unwritable"
exit 3
fi


# Download files
while (( COUNT-- )); do
files+=( $(mktemp "$DIR/$FORMAT") )
curl -so "${files[-1]}" "$(curl -s "https://xkcd.com/$(( $RANDOM % 2599 ))/info.0.json" | jq . | grep -o '"img": ".*"' | grep -o 'h[^"]*')" &
done
wait -n


# Watch memes ^_^
fbi -t "$TIME" -1 -a -l <(echo "${files[@]}" | tr " " "\n") &> /dev/null

# Remove files
if $REMOVE; then
rm -rf "${files[@]}"
fi

0 comments on commit 5ff1b20

Please sign in to comment.