fix: append command history row in real-time after execution

This commit is contained in:
2026-06-14 09:12:28 -04:00
parent 49d724cf9c
commit b7bd4bfe92
2 changed files with 34 additions and 1 deletions

View File

@@ -12,6 +12,12 @@ terminalInput.addEventListener('keydown', function(e) {
}
});
function escapeHtml(str) {
const d = document.createElement('div');
d.textContent = str;
return d.innerHTML;
}
function executeCommand() {
const command = terminalInput.value.trim();
if (!command) return;
@@ -46,6 +52,9 @@ function executeCommand() {
if (data.stderr) {
appendTerminalLine(data.stderr, 'error');
}
if (data.history) {
appendHistoryRow(data.history);
}
updateStatus('connected', 'Connected');
} else {
appendTerminalLine(data.message || 'Command failed', 'error');
@@ -84,6 +93,23 @@ function clearTerminal() {
terminalOutput.innerHTML = '';
}
function appendHistoryRow(h) {
const tbody = document.querySelector('.card-body .table tbody');
if (!tbody) return;
const emptyState = tbody.closest('.card-body')?.querySelector('.empty-state');
if (emptyState) emptyState.remove();
const row = document.createElement('tr');
const time = h.executed_at ? new Date(h.executed_at.replace(' ', 'T')).toLocaleTimeString('en-US', { hour12: false }) : '';
row.innerHTML =
'<td>' + time + '</td>' +
'<td><code>' + escapeHtml(h.command) + '</code></td>' +
'<td><span class="badge ' + (h.exit_status === 0 ? 'badge-success' : 'badge-danger') + '">' + (h.exit_status === 0 ? 'OK' : 'ERR') + '</span></td>' +
'<td>' + escapeHtml(h.username || 'System') + '</td>';
tbody.prepend(row);
const container = tbody.closest('.table-responsive');
if (container) container.scrollTop = 0;
}
function clearHistory() {
if (!confirm('Clear all command history for this server?')) return;

View File

@@ -100,7 +100,7 @@ class TerminalController
$ssh->disconnect();
$commandHistory = new CommandHistory();
$commandHistory->log(
$historyId = $commandHistory->log(
$id,
Session::get('user_id'),
$command,
@@ -115,6 +115,13 @@ class TerminalController
'output' => $result['output'],
'exit_status' => $result['exit_status'],
'stderr' => $result['stderr'],
'history' => [
'id' => $historyId,
'command' => $command,
'exit_status' => $result['exit_status'] ?? 0,
'executed_at' => date('Y-m-d H:i:s'),
'username' => Session::get('username'),
],
]);
} catch (\Throwable $e) {
$this->view->json([