#!/usr/bin/env python3 """An app to encrypt and decrypt text using ROT13.""" from textual.app import App, ComposeResult from textual.binding import Binding from textual.containers import Horizontal from textual.widgets import Button, Footer, Header, TextArea def transcript(text: str) -> str: """Applies ROT13 transformation to the input string.""" return text.translate( str.maketrans( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm", ) ) class Rot13App(App): """A Textual app for ROT13 encryption and decryption.""" TITLE = "ROT13" BINDINGS = [ Binding("ctrl+e", "encrypt", "Encrypt", show=True), Binding("ctrl+d", "decrypt", "Decrypt", show=True), Binding("ctrl+x", "quit", "Exit", show=True), ] CSS = """ Screen { layout: vertical; padding: 1 2; } TextArea { height: 1fr; margin-bottom: 1; } Horizontal { height: auto; align: center middle; } Button { margin: 0 1; } """ def compose(self) -> ComposeResult: yield Header() yield TextArea(placeholder="Enter text here...") with Horizontal(): yield Button("Encrypt", id="encrypt_btn", variant="primary") yield Button("Decrypt", id="decrypt_btn", variant="success") yield Button("Exit", id="exit_btn", variant="error") yield Footer() def process_text(self) -> None: """Helper to transform and replace text inside the TextArea.""" text_area = self.query_one(TextArea) text_area.text = transcript(text_area.text) # Keyboard binding actions def action_encrypt(self) -> None: self.process_text() def action_decrypt(self) -> None: self.process_text() # Button click handlers def on_button_pressed(self, event: Button.Pressed) -> None: if event.button.id in ("encrypt_btn", "decrypt_btn"): self.process_text() elif event.button.id == "exit_btn": self.exit() if __name__ == "__main__": app = Rot13App() app.run()