Allow simple JSON configuration for site sessions

Different terminals need different hotkeys, but I want the kitty
configuration to be shared. Add a blanket json file to allow
creating more advanced configurations per site while keeping the
rest of the config common.
This commit is contained in:
Andrew Mulbrook 2021-02-12 12:35:40 -06:00
parent 42ce0bd066
commit 4cfeea363b
5 changed files with 75 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*.swp

10
efx_site.default Normal file
View file

@ -0,0 +1,10 @@
{ "sessions":
[
{ "title": "System Shell",
"cmd": "/bin/bash --login"
},
{ "title": "C Shell",
"cmd": "/bin/csh"
}
]
}

View file

@ -7,6 +7,8 @@
- link:
~/.bashrc: bashrc
~/.vimrc: vimrc
~/.config/kitty/kitty.conf: kitty.conf
~/.scripts: scripts
- shell:
- [git submodule update --init --recursive, Installing submodules]

12
kitty.conf Normal file
View file

@ -0,0 +1,12 @@
map ctrl+shift+1 new_tab efx_session 0
map ctrl+shift+2 new_tab efx_session 1
map ctrl+shift+3 new_tab efx_session 2
map ctrl+shift+4 new_tab efx_session 3
map ctrl+shift+5 new_tab efx_session 4
map ctrl+shift+6 new_tab efx_session 5
map ctrl+shift+7 new_tab efx_session 6
map ctrl+shift+8 new_tab efx_session 7
map ctrl+shift+9 new_tab efx_session 8

49
scripts/efx_session Executable file
View file

@ -0,0 +1,49 @@
#!/usr/bin/env python3
import os
import shlex
from pathlib import Path
from typing import List, Optional
import argparse
import json
class EfxSession:
def __init__(self, cfg):
self.cmd = cfg.get('cmd', '/bin/bash --login')
self.title = cfg.get('title', 'System Shell')
def execute(self):
cmd_val = shlex.split(self.cmd)
os.execvp(cmd_val[0], cmd_val)
class EfxConfig:
def __init__(self, cfg):
self._sessions = _attr_sessions(cfg)
def get_session(self, num: int) -> Optional[EfxSession]:
return self._sessions[num] if 0 <= num < len(self._sessions) else None
def _load_config() -> EfxConfig:
json_site = (Path.home() / '.efx_site.json')
if not json_site.exists():
raise RuntimeError('No available configuration')
cfg = json.loads(json_site.read_text())
return EfxConfig(cfg)
def _attr_sessions(cfg) -> List[EfxSession]:
return [EfxSession(x) for x in cfg.get('sessions', [])]
def main():
parser = argparse.ArgumentParser(description='Site Information[Session]')
parser.add_argument('id', type=int, help='Session identifier')
args = parser.parse_args()
cfg = _load_config()
session = cfg.get_session(args.id)
if not session:
raise RuntimeError('Invalid session id')
session.execute()
if __name__ == "__main__":
main()