Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions sqlit/domains/query/ui/mixins/query_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,28 @@ def _apply_history_query(self: QueryMixinHost, query: str) -> None:
# Focus query input - this triggers on_descendant_focus which updates footer bindings
self.query_input.focus()

def _append_history_query(self: QueryMixinHost, query: str) -> None:
"""Append a query to the end of the editor without replacing text."""
if query is None:
return

current_text = self.query_input.text
if current_text:
trimmed_current = current_text.rstrip("\n")
separator = "\n" if trimmed_current else ""
new_text = f"{trimmed_current}{separator}{query}"
else:
new_text = query

self._push_undo_state()
self.query_input.text = new_text

lines = new_text.split("\n")
last_line = len(lines) - 1
last_col = len(lines[-1]) if lines else 0
self.query_input.cursor_location = (last_line, last_col)
self.query_input.focus()

def action_show_history(self: QueryMixinHost) -> None:
"""Show query history for the current connection."""
if not self.current_config:
Expand Down Expand Up @@ -781,6 +803,8 @@ def _handle_history_result(self: QueryMixinHost, result: Any) -> None:
action, data = result
if action == "select":
self._apply_history_query(data)
elif action == "append":
self._append_history_query(data)
elif action == "delete":
self._delete_history_entry(data)
self.action_show_history()
Expand Down Expand Up @@ -853,6 +877,10 @@ def _handle_telescope_result(self: QueryMixinHost, result: Any) -> None:
connection_name = data.get("connection_name", "")
database = data.get("database", "")
self._run_telescope_query(connection_name, query, database=database)
elif action == "append":
query = data.get("query", "")
if query:
self._append_history_query(query)
elif action == "delete":
timestamp = data.get("timestamp", "")
connection_name = data.get("connection_name", "")
Expand Down
24 changes: 23 additions & 1 deletion sqlit/domains/query/ui/screens/query_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class QueryHistoryScreen(ModalScreen):
Binding("escape", "cancel", "Cancel", priority=True),
Binding("q", "cancel", "Cancel"),
Binding("enter", "select", "Select"),
Binding("a", "append", "Append"),
Binding("d", "delete", "Delete"),
Binding("asterisk", "toggle_star", "Star"),
Binding("slash", "open_filter", "Filter"),
Expand Down Expand Up @@ -177,7 +178,12 @@ def compose(self) -> ComposeResult:
else:
title = f"Query History - {self.connection_name}"
empty_message = "No query history for this connection"
shortcuts = [("Select", "<enter>"), ("Star", "*"), ("Delete", "D")]
shortcuts = [
("Select", "<enter>"),
("Append", "a"),
("Star", "*"),
("Delete", "D"),
]

self._merged_entries = self._merge_entries()

Expand Down Expand Up @@ -240,6 +246,22 @@ def action_select(self) -> None:
except Exception:
self.dismiss(None)

def action_append(self) -> None:
entries = self._get_display_entries()
if not entries:
self.dismiss(None)
return

try:
option_list = self.query_one("#history-list", OptionList)
idx = option_list.highlighted
if idx is not None and idx < len(entries):
self.dismiss(self._build_action_result("append", entries[idx]))
else:
self.dismiss(None)
except Exception:
self.dismiss(None)

def on_option_list_option_selected(self, event: OptionList.OptionSelected) -> None:
if event.option_list.id == "history-list":
idx = event.option_list.highlighted
Expand Down