From 1e68e5034d70a648d08ead135826becc1a6f8ee2 Mon Sep 17 00:00:00 2001 From: hellerve Date: Mon, 24 Jun 2019 14:51:44 +0200 Subject: [PATCH] initial --- Makefile | 9 +++++ README.md | 59 +++++++++++++++++++++++++++++ alacritty_config.py | 91 +++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 4 files changed, 160 insertions(+) create mode 100644 Makefile create mode 100644 README.md create mode 100644 alacritty_config.py create mode 100644 requirements.txt diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7637339 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +INSTALL_DIR=/usr/local/bin + +install: + pip3 install -Ur requirements.txt + cp alacritty_config.py ${INSTALL_DIR}/alacritty-config + chmod u+x ${INSTALL_DIR}/alacritty-config + +uninstall: + rm ${INSTALL_DIR}/alacritty-config diff --git a/README.md b/README.md new file mode 100644 index 0000000..d47b179 --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +# alacritty-config + +A simple Python script to simplify configuring my alacritty. + +## Installation + +You can install `alacritty-config` by cloning this repository and running `make +install`. By default, it will be installed in `/usr/local/bin`, but you can set a +different `INSTALL_DIR` if you like. + +Uninstall it using `make uninstall`. + +## Usage + +`alacritty-config` has three different modes: show, add, and delete. + +Calling the program without any arguments will dump your entire config: you’re in show +mode, but you haven’t selected anything. If you want to know the value of the `tabspaces` +property, call `alacritty-config` with just that argument. If you want to know the action +of your third key binding, call it with `key_bindings.2.action`—we’re 0-indexed. + +``` +$ alacritty-config + + +$ alacritty-config tabspaces +8 # or whatever you set it to + +$ alacritty-config key_bindings.2.action +Paste # or whatever you set it to +``` + +If you want to delete a property, simply add `delete`. + +``` +$ alacritty-config tabspaces delete +# Poof! It’s gone! +``` + +If you want to add or edit a property, simply add a different YAML-parseable property. +If you want to set something to `delete`, simply quote it. + +``` +$ alacritty-config tabspaces 4 +# Set tabspaces to 4 + +$ alacritty-config tabspaces '"delete"' +# Set tabspaces to "delete", which won’t work +``` + +## TODO + +Oh, so much! Ideally, I’d like a GUI at least for all the colorful properties and for +selecting fonts. For now the tool is simple and usable enough to be useful, but a simple +GUI would definitely give this a better UX. Another time. + +
+ +Have fun! diff --git a/alacritty_config.py b/alacritty_config.py new file mode 100644 index 0000000..3579d94 --- /dev/null +++ b/alacritty_config.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 +import os +import sys + +import yaml + + +ALACRITTY_CONFIG = os.path.expanduser("~/.config/alacritty/alacritty.yml") + + +class Action: + mutating = False + def __init__(self, path): + self.path = path + + def traverse(self, yml): + for p in self.path: + yml = yml[p] + return yml + + +class ShowAction(Action): + def act_on(self, yml): + yml = self.traverse(yml) + if type(yml) is dict: + print(yaml.dump(yml)) + else: + print(yml) + + +class EditAction(Action): + mutating = True + def __init__(self, path): + self.path = path[:-1] + self.prop = path[-1] + + +class AddAction(EditAction): + def __init__(self, path, val): + super().__init__(path) + self.val = yaml.safe_load(val) + + def act_on(self, yml): + yml = self.traverse(yml) + yml[self.prop] = self.val + + +class DeleteAction(EditAction): + def act_on(self, yml): + yml = self.traverse(yml) + del yml[self.prop] + + +def parse_path(p): + if p.isdigit(): + return int(p) + return p + + +def parse_action(): + args = sys.argv + + if len(args) == 1: + return ShowAction([]) + + path = [parse_path(a) for a in args[1].split('.')] + if len(args) == 2: + return ShowAction(path) + + if len(args) == 3 and args[2] == 'delete': + return DeleteAction(path) + + return AddAction(path, args[2]) + + +def main(): + action = parse_action() + + try: + with open(ALACRITTY_CONFIG) as f: + y = yaml.safe_load(f.read()) + action.act_on(y) + if action.mutating: + with open(ALACRITTY_CONFIG, "w+") as f: + f.write(yaml.dump(y)) + except KeyError as e: + print('Key "{}" was not found in alacritty config!'.format(e)) + + +if __name__ == '__main__': + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c3726e8 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pyyaml