mirror of
https://github.com/PR0M3TH3AN/VoxVera.git
synced 2025-09-08 06:58:42 +00:00
Improve Tkinter text editor
This commit is contained in:
@@ -6,7 +6,7 @@ Generate printable flyers with QR codes linking to Tor (.onion) or HTTPS sites,
|
|||||||
|
|
||||||
## 🚀 Key Features
|
## 🚀 Key Features
|
||||||
|
|
||||||
* **Interactive setup**: `voxvera init` prompts for metadata or extracts from a PDF form.
|
* **Interactive setup**: `voxvera init` prompts for metadata or extracts from a PDF form. When editing body text, a small Tkinter window opens with the current content pre-filled. If no GUI is available it falls back to `$EDITOR`.
|
||||||
* **Template support**: `voxvera init --template <name>` copies built‑in templates (`blank`, `voxvera`).
|
* **Template support**: `voxvera init --template <name>` copies built‑in templates (`blank`, `voxvera`).
|
||||||
* **Build assets**: `voxvera build [--pdf <path>] [--download <file.zip>]` generates HTML, obfuscated JS/CSS, QR codes, and bundles PDFs.
|
* **Build assets**: `voxvera build [--pdf <path>] [--download <file.zip>]` generates HTML, obfuscated JS/CSS, QR codes, and bundles PDFs.
|
||||||
* **Batch import**: `voxvera import` processes all JSON configs in `imports/`.
|
* **Batch import**: `voxvera import` processes all JSON configs in `imports/`.
|
||||||
|
@@ -84,20 +84,61 @@ def save_config(data: dict, path: str):
|
|||||||
json.dump(data, fh, indent=2)
|
json.dump(data, fh, indent=2)
|
||||||
|
|
||||||
|
|
||||||
def open_editor(initial: str) -> str:
|
def _open_editor_terminal(initial: str) -> str:
|
||||||
|
"""Fallback to opening the user's $EDITOR in the terminal."""
|
||||||
import tempfile
|
import tempfile
|
||||||
editor = os.environ.get('EDITOR', 'nano')
|
|
||||||
fd, path = tempfile.mkstemp(suffix='.txt')
|
editor = os.environ.get("EDITOR", "nano")
|
||||||
|
fd, path = tempfile.mkstemp(suffix=".txt")
|
||||||
try:
|
try:
|
||||||
with os.fdopen(fd, 'w', encoding='utf-8') as fh:
|
with os.fdopen(fd, "w", encoding="utf-8") as fh:
|
||||||
fh.write(initial or '')
|
fh.write(initial or "")
|
||||||
subprocess.call([editor, path])
|
subprocess.call([editor, path])
|
||||||
with open(path, 'r', encoding='utf-8') as fh:
|
with open(path, "r", encoding="utf-8") as fh:
|
||||||
return fh.read()
|
return fh.read()
|
||||||
finally:
|
finally:
|
||||||
os.unlink(path)
|
os.unlink(path)
|
||||||
|
|
||||||
|
|
||||||
|
def open_editor(initial: str) -> str:
|
||||||
|
"""Edit text in a small GUI window if possible.
|
||||||
|
|
||||||
|
Existing text is pre-filled in the editor. When ``tkinter`` or a display
|
||||||
|
server is unavailable the function falls back to ``$EDITOR`` in the
|
||||||
|
terminal.
|
||||||
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
import tkinter as tk
|
||||||
|
from tkinter import scrolledtext
|
||||||
|
except Exception:
|
||||||
|
return _open_editor_terminal(initial)
|
||||||
|
|
||||||
|
try:
|
||||||
|
root = tk.Tk()
|
||||||
|
root.title("Edit text")
|
||||||
|
except tk.TclError:
|
||||||
|
return _open_editor_terminal(initial)
|
||||||
|
|
||||||
|
result = {"text": initial or ""}
|
||||||
|
|
||||||
|
text = scrolledtext.ScrolledText(root, width=80, height=20)
|
||||||
|
text.pack(expand=True, fill="both")
|
||||||
|
if initial:
|
||||||
|
text.insert("1.0", initial)
|
||||||
|
text.focus_set()
|
||||||
|
|
||||||
|
def finalize():
|
||||||
|
result["text"] = text.get("1.0", "end-1c")
|
||||||
|
root.quit()
|
||||||
|
|
||||||
|
tk.Button(root, text="Save", command=finalize).pack()
|
||||||
|
root.protocol("WM_DELETE_WINDOW", finalize)
|
||||||
|
root.mainloop()
|
||||||
|
root.destroy()
|
||||||
|
return result["text"]
|
||||||
|
|
||||||
|
|
||||||
def _len_transform(limit: int):
|
def _len_transform(limit: int):
|
||||||
def _t(val: str) -> str:
|
def _t(val: str) -> str:
|
||||||
length = len(val)
|
length = len(val)
|
||||||
|
Reference in New Issue
Block a user