Add SUPER+A quick Claude question popup that follows the cursor
cursor_ask.sh runs in a kitty window that cursor_follow keeps beside the cursor. It holds keyboard focus while following and stops following once a question is sent, so the answer can be scrolled. Answers stream from claude -p (Haiku), kept short, with markdown styled and word-wrapped, and highlighted text is sent along as context. Code requests get the library and functions to use rather than the code itself. Also includes the earlier cursor_follow additions: centered mode (SUPER+CTRL+P), playerctl video keys, gap keys and the SUPER+ALT+P key list. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Executable
+153
@@ -0,0 +1,153 @@
|
||||
#!/bin/bash
|
||||
# cursor_ask: ask Claude a quick question in a popup that follows the mouse cursor
|
||||
# Opened by SUPER+A (see cursor_follow.lua), which freezes the window once the title
|
||||
# changes to "cursor-ask: <question>". Highlighted text is sent along with the first question.
|
||||
# Ctrl+C stops an answer, Ctrl+D or Esc closes the window.
|
||||
|
||||
model=${CURSOR_ASK_MODEL:-haiku}
|
||||
system="You are a terse reference assistant in a small terminal popup, about 70 columns by 20 lines. \
|
||||
Reply with only the answer, starting with its first word, in 1-3 short sentences. \
|
||||
No preamble, alternatives, caveats or summaries. No headings, tables or bold. \
|
||||
For requests to write or make code you are a guide, not a code writer: reply with the library to use, \
|
||||
then the key functions or methods in the order they would be called, one per line as \`name()\` \
|
||||
and a few words on what it does. Write actual code only if the user explicitly asks to see it. \
|
||||
If the question could mean two things, answer the likelier one and name the other in a few words. \
|
||||
The user will ask a follow-up if they want more."
|
||||
# highlighted text sent last time, so a stale selection is not sent again
|
||||
seen="${XDG_RUNTIME_DIR:-/tmp}/cursor-ask-selection"
|
||||
dir="${XDG_CACHE_HOME:-$HOME/.cache}/cursor-ask"
|
||||
|
||||
err=$(mktemp)
|
||||
trap 'rm -f "$err"' EXIT
|
||||
# Ctrl+C only interrupts the running answer or the prompt
|
||||
trap 'printf "\n"' INT
|
||||
|
||||
# keep these throwaway sessions out of real projects
|
||||
mkdir -p "$dir" && cd "$dir" || exit 1
|
||||
|
||||
title() {
|
||||
printf '\e]2;%s\a' "$1"
|
||||
}
|
||||
|
||||
# style markdown with terminal escapes and wrap it at word boundaries, a line at a time
|
||||
# so the answer still streams in
|
||||
md() {
|
||||
awk -v width="$(tput cols)" '
|
||||
function inline(s) {
|
||||
s = gensub(/\*\*([^*]+)\*\*/, "\033[1m\\1\033[22m", "g", s)
|
||||
s = gensub(/(^|[^*])\*([^* ][^*]*)\*/, "\\1\033[3m\\2\033[23m", "g", s)
|
||||
return gensub(/\[([^]]+)\]\(([^)]+)\)/, "\033[4m\\1\033[24m (\\2)", "g", s)
|
||||
}
|
||||
# length on screen, without the escape sequences
|
||||
function len(s) {
|
||||
gsub(/\033\[[0-9;]*m/, "", s)
|
||||
return length(s)
|
||||
}
|
||||
# print s word-wrapped, starting with first and indenting the following lines with rest
|
||||
function wrap(s, first, rest, w, n, i, line, used, fresh) {
|
||||
n = split(s, w, " ")
|
||||
line = first; used = len(first); fresh = 1
|
||||
for (i = 1; i <= n; i++) {
|
||||
if (!fresh && used + 1 + len(w[i]) > width) {
|
||||
print line
|
||||
line = rest; used = len(rest); fresh = 1
|
||||
}
|
||||
line = line (fresh ? "" : " ") w[i]
|
||||
used += (fresh ? 0 : 1) + len(w[i])
|
||||
fresh = 0
|
||||
}
|
||||
print line
|
||||
}
|
||||
/^[[:space:]]*```/ { code = !code; next }
|
||||
code { printf " \033[33m%s\033[39m\n", $0; fflush(); next }
|
||||
{
|
||||
pre = post = rest = ""
|
||||
if (match($0, /^#+[[:space:]]+/)) {
|
||||
pre = "\033[1;36m"; post = "\033[0m"
|
||||
} else if (match($0, /^([[:space:]]*)[-*+][[:space:]]+/, m)) {
|
||||
pre = m[1] "• "; rest = sprintf("%*s", len(pre), "")
|
||||
} else if (match($0, /^>[[:space:]]?/)) {
|
||||
pre = rest = "\033[2m│\033[22m "
|
||||
}
|
||||
line = substr($0, RLENGTH > 0 ? RLENGTH + 1 : 1)
|
||||
# balanced backticks split the line into text and inline code, alternately
|
||||
n = split(line, p, "`")
|
||||
if (n % 2) {
|
||||
line = ""
|
||||
for (i = 1; i <= n; i++) line = line (i % 2 ? inline(p[i]) : "\033[33m" p[i] "\033[39m")
|
||||
} else {
|
||||
line = inline(line)
|
||||
}
|
||||
wrap(line post, pre, rest)
|
||||
fflush()
|
||||
}'
|
||||
}
|
||||
|
||||
# the first character replaces the "thinking…" line; fails when nothing came back
|
||||
render() {
|
||||
local c
|
||||
if IFS= read -r -n1 -d '' c; then
|
||||
printf '\r\e[K%s' "$c"
|
||||
cat
|
||||
else
|
||||
printf '\r\e[K'
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
title cursor-ask
|
||||
|
||||
selection=$(wl-paste --primary --no-newline 2>/dev/null | head -c 20000)
|
||||
if [[ -n $selection && $selection != "$(cat "$seen" 2>/dev/null)" ]]; then
|
||||
printf '%s' "$selection" >"$seen"
|
||||
width=$(($(tput cols) - 2))
|
||||
printf '\e[2mwith highlighted text (Enter on an empty line drops it):\n'
|
||||
while IFS= read -r line; do
|
||||
printf '│ %s\n' "${line:0:width}"
|
||||
done < <(head -n 3 <<<"$selection")
|
||||
lines=$(wc -l <<<"$selection")
|
||||
((lines > 3)) && printf '│ … %d lines\n' "$lines"
|
||||
printf '\e[0m\n'
|
||||
else
|
||||
selection=
|
||||
fi
|
||||
|
||||
session=$(cat /proc/sys/kernel/random/uuid)
|
||||
while true; do
|
||||
IFS= read -e -r -p $'\001\e[1;36m\002> \001\e[0m\002' q
|
||||
status=$?
|
||||
((status == 1)) && exit # Ctrl+D
|
||||
((status > 128)) && continue # Ctrl+C
|
||||
if [[ -z ${q//[[:space:]]/} ]]; then
|
||||
if [[ -n $selection ]]; then
|
||||
selection=
|
||||
printf '\e[2m(dropped the highlighted text)\e[0m\n'
|
||||
fi
|
||||
continue
|
||||
fi
|
||||
history -s "$q"
|
||||
title "cursor-ask: ${q:0:60}"
|
||||
|
||||
prompt=$q
|
||||
if [[ -n $selection ]]; then
|
||||
prompt=$(printf '<selection>\n%s\n</selection>\n\n%s' "$selection" "$q")
|
||||
selection=
|
||||
fi
|
||||
# follow-ups continue the conversation once the first call has saved it
|
||||
if compgen -G "$HOME/.claude/projects/*/$session.jsonl" >/dev/null; then
|
||||
resume=(--resume "$session")
|
||||
else
|
||||
resume=(--session-id "$session")
|
||||
fi
|
||||
|
||||
printf '\e[2mthinking…\e[0m'
|
||||
claude -p "$prompt" --model "$model" --system-prompt "$system" "${resume[@]}" \
|
||||
--tools "" --strict-mcp-config \
|
||||
--output-format stream-json --verbose --include-partial-messages 2>"$err" |
|
||||
jq --unbuffered -j '
|
||||
if .type == "stream_event" and .event.delta.type? == "text_delta" then .event.delta.text
|
||||
elif .type == "result" and .is_error then "\u001b[31m\(.result)\u001b[0m"
|
||||
else empty end' |
|
||||
md | render || printf '\e[31m%s\e[0m' "$(tail -n 3 "$err")"
|
||||
printf '\n'
|
||||
done
|
||||
+201
-24
@@ -1,6 +1,10 @@
|
||||
-- cursor_follow: keep a floating window glued next to the mouse cursor (Hyprland Lua config)
|
||||
-- Picture-in-Picture windows follow automatically; SUPER+P makes the focused window follow,
|
||||
-- or pauses/resumes following; SUPER+SHIFT+P stops.
|
||||
-- or pauses/resumes following; SUPER+SHIFT+P stops; SUPER+CTRL+P toggles a bigger window centered
|
||||
-- above the cursor; SUPER+ALT+Space/Left/Right play/pause and seek the video (via playerctl);
|
||||
-- SUPER+ALT+Up/Down widen/narrow the gap to the cursor; SUPER+ALT+P lists these keybindings.
|
||||
-- SUPER+A opens a terminal beside the cursor to ask Claude a quick question (cursor_ask.sh); it holds
|
||||
-- keyboard focus while following and stops following once the question is sent, so it can be scrolled.
|
||||
-- Load from hyprland.lua with: require("cursor_follow")
|
||||
|
||||
local M = {
|
||||
@@ -8,41 +12,95 @@ local M = {
|
||||
width = 480, -- size applied when a window starts following
|
||||
height = 270,
|
||||
gap = 24, -- px between the cursor hotspot and the window
|
||||
gap_step = 8, -- px added/removed per gap key press
|
||||
margin = 8, -- keep this far from monitor edges
|
||||
dir = { x = 1, y = -1 }, -- preferred side of the cursor (right, above); flips at screen edges
|
||||
dir = { x = 1, y = -1 }, -- starting side of the cursor (right, above); flips at screen edges
|
||||
centered_scale = 1.25, -- window size multiplier while centered above the cursor
|
||||
interval = 8, -- ms between cursor polls
|
||||
player = "firefox", -- MPRIS player for the video keys (Waterfox shows up as firefox)
|
||||
seek = 10, -- seconds skipped by the seek keys
|
||||
ask_width = 640, -- size of the SUPER+A question window
|
||||
ask_height = 420,
|
||||
},
|
||||
target = nil, -- address of the followed window
|
||||
paused = false,
|
||||
centered = false, -- window centered horizontally above the cursor instead of beside it
|
||||
}
|
||||
|
||||
local cfg = M.cfg
|
||||
local last_x, last_y
|
||||
-- current side of the cursor per axis; stays flipped until the window hits the opposite edge
|
||||
local side = { x = cfg.dir.x, y = cfg.dir.y }
|
||||
local timer
|
||||
-- marks the followed window so it can be picked back up after a config reload
|
||||
local tag = "cursor-follow"
|
||||
-- marks it as centered too, so the mode survives a reload
|
||||
local centered_tag = "cursor-follow-centered"
|
||||
-- class of the SUPER+A question window; cursor_ask.sh retitles it to "cursor-ask: <question>"
|
||||
local ask_class = "cursor-ask"
|
||||
-- window that was following before the question window took over, picked back up when it closes
|
||||
local resume_after_ask
|
||||
|
||||
local function selector()
|
||||
return "address:" .. M.target
|
||||
end
|
||||
|
||||
-- Position along one axis: on side `dir` of the cursor, flipped if it would leave the monitor
|
||||
local function axis(cur, size, lo, hi, dir)
|
||||
local after, before = cur + cfg.gap, cur - cfg.gap - size
|
||||
local p = dir > 0 and after or before
|
||||
if dir > 0 and after + size > hi - cfg.margin then
|
||||
p = before
|
||||
elseif dir < 0 and before < lo + cfg.margin then
|
||||
p = after
|
||||
local function is_ask(w)
|
||||
return w ~= nil and w.class == ask_class
|
||||
end
|
||||
|
||||
-- a question has been sent from this window
|
||||
local function asked(w)
|
||||
return w.title:sub(1, #ask_class + 2) == ask_class .. ": "
|
||||
end
|
||||
|
||||
-- focus follows the mouse and the followed window is never under the cursor, so the question
|
||||
-- window holds keyboard focus while it follows
|
||||
local function lock_focus(on)
|
||||
local w = M.target and hl.get_window(selector())
|
||||
if is_ask(w) then
|
||||
hl.dispatch(hl.dsp.window.set_prop({ window = selector(), prop = "stay_focused", value = on and "1" or "0" }))
|
||||
end
|
||||
end
|
||||
|
||||
local function set_tag(name, on)
|
||||
hl.dispatch(hl.dsp.window.tag({ window = selector(), tag = (on and "+" or "-") .. name }))
|
||||
end
|
||||
|
||||
local function resize(x, y)
|
||||
hl.dispatch(hl.dsp.window.resize({ window = selector(), x = math.floor(x + 0.5), y = math.floor(y + 0.5) }))
|
||||
end
|
||||
|
||||
-- centered mode always starts with the window above the cursor
|
||||
local function reset_side()
|
||||
side.x, side.y = cfg.dir.x, M.centered and -1 or cfg.dir.y
|
||||
end
|
||||
|
||||
-- Keep a window of `size` starting at `p` within [lo, hi] minus the margin
|
||||
local function clamp(p, size, lo, hi)
|
||||
p = math.max(lo + cfg.margin, math.min(p, hi - cfg.margin - size))
|
||||
return math.floor(p + 0.5)
|
||||
end
|
||||
|
||||
-- Position along one axis: on side `dir` of the cursor, flipped if it would leave the monitor
|
||||
-- and the other side has room. Returns the position and the (possibly flipped) side.
|
||||
local function axis(cur, size, lo, hi, dir)
|
||||
local after, before = cur + cfg.gap, cur - cfg.gap - size
|
||||
local fits_after, fits_before = after + size <= hi - cfg.margin, before >= lo + cfg.margin
|
||||
if dir > 0 and not fits_after and fits_before then
|
||||
dir = -1
|
||||
elseif dir < 0 and not fits_before and fits_after then
|
||||
dir = 1
|
||||
end
|
||||
return clamp(dir > 0 and after or before, size, lo, hi), dir
|
||||
end
|
||||
|
||||
function M.detach()
|
||||
timer:set_enabled(false)
|
||||
if M.target then
|
||||
hl.dispatch(hl.dsp.window.tag({ window = selector(), tag = "-" .. tag }))
|
||||
set_tag(tag, false)
|
||||
set_tag(centered_tag, false)
|
||||
lock_focus(false)
|
||||
end
|
||||
M.target, M.paused = nil, false
|
||||
last_x, last_y = nil, nil
|
||||
@@ -61,8 +119,13 @@ local function place()
|
||||
end
|
||||
|
||||
local mw, mh = m.width / m.scale, m.height / m.scale
|
||||
local x = axis(c.x, w.size.x, m.x, m.x + mw, cfg.dir.x)
|
||||
local y = axis(c.y, w.size.y, m.y, m.y + mh, cfg.dir.y)
|
||||
local x, y
|
||||
if M.centered then
|
||||
x = clamp(c.x - w.size.x / 2, w.size.x, m.x, m.x + mw)
|
||||
else
|
||||
x, side.x = axis(c.x, w.size.x, m.x, m.x + mw, side.x)
|
||||
end
|
||||
y, side.y = axis(c.y, w.size.y, m.y, m.y + mh, side.y)
|
||||
if x == last_x and y == last_y then
|
||||
return
|
||||
end
|
||||
@@ -79,15 +142,19 @@ function M.attach(w, keep_size)
|
||||
M.detach()
|
||||
end
|
||||
M.target = w.address
|
||||
reset_side()
|
||||
local sel = selector()
|
||||
hl.dispatch(hl.dsp.window.tag({ window = sel, tag = "+" .. tag }))
|
||||
set_tag(tag, true)
|
||||
set_tag(centered_tag, M.centered)
|
||||
hl.dispatch(hl.dsp.window.float({ window = sel, action = "enable" }))
|
||||
hl.dispatch(hl.dsp.window.pin({ window = sel, action = "enable" }))
|
||||
-- moves would otherwise animate and trail behind the cursor
|
||||
hl.dispatch(hl.dsp.window.set_prop({ window = sel, prop = "no_anim", value = "1" }))
|
||||
if not keep_size then
|
||||
hl.dispatch(hl.dsp.window.resize({ window = sel, x = cfg.width, y = cfg.height }))
|
||||
local s = M.centered and cfg.centered_scale or 1
|
||||
resize(cfg.width * s, cfg.height * s)
|
||||
end
|
||||
lock_focus(true)
|
||||
timer:set_enabled(true)
|
||||
end
|
||||
|
||||
@@ -95,6 +162,22 @@ function M.toggle_pause()
|
||||
M.paused = not M.paused
|
||||
last_x, last_y = nil, nil
|
||||
timer:set_enabled(not M.paused)
|
||||
lock_focus(not M.paused)
|
||||
end
|
||||
|
||||
function M.change_gap(d)
|
||||
cfg.gap = math.max(0, cfg.gap + d)
|
||||
end
|
||||
|
||||
function M.toggle_centered()
|
||||
M.centered = not M.centered
|
||||
reset_side()
|
||||
local w = M.target and hl.get_window(selector())
|
||||
if w then
|
||||
set_tag(centered_tag, M.centered)
|
||||
local s = M.centered and cfg.centered_scale or 1 / cfg.centered_scale
|
||||
resize(w.size.x * s, w.size.y * s)
|
||||
end
|
||||
end
|
||||
|
||||
---------------
|
||||
@@ -114,6 +197,15 @@ hl.window_rule({
|
||||
keep_aspect_ratio = true,
|
||||
})
|
||||
|
||||
hl.window_rule({
|
||||
name = "cursor-follow-ask",
|
||||
match = { class = "^(" .. ask_class .. ")$" },
|
||||
float = true,
|
||||
pin = true,
|
||||
size = cfg.ask_width .. " " .. cfg.ask_height,
|
||||
no_anim = true,
|
||||
})
|
||||
|
||||
----------------
|
||||
---- EVENTS ----
|
||||
----------------
|
||||
@@ -124,13 +216,33 @@ local function maybe_attach_pip(w)
|
||||
end
|
||||
end
|
||||
|
||||
local function maybe_attach_ask(w)
|
||||
if is_ask(w) and w.address ~= M.target then
|
||||
resume_after_ask = M.target
|
||||
M.attach(w, true)
|
||||
end
|
||||
end
|
||||
|
||||
hl.on("window.open", maybe_attach_pip)
|
||||
hl.on("window.open", maybe_attach_ask)
|
||||
-- browsers can set the PiP title after the window maps
|
||||
hl.on("window.title", maybe_attach_pip)
|
||||
|
||||
-- stop following once a question is sent, so the answer can be scrolled with the mouse
|
||||
hl.on("window.title", function(w)
|
||||
if w.address == M.target and is_ask(w) and asked(w) and not M.paused then
|
||||
M.toggle_pause()
|
||||
end
|
||||
end)
|
||||
|
||||
hl.on("window.close", function(w)
|
||||
if w.address == M.target then
|
||||
M.detach()
|
||||
-- hand following back to the window the question window took over from
|
||||
local prev = is_ask(w) and resume_after_ask and hl.get_window("address:" .. resume_after_ask)
|
||||
if prev and prev.floating then
|
||||
M.attach(prev, true)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
@@ -138,18 +250,79 @@ end)
|
||||
---- KEYBINDS ----
|
||||
------------------
|
||||
|
||||
hl.bind("SUPER + P", function()
|
||||
if M.target then
|
||||
M.toggle_pause()
|
||||
else
|
||||
local w = hl.get_active_window()
|
||||
if w then
|
||||
M.attach(w)
|
||||
local keys -- { keys, description, action, bind options }, listed by M.help
|
||||
|
||||
local function player(args)
|
||||
return hl.dsp.exec_cmd("playerctl -p " .. cfg.player .. " " .. args)
|
||||
end
|
||||
|
||||
function M.help()
|
||||
local lines = {}
|
||||
for _, k in ipairs(keys) do
|
||||
lines[#lines + 1] = k[1] .. ": " .. k[2]
|
||||
end
|
||||
hl.exec_cmd("notify-send -t 10000 'cursor follow' '" .. table.concat(lines, "\n") .. "'")
|
||||
end
|
||||
|
||||
function M.toggle_ask()
|
||||
for _, w in ipairs(hl.get_windows()) do
|
||||
if is_ask(w) then
|
||||
hl.dispatch(hl.dsp.window.close({ window = "address:" .. w.address }))
|
||||
return
|
||||
end
|
||||
end
|
||||
end)
|
||||
hl.exec_cmd(
|
||||
"kitty --class "
|
||||
.. ask_class
|
||||
.. " -o confirm_os_window_close=0 -o window_padding_width=10 -o 'map escape close_window' -e "
|
||||
.. os.getenv("HOME")
|
||||
.. "/.config/hypr/cursor_ask.sh"
|
||||
)
|
||||
end
|
||||
|
||||
hl.bind("SUPER + SHIFT + P", M.detach)
|
||||
keys = {
|
||||
{
|
||||
"SUPER + P",
|
||||
"follow the focused window, or pause/resume",
|
||||
function()
|
||||
if M.target then
|
||||
M.toggle_pause()
|
||||
else
|
||||
local w = hl.get_active_window()
|
||||
if w then
|
||||
M.attach(w)
|
||||
end
|
||||
end
|
||||
end,
|
||||
},
|
||||
{ "SUPER + SHIFT + P", "stop following", M.detach },
|
||||
{ "SUPER + CTRL + P", "toggle a bigger window centered above the cursor", M.toggle_centered },
|
||||
{ "SUPER + ALT + space", "play/pause the video", player("play-pause") },
|
||||
{ "SUPER + ALT + left", "skip back " .. cfg.seek .. "s", player("position " .. cfg.seek .. "-"), { repeating = true } },
|
||||
{ "SUPER + ALT + right", "skip forward " .. cfg.seek .. "s", player("position " .. cfg.seek .. "+"), { repeating = true } },
|
||||
{
|
||||
"SUPER + ALT + up",
|
||||
"widen the gap to the cursor",
|
||||
function()
|
||||
M.change_gap(cfg.gap_step)
|
||||
end,
|
||||
{ repeating = true },
|
||||
},
|
||||
{
|
||||
"SUPER + ALT + down",
|
||||
"narrow the gap to the cursor",
|
||||
function()
|
||||
M.change_gap(-cfg.gap_step)
|
||||
end,
|
||||
{ repeating = true },
|
||||
},
|
||||
{ "SUPER + ALT + P", "show these keybindings", M.help },
|
||||
{ "SUPER + A", "ask Claude a quick question, or close the question window", M.toggle_ask },
|
||||
}
|
||||
|
||||
for _, k in ipairs(keys) do
|
||||
hl.bind(k[1], k[3], k[4])
|
||||
end
|
||||
|
||||
-- A config reload starts this module from scratch while the followed window stays pinned,
|
||||
-- so it would tag along across workspaces but stand still. Pick it back up.
|
||||
@@ -163,7 +336,11 @@ if not resumed then
|
||||
end
|
||||
end
|
||||
if resumed then
|
||||
M.centered = hl.get_windows({ tag = centered_tag })[1] ~= nil
|
||||
M.attach(resumed, true)
|
||||
if is_ask(resumed) and asked(resumed) then
|
||||
M.toggle_pause()
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user