aboutsummaryrefslogtreecommitdiff
path: root/linux/home/.config/ags/widget/launcher/ShRun.ts
blob: c4215efdd1e502b80886e03bab3ebd74f41638d9 (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
import icons from "lib/icons"
import options from "options"
import { bash, dependencies } from "lib/utils"

const iconVisible = Variable(false)

const MAX = options.launcher.sh.max
const BINS = `${Utils.CACHE_DIR}/binaries`
bash("{ IFS=:; ls -H $PATH; } | sort ")
    .then(bins => Utils.writeFile(bins, BINS))

async function query(filter: string) {
    if (!dependencies("fzf"))
        return [] as string[]

    return bash(`cat ${BINS} | fzf -f ${filter} | head -n ${MAX}`)
        .then(str => Array.from(new Set(str.split("\n").filter(i => i)).values()))
        .catch(err => { print(err); return [] })
}

function run(args: string) {
    Utils.execAsync(args)
        .then(out => {
            print(`:sh ${args.trim()}:`)
            print(out)
        })
        .catch(err => {
            Utils.notify("ShRun Error", err, icons.app.terminal)
        })
}

function Item(bin: string) {
    return Widget.Box(
        {
            attribute: { bin },
            vertical: true,
        },
        Widget.Separator(),
        Widget.Button({
            child: Widget.Label({
                label: bin,
                hpack: "start",
            }),
            class_name: "sh-item",
            on_clicked: () => {
                Utils.execAsync(bin)
                App.closeWindow("launcher")
            },
        }),
    )
}

export function Icon() {
    const icon = Widget.Icon({
        icon: icons.app.terminal,
        class_name: "spinner",
    })

    return Widget.Revealer({
        transition: "slide_left",
        child: icon,
        reveal_child: iconVisible.bind(),
    })
}

export function ShRun() {
    const list = Widget.Box<ReturnType<typeof Item>>({
        vertical: true,
    })

    const revealer = Widget.Revealer({
        child: list,
    })

    async function filter(term: string) {
        iconVisible.value = Boolean(term)

        if (!term)
            revealer.reveal_child = false

        if (term.trim()) {
            const found = await query(term)
            list.children = found.map(Item)
            revealer.reveal_child = true
        }
    }

    return Object.assign(revealer, { filter, run })
}