initial commit; repository moved

This commit is contained in:
Valerie Wolfe 2023-07-06 11:07:16 -04:00
commit 2e84e05fe5
16 changed files with 309 additions and 0 deletions

37
README.md Normal file
View file

@ -0,0 +1,37 @@
# Shell Scripts
A handful of scripts I've written to make my life easier on Linux.
## banana-runner
A Python script that runs command shortcuts from an ini file. Quick and easy to set
up.
## codecat
A bash functino that wraps GNU `source-highlight` in a way that isn't a complete
nightmare to use. Automatically sets the `-f`, `-i`, and `--src-lang` flags where
appropriate.
## goto
A local jump bash function. Propagates backwards through the working path to find a
`.goto` ini file, and supports chaining jumps if you need that kind of thing.
## inet
A collection of network related scripts:
- `is-back`: send a notification once the internet connection is online
- `myip`: gets the machine's local IP address
- `weather`: wraps [wttr.in](https://wttr.in) with location
## mountain
Friendly and consistent device mounting/unmounting.
## run
Just runs `.run` in a directory.

7
banana-runner/.banana Normal file
View file

@ -0,0 +1,7 @@
[$]
TEST = foo bar
[test]
command = echo '${TEST}'
shell = true

69
banana-runner/banana-runner.py Executable file
View file

@ -0,0 +1,69 @@
#!/usr/bin/python
from configparser import ConfigParser
from os import chdir, getcwd
from os.path import devnull, expanduser, isdir, isfile
from re import search
from shlex import split
from subprocess import Popen, run
from sys import argv, stdin, stdout, stderr
config = ConfigParser()
CONFIG_PATH = f'{getcwd()}/.banana'
if not isfile(CONFIG_PATH):
print("banana-runner: no config found!")
exit(1)
config.read(CONFIG_PATH)
sections = config.sections()
sections.remove("$")
if len(argv) == 1 or argv[0] == "$":
print("banana-runner: no arguments given")
exit(1)
if argv[1] == '-l' or argv[1] == '--list':
# do thing
for section in sections:
if section == '$':
continue
print(f'{section}')
exit()
name = argv[1]
if name not in sections:
print(f'banana-runner: no section {name} found')
exit(1)
section = config[name]
cmd = section['command']
alias_list = config['$']
def sub_aliases(string):
output = string
for alias in alias_list:
substring = '${' + alias.upper() + "}"
if substring in output:
output = output.replace(substring, alias_list[alias])
return output
cmd = sub_aliases(cmd)
if 'target' in section:
tdir = sub_aliases(section['target'])
tdir = expanduser(tdir)
if isdir(tdir):
chdir(tdir)
else:
if isfile(tdir):
print(f'banana-runner: "{tdir}" is not a directory!')
else:
print(f'banana-runner: "{tdir}" does not exist!')
exit(1)
print(f"{cmd}")
if 'shell' in section and section['shell']:
run(split(cmd))
else:
with open(devnull, 'w') as _:
Popen(cmd, shell = True, stdout = _, stderr = _)

View file

@ -0,0 +1,17 @@
#!/usr/bin/xonsh
from configparser import ConfigParser
config = ConfigParser()
CONFIG_PATH = "$(pwd)/.banana"
if not isfile(CONFIG_PATH):
echo "banana-runner: no config found!")
exit 1
config.read(CONFIG_PATH)
sections = config.sections()
sections.remove('$')
if len($($!)) == 1 or $($1) == '$':
echo 'banana-runner: no arguments given'

15
goto/README.md Normal file
View file

@ -0,0 +1,15 @@
# Goto
I don't like how jump works, so I made a different solution: I got tired of using cd to go through structured directories for development, so my `goto` uses a `.goto` ini file in the root directory of a project to store aliases for directories. For a sample configuration:
```ini
[goto]
root = .
foo = ./some/long/directory/path/
bar = ./some/other/long/path/
```
`goto foo` replaces `cd some/long/directory/path/`, and from `some/long/directory/path/` to get back to the "root" we put the file at, `goto root` replaces `cd ../../../../`.
## Installing goto
just move the two scripts into `/usr/lib/goto/` and add `source /usr/lib/goto/goto.sh` to your shell `rc` file.

19
goto/goto.sh Executable file
View file

@ -0,0 +1,19 @@
#!/bin/bash
function goto() {
if [ "$1" == "-l" ]; then
/usr/lib/goto/resolve "$@"
return
fi
if [[ $# > 1 ]]; then
for name in "$@"
do
jump=$(/usr/lib/goto/resolve "$name")
builtin cd "$jump"
done
return
fi
td=$(/usr/lib/goto/resolve "$@")
builtin cd "$td"
return
}

35
goto/resolve Executable file
View file

@ -0,0 +1,35 @@
#!/usr/bin/python
from configparser import ConfigParser
from os import chdir, getcwd
from os.path import isfile
from pathlib import Path
from signal import signal, SIGPIPE, SIG_DFL
from sys import argv
config = ConfigParser()
FILE = f'{getcwd()}/.goto'
while not isfile(FILE):
if(getcwd() == '/'):
exit()
else:
chdir(Path(getcwd()).parent)
FILE = f'{getcwd()}/.goto'
config.read(FILE)
places = config['goto']
if len(argv) == 0:
exit()
if argv[1] == '-l' or argv[1] == '--list':
for title in places:
print(f'{title} -> {places[title]}')
exit()
if argv[1] not in places:
exit()
path = places[argv[1]]
if path.startswith('./'):
path = f'{str(Path(FILE).parent)}/{path[2:]}'
signal(SIGPIPE, SIG_DFL)
print(path)

14
inet/README.md Normal file
View file

@ -0,0 +1,14 @@
# Networking-related scripts
## is-back
Pings `archlinux.org` until successful. Sends a notification when it is.
## myip
Check a device's IP address with way less typing.
## weather
Just curls [wttr.in](https://wttr.in) with the location stored in `~/.config/weather_location`.

12
inet/is-back Executable file
View file

@ -0,0 +1,12 @@
#!/usr/bin/bash
while true; do
if (ping archlinux.org -W 5 -c 1); then
echo "Ping responded; internet is up."
notify-send "Internet is up." --app-name="Ping Test" --icon="$(pwd)/is-back-icon.png"
exit 0
else
continue
fi
done

BIN
inet/is-back-icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

3
inet/myip Executable file
View file

@ -0,0 +1,3 @@
#!/usr/bin/bash
ip addr | grep -Po '\d+\.\d+\.\d+\.\d+/24' | awk '{ print substr($1, 0, length($1) - 3) }'

6
inet/weather Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/bash
location="$(cat ~/.config/weather_location)"
curl "https://wttr.in/$location?0mQ"

36
mountain/mountain Executable file
View file

@ -0,0 +1,36 @@
#!/usr/bin/bash
TEXT=$(/usr/lib/mountain/mountain-handle $1)
CODE=$?
if [[ $CODE -eq 1 ]]; then
echo "mountain: no device supplied"
exit 1
elif [[ $CODE -eq 2 ]]; then
echo "mountain: no settings for \"$1\""
exit 2
fi
read -t 1 -ra PROP <<< $TEXT
DEV=${PROP[0]}
DIR=${PROP[1]}
mount | grep "$DEV" >> /dev/null
EXISTS=$?
if [[ "$2" == "u" ]]; then
if [[ $EXISTS -ne 0 ]]; then
echo "mountain: $DEV is not mounted"
exit 2
fi
echo "Unmounting $DIR"
sudo umount $DIR
exit 0
fi
if [[ $EXISTS -ne 1 ]]; then
echo "mountain: $DEV is already mounted"
exit 2
fi
echo "Mounting $DEV at $DIR"
sudo mount $DEV $DIR

24
mountain/mountain-handle Executable file
View file

@ -0,0 +1,24 @@
#!/usr/bin/python
from configparser import ConfigParser
from os import environ
from os.path import expanduser
from sys import argv
if len(argv) == 1:
exit(1)
config = ConfigParser()
CONFIG_PATH = expanduser("~/.config/mountain.ini")
config.read(CONFIG_PATH)
sections = config.sections()
name = argv[1]
if name not in sections:
exit(2)
if config.has_option(name, "alias"):
name = config[name]["alias"]
dev = config[name]["dev"]
dir = config[name]["dir"]
print(dev, dir)

5
run/README.md Normal file
View file

@ -0,0 +1,5 @@
# run
Dead-simple script to run `./.run` with all arguments passed through (`$@` in bash).
I mostly use it for build scripts and running games.

10
run/run Executable file
View file

@ -0,0 +1,10 @@
#!/bin/bash
if [[ -f ./.run ]]; then
./.run $@
exit $?
else
echo "run: ./.run does not exist"
exit 1
fi