aboutsummaryrefslogtreecommitdiff
path: root/utils.py
diff options
context:
space:
mode:
authordavid <[email protected]>2026-09-08 00:36:22 -0300
committerdavid <[email protected]>2026-09-08 00:36:22 -0300
commit33b2688a2d2888bf5e560c70a5bd92a319f5288d (patch)
treeb5822c6dc5a0d7b3223e7efce3d5d8d36368a859 /utils.py
parent71616cfc9b28df1a153f3746fdae1bf8aee23ef3 (diff)
downloadfaggot-chan-33b2688a2d2888bf5e560c70a5bd92a319f5288d.tar.gz
faggot-chan-33b2688a2d2888bf5e560c70a5bd92a319f5288d.zip
all of this was vibecoded. i just asked claude to fix all my bugs and he actually overhauled shit woaw. he only changed the name and position of a couple functions so if i want to write more things it should be fine. i love claude i love antrhopic i love vibecoding @claude make me a million dollars in a day make no mistake
Diffstat (limited to 'utils.py')
-rw-r--r--utils.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/utils.py b/utils.py
new file mode 100644
index 0000000..aea653b
--- /dev/null
+++ b/utils.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python3
+from pathlib import Path
+from datetime import datetime
+
+
+def now_str() -> str:
+ return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+
+
+def nots(line: str) -> str:
+ if line.startswith("[") and "]" in line:
+ return line.split("]", 1)[1].lstrip()
+ return line
+
+
+def tail_lines(path: Path, n: int) -> list[str]:
+ # grabs the last n lines without reading the whole file into memory first,
+ # since memory.txt only ever grows and reading it fully every message doesn't scale
+ if n <= 0:
+ return []
+ avg_line_bytes = 200
+ with path.open("rb") as f:
+ f.seek(0, 2)
+ file_size = f.tell()
+ if file_size == 0:
+ return []
+ block_size = n * avg_line_bytes
+ data = b""
+ lines = []
+ while True:
+ seek_pos = max(file_size - block_size, 0)
+ f.seek(seek_pos)
+ data = f.read(file_size - seek_pos)
+ lines = data.splitlines()
+ if len(lines) > n or seek_pos == 0:
+ break
+ block_size *= 2
+ return [l.decode("utf-8", errors="ignore").strip() for l in lines[-n:] if l.strip()]