From 8659206ee67b43bbb2cde90438644327fac6eb60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Sun, 10 Oct 2021 20:16:19 +0200 Subject: [PATCH] add rofi opening script for exercises --- scripts/rofi-exercises.py | 49 ++++++++++++++++++++++++++++++++++++ scripts/window_subprocess.py | 4 +-- 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100755 scripts/rofi-exercises.py diff --git a/scripts/rofi-exercises.py b/scripts/rofi-exercises.py new file mode 100755 index 0000000..132d5c6 --- /dev/null +++ b/scripts/rofi-exercises.py @@ -0,0 +1,49 @@ +#!/usr/bin/python3 +from courses import Courses +from exercises import Exercises +from rofi import rofi +from config import MAX_LEN +import sys + + +def rofi_pick_exercise(spec: str = 'writeup'): + exercises = Courses().current.exercises + switcher = { + 'writeup': Exercises.writeups, + 'solution': Exercises.solutions, + 'sheet': Exercises.sheets + } + + sorted_ex = sorted(switcher[spec].fget(exercises), key=lambda e: -e.number) + + options = [ + "{number: >2}".format( + number=ex.number + ) + for ex in sorted_ex + ] + + switcher = { + 'writeup': 'writeup', + 'solution': 'solution', + 'sheet': 'sheet' + } + + key, index, selected = rofi('Select number of exercise {spec}'.format(spec=switcher[spec]), options, [ + '-lines', max(5, min(15, len(sorted_ex))), + '-markup-rows', + '-kb-custom-1', 'Alt+n' + ]) + + if key == 0: + sorted_ex[index].open() + elif key == 1: + pass # TODO: make new exercise + + +if __name__ == '__main__': + if not len(sys.argv) == 1: + print('Please specify exactly one of "writeup", "solution" and "sheet"') + exit(1) + rofi_pick_exercise('sheet') + exit(0) diff --git a/scripts/window_subprocess.py b/scripts/window_subprocess.py index a60e785..e34c494 100644 --- a/scripts/window_subprocess.py +++ b/scripts/window_subprocess.py @@ -5,13 +5,13 @@ from pathlib import Path import os -def edit(filepath: Path, rootpath: Path = None, env=os.environ): +def edit(filepath: Path, rootpath: Path = None, env=os.environ, servername='tex lecture'): if not rootpath: rootpath = filepath.root subprocess.Popen([ "termite", "-e", - f"vim --servername tex-vorlesung --remote-silent {str(filepath)}" + f"vim --servername {servername} --remote-silent {str(filepath)}" ], env=env, cwd=str(rootpath))