-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed
Description
If you try to make a binding that is ctrl + key using the on_key function, it won't work. The only way to bind is to use the arrow keys which is an anti-pattern for the terminal
from textual.reactive import reactive
from apptypes.Command import Command
from lib.scriptloader import ScriptLoader
from textual.app import ComposeResult
from textual.containers import HorizontalGroup
from textual.widgets import Input, Static
from widgets.ReplContent import ReplContent
from datetime import datetime
from peewee import SqliteDatabase
from lib.models import History
from os import getenv
class ReplEntry(ScriptLoader, HorizontalGroup):
BINDINGS = [
("ctrl+j", "move_down", "Move history down"),
("ctrl+k", "move_up", "Move history up"),
("tab", "focus_repl", "Focus REPL scrolling"),
]
history:reactive[list[str]] = reactive([], recompose=True)
history_index:reactive[int] = reactive(0, recompose=True)
def load_input_history(self) -> None:
with open(".history", "r") as f:
self.history = f.read().splitlines()
def action_focus_repl(self) -> None:
repl_content:ReplContent = self.app.query_one(ReplContent)
repl_content.focusme()
def action_move_up(self) -> None:
if len(self.history) - 1 != self.history_index:
field = self.query_one(".field", Input)
self.history_index = self.history_index + 1
field.value = self.history[self.history_index]
def action_move_down(self) -> None:
if self.history_index != 0:
field = self.query_one(".field", Input)
self.history_index = self.history_index - 1
field.value = self.history[self.history_index]
def on_key(self, event) -> None:
if event.key == "j" and event.ctrl:
self.action_move_down()
elif event.key == "k" and event.ctrl:
self.action_move_up()
def _on_input_submitted(self, event:Input.Submitted) -> None:
text:str = event.value.strip()
# if there is text
if text:
repl:ReplContent = self.app.query_one(ReplContent)
result:list[Command]|None = self.scriptloader(self.app, text)
home_path:str|None = getenv("HOME")
if home_path == None: exit(1)
db:SqliteDatabase = SqliteDatabase(home_path + "/.adsys/history.db")
db.bind([History])
db.connect()
time:str = datetime.now().strftime("%m/%d/%y - %l:%M:%S%p")
if result != None:
if result[0]["result"] == "success":
data:Command = { "type": "prompt", "content": text, "timestamp": time, "result": "success" }
repl.commands = repl.commands + [data]
History.create(type=data['type'], result=data['result'], content=data['content'], timestamp=data['timestamp'])
elif result[0]["result"] == "fail":
data:Command = { "type": "prompt", "content": text, "timestamp": time, "result": "fail" }
repl.commands = repl.commands + [data]
History.create(type=data['type'], result=data['result'], content=data['content'], timestamp=data['timestamp'])
for r in result:
repl.commands = repl.commands + [r]
History.create(type=r['type'], result=r['result'], content=r['content'], timestamp=r['timestamp'])
else:
data:Command = { "result": "fail", "content": "Failed to load", "timestamp": time, "type": "prompt" }
History.create(type=data['type'], result=data['result'], content=data['content'], timestamp=data['timestamp'])
event.input.value = ""
def on_mount(self) -> None:
self.input_widget.focus()
def compose(self) -> ComposeResult:
yield Static("i?: ", classes="plainstatic")
self.input_widget = Input(placeholder="Enter Command...", classes="field")
yield self.input_widgetReactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels