aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/git/GitPage.qml
blob: 6edf085562116b009e6f5db223cf440fcbef10d6 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

import Quickshell
import Quickshell.Io
import QtQuick
import "../.."
import "Git.js" as G

// Every global git value, one row each, grouped by section in file order.
// Enter on a row saves it, Escape restores it, Remove drops that one value.
// Every write goes through gitcfg and is followed by a fresh list, so a row
// always shows what git holds. A failed write shows git's reason under the
// row it came from.
Column {
    id: page

    spacing: 6

    readonly property string ctl: Qt.resolvedUrl("gitcfg").toString().replace(/^file:\/\//, "")
    property var entries: []
    // Keyed by tag(): name and value, which survive the reload after a failed
    // write because the value did not change.
    property var errors: ({})
    property bool busy: false
    property string loadError: ""
    // The module, which outlives the page: a write in flight finishes even if
    // the page is closed. Not named `gm`, the module's id (it would bind to itself).
    required property QtObject owner

    // The section whose add row is open, and what is typed in it. The text
    // lives here rather than in the fields because every reload rebuilds the
    // rows, and a failed add should keep what was typed.
    property string adding: ""
    property string newKey: ""
    property string newValue: ""
    onAddingChanged: { newKey = ""; newValue = ""; errors = ({}); }

    function add() {
        if (page.newKey.trim() === "") return;
        page.write(["add", page.adding + "." + page.newKey.trim(), page.newValue], "add",
                   () => page.adding = "");
    }

    function tag(e) { return e.name + "=" + e.value; }

    // One Process per call, so reads and writes never share one (a running
    // Process ignores a second start, AGENTS.md). cb gets stdout.
    function run(cmd, cb) {
        op.createObject(page.owner, { command: cmd, cb: cb }).running = true;
    }
    Component {
        id: op
        Process {
            id: proc
            property var cb: null
            stdout: StdioCollector {
                onStreamFinished: {
                    try { proc.cb(text); }
                    finally { proc.destroy(); }
                }
            }
        }
    }

    function load() {
        page.run([page.ctl, "list"], out => {
            // gitcfg reports a file git cannot parse as one error= line;
            // without it the page would draw an empty config.
            page.loadError = out.startsWith("error=") ? out.slice(6).trim() : "";
            page.entries = page.loadError ? [] : G.parse(out);
            page.busy = false;
        });
    }

    // errKey: where a failure is shown. ok: run on success only.
    function write(args, errKey, ok) {
        if (page.busy) return;
        page.busy = true;
        page.run([page.ctl].concat(args), out => {
            const e = {};
            if (out.trim() !== "") e[errKey] = out.trim();
            else if (ok) ok();
            page.errors = e;
            page.load();
        });
    }

    Component.onCompleted: page.load()

    // Detached, so the editor outlives the drawer. ponytail: ~/.gitconfig is
    // the one global file here; ~/.config/git/config would be missed.
    Button {
        text: "Edit in editor"
        onClicked: Quickshell.execDetached(["sublime_text", Quickshell.env("HOME") + "/.gitconfig"])
    }

    Text {
        width: parent.width
        visible: page.loadError !== ""
        text: page.loadError
        wrapMode: Text.Wrap
        color: Theme.red
        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 }
    }

    // The add row under a section's heading: key and value. Enter in the key
    // moves to the value, Enter there adds, Escape closes the row.
    Component {
        id: addRow
        Column {
            spacing: 4

            Item {
                width: parent.width
                height: 34

                Rectangle {
                    id: keyBox
                    anchors.left: parent.left
                    width: parent.width * 0.32
                    height: parent.height
                    radius: 8
                    color: Theme.surface
                    TextInput {
                        id: keyIn
                        anchors { fill: parent; leftMargin: 10; rightMargin: 10 }
                        verticalAlignment: TextInput.AlignVCenter
                        clip: true
                        selectByMouse: true
                        color: Theme.text
                        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
                        text: page.newKey
                        onTextEdited: page.newKey = text
                        onAccepted: valIn.forceActiveFocus()
                        Keys.onEscapePressed: page.adding = ""
                        Component.onCompleted: forceActiveFocus()
                        Text {
                            anchors.verticalCenter: parent.verticalCenter
                            visible: parent.text === ""
                            text: "key"
                            color: Theme.subtext
                            font: parent.font
                        }
                    }
                }

                Rectangle {
                    anchors { left: keyBox.right; leftMargin: 8; right: addBtn.left; rightMargin: 8 }
                    height: parent.height
                    radius: 8
                    color: Theme.surface
                    TextInput {
                        id: valIn
                        anchors { fill: parent; leftMargin: 10; rightMargin: 10 }
                        verticalAlignment: TextInput.AlignVCenter
                        clip: true
                        selectByMouse: true
                        color: Theme.text
                        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
                        text: page.newValue
                        onTextEdited: page.newValue = text
                        onAccepted: page.add()
                        Keys.onEscapePressed: page.adding = ""
                        Text {
                            anchors.verticalCenter: parent.verticalCenter
                            visible: parent.text === ""
                            text: "value"
                            color: Theme.subtext
                            font: parent.font
                        }
                    }
                }

                Button {
                    id: addBtn
                    anchors { right: parent.right; verticalCenter: parent.verticalCenter }
                    text: "Add"
                    enabled: page.newKey.trim() !== ""
                    onClicked: page.add()
                }
            }

            Text {
                width: parent.width
                visible: !!page.errors.add
                text: page.errors.add ?? ""
                wrapMode: Text.Wrap
                color: Theme.red
                font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 }
            }
        }
    }

    Repeater {
        model: page.entries

        delegate: Column {
            id: row
            required property var modelData
            required property int index
            readonly property string tag: page.tag(modelData)
            width: page.width
            spacing: 4
            enabled: !page.busy

            // A section's heading, above its first row, with + to add a key
            // to it. git config --add writes into the existing section, so
            // the new key lands with its neighbours in the file.
            Item {
                id: heading
                visible: row.index === 0 || page.entries[row.index - 1].section !== row.modelData.section
                width: parent.width
                height: 40

                Text {
                    anchors { left: parent.left; bottom: parent.bottom; bottomMargin: 6 }
                    text: row.modelData.section
                    font { family: Theme.fontFamily; pixelSize: Theme.fontSize; bold: true }
                    color: Theme.text
                }
                Button {
                    anchors { right: parent.right; bottom: parent.bottom; bottomMargin: 2 }
                    text: "+"
                    onClicked: page.adding = page.adding === row.modelData.section ? "" : row.modelData.section
                }
            }

            Loader {
                width: parent.width
                active: heading.visible && page.adding === row.modelData.section
                visible: active
                sourceComponent: addRow
            }

            // An Item with anchors, not a Row: a Row sizes to its children
            // and fixed widths in one overflow the page (AGENTS.md).
            Item {
                width: parent.width
                height: 34

                Text {
                    id: keyLabel
                    anchors { left: parent.left; verticalCenter: parent.verticalCenter }
                    width: parent.width * 0.32
                    elide: Text.ElideRight
                    text: row.modelData.key
                    font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 3 }
                    color: Theme.subtext
                }

                Rectangle {
                    anchors { left: keyLabel.right; leftMargin: 8; right: del.left; rightMargin: 8 }
                    height: parent.height
                    radius: 8
                    color: Theme.surface

                    // ponytail: one line; a value holding a newline (a long
                    // alias) shows joined and is best edited in the editor.
                    TextInput {
                        id: input
                        anchors { fill: parent; leftMargin: 10; rightMargin: 10 }
                        verticalAlignment: TextInput.AlignVCenter
                        clip: true
                        selectByMouse: true
                        color: Theme.text
                        font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 }
                        text: row.modelData.value
                        onAccepted: if (text !== row.modelData.value)
                            page.write(["set", row.modelData.name, row.modelData.value, text], row.tag)
                        // An edit in progress is undone first; an unedited
                        // field lets Escape through, to leave the page.
                        Keys.onEscapePressed: event => {
                            if (text !== row.modelData.value) text = row.modelData.value;
                            else event.accepted = false;
                        }
                    }
                }

                Button {
                    id: del
                    anchors { right: parent.right; verticalCenter: parent.verticalCenter }
                    text: "Remove"
                    danger: true
                    onClicked: page.write(["unset", row.modelData.name, row.modelData.value], row.tag)
                }
            }

            Text {
                width: parent.width
                visible: !!page.errors[row.tag]
                text: page.errors[row.tag] ?? ""
                wrapMode: Text.Wrap
                color: Theme.red
                font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 }
            }
        }
    }
}