summaryrefslogtreecommitdiff
path: root/raveos-theme/hyprland/theme-data/DankMaterialShell/quickshell/Services/MuxService.qml
blob: c2f941cca1b2fc0d8d4524674609e884cad3ba19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
pragma Singleton
pragma ComponentBehavior: Bound

import QtQuick
import Quickshell
import Quickshell.Io
import qs.Common

Singleton {
    id: root

    property var sessions: []
    property bool loading: false

    property bool tmuxAvailable: false
    property bool zellijAvailable: false
    readonly property bool currentMuxAvailable: muxType === "zellij" ? zellijAvailable : tmuxAvailable

    readonly property string muxType: SettingsData.muxType
    readonly property string displayName: muxType === "zellij" ? "Zellij" : "Tmux"

    readonly property var terminalFlags: ({
        "ghostty": ["-e"],
        "kitty": ["-e"],
        "alacritty": ["-e"],
        "foot": [],
        "wezterm": ["start", "--"],
        "gnome-terminal": ["--"],
        "xterm": ["-e"],
        "konsole": ["-e"],
        "st": ["-e"],
        "terminator": ["-e"],
        "xfce4-terminal": ["-e"]
    })

    function getTerminalFlag(terminal) {
        return terminalFlags[terminal] ?? ["-e"]
    }

    readonly property string terminal: Quickshell.env("TERMINAL") || "ghostty"

    function _terminalPrefix() {
        return [terminal].concat(getTerminalFlag(terminal))
    }

    Process {
        id: tmuxCheckProcess
        command: ["which", "tmux"]
        running: false
        onExited: (code) => { root.tmuxAvailable = (code === 0) }
    }

    Process {
        id: zellijCheckProcess
        command: ["which", "zellij"]
        running: false
        onExited: (code) => { root.zellijAvailable = (code === 0) }
    }

    function checkAvailability() {
        tmuxCheckProcess.running = true
        zellijCheckProcess.running = true
    }

    Component.onCompleted: checkAvailability()

    Process {
        id: listProcess
        running: false

        stdout: StdioCollector {
            onStreamFinished: {
                try {
                    if (root.muxType === "zellij")
                        root._parseZellijSessions(text)
                    else
                        root._parseTmuxSessions(text)
                } catch (e) {
                    console.error("[MuxService] Error parsing sessions:", e)
                    root.sessions = []
                }
                root.loading = false
            }
        }

        stderr: SplitParser {
            onRead: (line) => {
                if (line.trim())
                    console.error("[MuxService] stderr:", line)
            }
        }

        onExited: (code) => {
            if (code !== 0 && code !== 1) {
                console.warn("[MuxService] Process exited with code:", code)
                root.sessions = []
            }
            root.loading = false
        }
    }

    function refreshSessions() {
        if (!root.currentMuxAvailable) {
            root.sessions = []
            return
        }

        root.loading = true

        if (listProcess.running)
            listProcess.running = false

        if (root.muxType === "zellij")
            listProcess.command = ["zellij", "list-sessions", "--no-formatting"]
        else
            listProcess.command = ["tmux", "list-sessions", "-F", "#{session_name}|#{session_windows}|#{session_attached}"]

        Qt.callLater(function () {
            listProcess.running = true
        })
    }

    function _isSessionExcluded(name) {
        var filter = SettingsData.muxSessionFilter.trim()
        if (filter.length === 0)
            return false
        var parts = filter.split(",")
        for (var i = 0; i < parts.length; i++) {
            var pattern = parts[i].trim()
            if (pattern.length === 0)
                continue
            if (pattern.startsWith("/") && pattern.endsWith("/") && pattern.length > 2) {
                try {
                    var re = new RegExp(pattern.slice(1, -1))
                    if (re.test(name))
                        return true
                } catch (e) {}
            } else {
                if (name.toLowerCase() === pattern.toLowerCase())
                    return true
            }
        }
        return false
    }

    function _parseTmuxSessions(output) {
        var sessionList = []
        var lines = output.trim().split('\n')

        for (var i = 0; i < lines.length; i++) {
            var line = lines[i].trim()
            if (line.length === 0)
                continue

            var parts = line.split('|')
            if (parts.length >= 3 && !_isSessionExcluded(parts[0])) {
                sessionList.push({
                    name: parts[0],
                    windows: parts[1],
                    attached: parts[2] === "1"
                })
            }
        }

        root.sessions = sessionList
    }

    function _parseZellijSessions(output) {
        var sessionList = []
        var lines = output.trim().split('\n')

        for (var i = 0; i < lines.length; i++) {
            var line = lines[i].trim()
            if (line.length === 0)
                continue

            var exited = line.includes("(EXITED")
            var bracketIdx = line.indexOf(" [")
            var name = (bracketIdx > 0 ? line.substring(0, bracketIdx) : line).trim()

            if (!_isSessionExcluded(name)) {
                sessionList.push({
                    name: name,
                    windows: "N/A",
                    attached: !exited
                })
            }
        }

        root.sessions = sessionList
    }

    function attachToSession(name) {
        if (SettingsData.muxUseCustomCommand && SettingsData.muxCustomCommand) {
            Quickshell.execDetached([SettingsData.muxCustomCommand, name])
        } else if (root.muxType === "zellij") {
            Quickshell.execDetached(_terminalPrefix().concat(["zellij", "attach", name]))
        } else {
            Quickshell.execDetached(_terminalPrefix().concat(["tmux", "attach", "-t", name]))
        }
    }

    function createSession(name) {
        if (SettingsData.muxUseCustomCommand && SettingsData.muxCustomCommand) {
            Quickshell.execDetached([SettingsData.muxCustomCommand, name])
        } else if (root.muxType === "zellij") {
            Quickshell.execDetached(_terminalPrefix().concat(["zellij", "-s", name]))
        } else {
            Quickshell.execDetached(_terminalPrefix().concat(["tmux", "new-session", "-s", name]))
        }
    }

    readonly property bool supportsRename: muxType !== "zellij"

    function renameSession(oldName, newName) {
        if (root.muxType === "zellij")
            return
        Quickshell.execDetached(["tmux", "rename-session", "-t", oldName, newName])
        Qt.callLater(refreshSessions)
    }

    function killSession(name) {
        if (root.muxType === "zellij") {
            Quickshell.execDetached(["zellij", "kill-session", name])
        } else {
            Quickshell.execDetached(["tmux", "kill-session", "-t", name])
        }
        Qt.callLater(refreshSessions)
    }
}