commit 5594617a75826f0cb2405e410df118e49add3f8d
parent 560a924c01399e240f311116d2afc4297c031605
Author: luke8086 <55237178+luke8086@users.noreply.github.com>
Date: Thu, 5 Sep 2024 06:51:21 +0000
Add support for rc files
Diffstat:
2 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
@@ -22,6 +22,24 @@ Press `?` to see available keybindings.
<img src="screenshot.png" width="600" />
+## Customization
+
+To customize retronews without directly editing the script, you can put any valid
+Python code in `~/.retronewsrc.py` (or other location specified with `--rcfile`) to
+be executed on startup. For example:
+
+```python
+# Ignore type warnings
+from typing import Any
+retronews: Any
+
+# Example: Custom key bindings
+retronews.KEY_BINDINGS[ord('a')] = lambda app: retronews.cmd_prev(app)
+retronews.KEY_BINDINGS[ord('z')] = lambda app: retronews.cmd_next(app)
+
+# Example: Custom colors
+retronews.COLORS['author'] = (retronews.curses.COLOR_RED, -1)
+```
## Known issues and limitations
@@ -29,8 +47,6 @@ Press `?` to see available keybindings.
- Message formatting is not perfect, but works well enough most of the time
- Detecting if threads contain unread responses works by only checking their
count, it's not reliable if any responses were deleted
-- No config file is planned, since the code is in Python, it's simpler to
- treat it as its own config and customize directly
## Why not an NNTP gateway?
diff --git a/retronews.py b/retronews.py
@@ -1501,12 +1501,23 @@ def setup_logging(path: Optional[str]) -> None:
logging.debug("Session started")
+def run_rcfile(path: str) -> None:
+ path = os.path.expanduser(path)
+
+ if not os.path.isfile(path):
+ return
+
+ code = compile(open(path).read(), path, "exec")
+ exec(code, {"retronews": sys.modules[__name__]})
+
+
if __name__ == "__main__":
tab_choices = range(1, len(GROUP_TABS) + 1)
ap = argparse.ArgumentParser(
formatter_class=lambda prog: argparse.ArgumentDefaultsHelpFormatter(prog, max_help_position=32)
)
+ ap.add_argument("-c", "--rcfile", metavar="PATH", default="~/.retronewsrc.py", help="optional startup code path")
ap.add_argument("-d", "--db", metavar="PATH", default="~/.retronews.db", help="database path")
ap.add_argument("-l", "--logfile", metavar="PATH", default=None, help="debug logfile path")
ap.add_argument("-t", "--tab", metavar="TAB", type=int, default=1, choices=tab_choices, help="initial tab")
@@ -1515,6 +1526,7 @@ if __name__ == "__main__":
args = ap.parse_args()
setup_logging(args.logfile)
+ run_rcfile(args.rcfile)
if (path := args.render) is not None:
with open(path) as fp: