馃敡 Where my dotfiles lives in harmony and peace, most of the time
1#!/usr/bin/env bash
2set -euo pipefail
3
4APP_NAME="magic-brush"
5NOTIFY_TITLE="馃獎 Magic Brush"
6NOTIFY_ICON="draw-brush"
7
8require() {
9 command -v "$1" >/dev/null 2>&1
10}
11
12notify() {
13 local urgency="$1"
14 local timeout_ms="$2"
15 local message="$3"
16
17 if require notify-send; then
18 notify-send -a "$APP_NAME" -i "$NOTIFY_ICON" -u "$urgency" -t "$timeout_ms" "$NOTIFY_TITLE" "$message"
19 fi
20}
21
22die() {
23 local message="$1"
24 notify critical 0 "$message"
25 echo "$APP_NAME: $message" >&2
26 exit 1
27}
28
29if [[ $# -ne 0 ]]; then
30 die "This script takes no arguments"
31fi
32
33for cmd in alacritty llm wl-copy wl-paste; do
34 if ! require "$cmd"; then
35 die "Missing required command: $cmd"
36 fi
37done
38
39INPUT="$(wl-paste --type text --no-newline)" || die "Failed to read clipboard text"
40if [[ -z "${INPUT}" ]]; then
41 die "Clipboard is empty"
42fi
43
44notify low 5000 "Polishing..."
45
46OUTPUT="$(
47 printf %s "$INPUT" | llm prompt -t polish --no-log --no-stream
48)" || die "LLM failed"
49
50if [[ -z "${OUTPUT}" ]]; then
51 die "LLM returned empty output"
52fi
53
54printf %s "$OUTPUT" | wl-copy
55notify low 1500 "Copied to clipboard"
56
57TMP_DIR="${XDG_RUNTIME_DIR:-/tmp}"
58TMP_FILE="$(mktemp -p "$TMP_DIR" magic-brush.XXXXXX)"
59trap 'rm -f "$TMP_FILE"' EXIT
60printf %s "$OUTPUT" >"$TMP_FILE"
61
62alacritty --title "$APP_NAME" --hold --command cat -- "$TMP_FILE"