dots/xsh/aliases.xsh

169 lines
4.5 KiB
Plaintext

import lib
source ~/xsh/functions.xsh
def _debug():
if $XONSH_SHOW_TRACEBACK:
$XONSH_SHOW_TRACEBACK = False
print("Debug mode disabled.")
else:
$XONSH_SHOW_TRACEBACK = True
print("Debug mode enabled.")
def bw_get(object: str, bw_id: str, nulled: bool = False):
if nulled and in_win():
return !(bw get @(object) @(bw_id) a> nul)
elif nulled and not in_win():
return !(bw get @(object) @(bw_id) a> /dev/null)
return $(bw get @(object) @(bw_id))
def _bwc(args: list):
requires("bw")
if not p'~/.bw_session'.exists():
raise FileNotFoundError("~/.bw_session")
source ~/.bw_session
if args[0] == "full": # determine which keys to search for.
items = ["username", "password", "totp"]
elif args[0] == "password":
items = ["password", "totp"]
else:
items = [args[0]]
for i in items: # removes keys that don't exist for the object.
if not bw_get(i, args[1], True):
items.remove(i)
print(f"found: {', '.join(items)}")
for i in items: # actually copy the info to clipboard.
output = bw_get(i, args[1])
to_clipboard(output)
if items.index(i) < len(items) - 1:
input(f"copied {i}, press enter to continue")
else:
print(f"copied {i}")
def _backup(args: list):
from pathlib import Path
target_path = Path(args[0])
max_backup_count: int = 3
if len(args) == 2 and isinstance(args[1], int):
max_backup_count = int(args[1])
bm = lib.BackupManager(target_path)
bm.create_backup()
bm.delete_excess_backups(max_backup_count)
def _mcrcon(args: list):
import json
try:
with p"~/.mcrcon.json".open("r") as fp:
servers_data = json.load(fp)
server_data = servers_data[args[0]]
$MCRCON_HOST = server_data["address"]
$MCRCON_PORT = server_data["port"]
$MCRCON_PASS = server_data["password"]
$(mcrcon @(" ".join(args[1:])))
except IndexError:
print("Please indicate which server to connect to.")
except KeyError:
print("Server configuration not found.")
except FileNotFoundError:
print("Server configuration file '.mcrcon.json' is not found.")
def _mcterm(args: list):
import json
try:
with p"~/.mcrcon.json".open("r") as fp:
servers_data = json.load(fp)
server_data = servers_data[args[0]]
$MCRCON_HOST = server_data["address"]
$MCRCON_PORT = server_data["port"]
$MCRCON_PASS = server_data["password"]
mcrcon -t
except IndexError:
print("Please indicate which server to connect to.")
except KeyError:
print("Server configuration not found.")
except FileNotFoundError:
print("Server configuration file '.mcrcon.json' is not found.")
def _mclist():
import json
try:
with p"~/.mcrcon.json".open("r") as fp:
servers_data = json.load(fp)
print("Servers:")
for key in servers_data:
print("\t-", key)
except FileNotFoundError:
print("Server configuration file '.mcrcon.json' is not found.")
def _alias():
for alias in aliases:
if callable(aliases[alias]):
print(alias + " = ", aliases[alias].__name__)
else:
print(alias + " =", " ".join(aliases[alias]))
def _ensure_tmux(args: list):
if $XONSH_SHOW_TRACEBACK:
print(args)
if not $(tmux has-session -t @(args[0])):
tmux new-session -d -s @(args[0]) all> /dev/null
$(tmux send-keys -t @(args[0]) @(f"cd {args[1]}") C-m)
$(tmux send-keys -t @(args[0]) @(f"{args[2]}") C-m)
def _colortest():
import sys
for i in range(256):
sys.stdout.write(f"\033[48;5;{i}m ")
if (i+1) % 16 == 0:
sys.stdout.write("\033[0m\n")
def _ls():
from colorama import Fore, Back, Style
line_list = []
command = $(cmd /c dir)
for line in command.split("\n"):
line = [x for x in line.split(" ") if x]
if "<DIR>" in line:
line[-1] = f"{Fore.GREEN}{line[-1]}{Style.RESET_ALL}"
line = " ".join(line)
line_list.append(line)
[[print(x) for x in line_list]]
def load_aliases():
aliases.update({
'bu': _backup,
'backup': _backup,
'bwg': _bwc,
'colortest': _colortest,
'debug': _debug,
'ls': 'ls -alhs --color=auto',
'mc': _mcrcon,
'mct': _mcterm,
'mcl': _mclist,
':q': 'exit',
'ensure-tmux': _ensure_tmux,
'aliases': _alias,
})
# WSL specific aliases
if in_wsl():
aliases.update({
'wh': "cd /mnt/c/Users/Riley",
})
# Windows specific aliases
if in_win():
aliases.update({
'config': 'git.exe --git-dir=$USERPROFILE/.cfg/ --work-tree=$USERPROFILE',
'ls': ['cmd', '/c', 'dir'], # get past namespace conflict with python's dir() function.
'lsn': _ls,
'lofi': ['powershell.exe', 'firefox.ps1', '-url', 'https://www.youtube.com/watch?v=5qap5aO4i9A/'],
})
if not in_win():
aliases.update({
'config': '/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME',
})