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
103 changes: 103 additions & 0 deletions hermes-ssh-chat/BarWidget.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import QtQuick
import QtQuick.Layouts
import Quickshell
import qs.Commons
import qs.Modules.Bar.Extras
import qs.Services.UI
import qs.Widgets
import "." as Local

Item {
id: root

property ShellScreen screen
property var pluginApi: null
property string widgetId: ""
property string section: ""
property int sectionWidgetIndex: -1
property int sectionWidgetsCount: 0

readonly property string screenName: screen ? screen.name : ""
readonly property string barPosition: Settings.getBarPositionForScreen(screenName)
readonly property bool isVertical: barPosition === "left" || barPosition === "right"
readonly property real capsuleHeight: Style.getCapsuleHeightForScreen(screenName)
readonly property real barFontSize: Style.getBarFontSizeForScreen(screenName)
readonly property bool showBarText: Local.HermesSession.setting("showBarText", true)
readonly property color stateColor: Local.HermesSession.connected ? Color.mPrimary : (Local.HermesSession.connecting ? Color.mSecondary : Color.mOnSurfaceVariant)

implicitWidth: isVertical ? capsuleHeight : Math.round(contentRow.implicitWidth + Style.margin2M)
implicitHeight: isVertical ? Math.round(contentRow.implicitHeight + Style.margin2M) : capsuleHeight

Component.onCompleted: Local.HermesSession.setPluginApi(pluginApi)

NPopupContextMenu {
id: contextMenu

model: [
{
"label": pluginApi?.tr("menu.settings"),
"action": "settings",
"icon": "settings"
},
]

onTriggered: action => {
contextMenu.close();
PanelService.closeContextMenu(screen);
if (action === "settings" && pluginApi?.manifest)
BarService.openPluginSettings(screen, pluginApi.manifest);
}
}

Rectangle {
anchors.fill: parent
radius: Style.radiusM
color: Style.capsuleColor
border.color: Style.capsuleBorderColor
border.width: Style.capsuleBorderWidth

RowLayout {
id: contentRow
anchors.centerIn: parent
spacing: Style.marginXS

Image {
property real iconSize: Style.toOdd(root.capsuleHeight * 0.42)

Layout.preferredWidth: iconSize
Layout.preferredHeight: iconSize
source: Qt.resolvedUrl("assets/hermesagent.svg")
sourceSize.width: iconSize
sourceSize.height: iconSize
fillMode: Image.PreserveAspectFit
smooth: true
mipmap: true
opacity: Local.HermesSession.sessionActive ? 1.0 : 0.72
}

NText {
visible: root.showBarText && !root.isVertical
text: Local.HermesSession.connecting ? pluginApi?.tr("widget.connecting") : pluginApi?.tr("widget.label")
pointSize: root.barFontSize
applyUiScale: false
color: root.stateColor
}
}

MouseArea {
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton
cursorShape: Qt.PointingHandCursor
hoverEnabled: true
onClicked: mouse => {
if (mouse.button === Qt.LeftButton) {
pluginApi?.togglePanel(screen, root);
} else if (mouse.button === Qt.RightButton) {
PanelService.showContextMenu(contextMenu, root, screen);
}
}
onEntered: TooltipService.show(root, Local.HermesSession.statusText)
onExited: TooltipService.hide()
}
}
}
240 changes: 240 additions & 0 deletions hermes-ssh-chat/HermesSession.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
pragma Singleton

import QtQuick
import Quickshell
import Quickshell.Io

Singleton {
id: root

property var pluginApi: null
property string host: ""
property int port: 22
property string user: ""
property string password: ""
property string status: "idle"
property string statusText: ""
property string lastError: ""
property string terminalBuffer: ""

readonly property bool connecting: status === "connecting"
readonly property bool connected: status === "connected"
readonly property bool sessionActive: connecting || connected
readonly property string targetLabel: user && host ? user + "@" + host : "Hermes"

property var _pendingConnect: null
property var _pendingResize: null
property bool _autoConnectAttempted: false

signal terminalOutput(string text)
signal terminalReset()

function tr(key, args) {
return root.pluginApi ? root.pluginApi.tr(key, args || ({})) : key;
}

function resetTerminal() {
root.terminalBuffer = "";
root.terminalReset();
}

function setPluginApi(api) {
if (!api) return;
root.pluginApi = api;
root.host = api.pluginSettings.host || "";
root.port = api.pluginSettings.port || 22;
root.user = api.pluginSettings.user || "";
if (root.status === "idle" && root.statusText.length === 0)
root.statusText = tr("status.disconnected");
root.maybeAutoConnect();
}

function setting(key, fallback) {
if (!root.pluginApi || !root.pluginApi.pluginSettings) return fallback;
var value = root.pluginApi.pluginSettings[key];
return value === undefined ? fallback : value;
}

function helperPath() {
var url = String(Qt.resolvedUrl("helpers/hermes_ssh_bridge.py"));
return decodeURIComponent(url.replace(/^file:\/\//, ""));
}

function maybeAutoConnect() {
if (root._autoConnectAttempted || !setting("autoConnectOnStartup", false))
return;

root._autoConnectAttempted = true;

if (!root.host || !root.user || root.sessionActive)
return;

Qt.callLater(function() {
if (!root.sessionActive)
root.connect(root.host, root.port, root.user, "", setting("terminalRows", 32), setting("terminalCols", 180));
});
}

function connect(targetHost, targetPort, targetUser, targetPassword, rows, cols) {
root.host = String(targetHost || "").trim();
root.port = Math.max(1, Math.min(65535, Number(targetPort || 22)));
root.user = String(targetUser || "").trim();
root.password = String(targetPassword || "");
root.lastError = "";

if (!root.host || !root.user) {
root.status = "idle";
root.statusText = tr("status.missingTarget");
root.lastError = root.statusText;
return;
}

if (root.pluginApi && setting("rememberLastTarget", true)) {
root.pluginApi.pluginSettings.host = root.host;
root.pluginApi.pluginSettings.port = root.port;
root.pluginApi.pluginSettings.user = root.user;
root.pluginApi.saveSettings();
}

root.resetTerminal();
root.status = "connecting";
root.statusText = tr("status.connecting", { "target": root.targetLabel });

root._pendingConnect = JSON.stringify({
"type": "connect",
"host": root.host,
"port": root.port,
"user": root.user,
"password": root.password,
"rows": Math.max(1, Number(rows || setting("terminalRows", 32))),
"cols": Math.max(1, Number(cols || setting("terminalCols", 180)))
}) + "\n";

bridge.command = ["python3", root.helperPath()];
if (bridge.running) {
bridge.running = false;
return;
}
bridge.running = true;
}

function send(text) {
if (!bridge.running || !root.sessionActive) return;
bridge.write(JSON.stringify({"type": "input", "text": String(text || "")}) + "\n");
}

function resize(rows, cols) {
var payload = JSON.stringify({
"type": "resize",
"rows": Math.max(1, Number(rows || setting("terminalRows", 32))),
"cols": Math.max(1, Number(cols || setting("terminalCols", 180)))
}) + "\n";

if (bridge.running && root.sessionActive) {
bridge.write(payload);
} else {
root._pendingResize = payload;
}
}

function disconnect() {
root._pendingConnect = null;
root._pendingResize = null;
if (bridge.running) {
bridge.write(JSON.stringify({"type": "disconnect"}) + "\n");
}
root.status = "idle";
root.statusText = tr("status.disconnected");
root.lastError = "";
root.password = "";
root.resetTerminal();
}

function handleMessage(line) {
var trimmed = String(line || "").trim();
if (!trimmed) return;

var msg = null;
try {
msg = JSON.parse(trimmed);
} catch (e) {
return;
}

if (msg.type === "output") {
var text = String(msg.text || "");
root.terminalBuffer += text;
root.terminalOutput(text);
return;
}

if (msg.type === "status") {
if (msg.status === "idle" && root.status === "connecting")
return;
root.status = String(msg.status || root.status);
root.statusText = String(msg.message || root.statusText);
return;
}

if (msg.type === "error") {
root.lastError = String(msg.message || tr("status.error"));
root.statusText = root.lastError;
return;
}

if (msg.type === "exit") {
root.status = "idle";
root.statusText = tr("status.disconnected");
root.password = "";
root.resetTerminal();
}
}

function flushPending() {
if (root._pendingConnect) {
bridge.write(root._pendingConnect);
root._pendingConnect = null;
}
if (root._pendingResize) {
bridge.write(root._pendingResize);
root._pendingResize = null;
}
}

Process {
id: bridge
running: false
stdinEnabled: true

stdout: SplitParser {
splitMarker: "\n"
onRead: function(data) { root.handleMessage(data); }
}

stderr: SplitParser {
splitMarker: "\n"
onRead: function(data) {
var text = String(data || "").trim();
if (text.length > 0) {
root.lastError = text;
root.statusText = text;
}
}
}

onStarted: root.flushPending()

onExited: function(exitCode, exitStatus) {
if (root._pendingConnect) {
bridge.running = true;
return;
}
if (root.sessionActive) {
root.status = "idle";
root.statusText = tr("status.bridgeExited");
root.password = "";
root.resetTerminal();
}
}
}
}
21 changes: 21 additions & 0 deletions hermes-ssh-chat/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 focky

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading