Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: header-click event for reporting clicks within header #3768

Merged
merged 8 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions man/man1/fzf.1
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,15 @@ e.g.
\fBfzf --bind space:jump,jump:accept,jump-cancel:abort\fR
.RE

\fIheader-click\fR
kuremu marked this conversation as resolved.
Show resolved Hide resolved
.RS
Triggered when a mouse click occurs within the header. Sets \fBFZF_HEADERCLICK_LINE\fR and \fBFZF_HEADERCLICK_COLUMN\fR environment variables.
kuremu marked this conversation as resolved.
Show resolved Hide resolved

e.g.
\fBprintf "head1\\nhead2" | fzf --header-lines=2 --bind header-click:transform-prompt:"printf \\${FZF_HEADERCLICK_LINE}x\\${FZF_HEADERCLICK_COLUMN}"\fR

.RE

.SS AVAILABLE ACTIONS:
A key or an event can be bound to one or more of the following actions.

Expand Down
2 changes: 2 additions & 0 deletions src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,8 @@ func parseKeyChordsImpl(str string, message string, exit func(string)) map[tui.E
add(tui.Jump)
case "jump-cancel":
add(tui.JumpCancel)
case "header-click":
add(tui.HeaderClick)
case "alt-enter", "alt-return":
chords[tui.CtrlAltKey('m')] = key
case "alt-space":
Expand Down
39 changes: 36 additions & 3 deletions src/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ type Terminal struct {
areaLines int
areaColumns int
forcePreview bool
headerClickLine int
headerClickColumn int
}

type selectedItem struct {
Expand Down Expand Up @@ -857,6 +859,8 @@ func (t *Terminal) environ() []string {
env = append(env, fmt.Sprintf("FZF_LINES=%d", t.areaLines))
env = append(env, fmt.Sprintf("FZF_COLUMNS=%d", t.areaColumns))
env = append(env, fmt.Sprintf("FZF_POS=%d", util.Min(t.merger.Length(), t.cy+1)))
env = append(env, fmt.Sprintf("FZF_HEADERCLICK_LINE=%d", t.headerClickLine))
env = append(env, fmt.Sprintf("FZF_HEADERCLICK_COLUMN=%d", t.headerClickColumn))
kuremu marked this conversation as resolved.
Show resolved Hide resolved
return env
}

Expand Down Expand Up @@ -3991,10 +3995,10 @@ func (t *Terminal) Loop() {
}

if me.Down {
mx = util.Constrain(mx-t.promptLen, 0, len(t.input))
if my == t.promptLine() && mx >= 0 {
mx_cons := util.Constrain(mx-t.promptLen, 0, len(t.input))
if my == t.promptLine() && mx_cons >= 0 {
// Prompt
t.cx = mx + t.xoffset
t.cx = mx_cons + t.xoffset
} else if my >= min {
t.vset(t.offset + my - min)
req(reqList)
Expand All @@ -4009,6 +4013,35 @@ func (t *Terminal) Loop() {
}
}
return doActions(actionsFor(evt))
} else {
// Header
lineOffset := 0
numLines := t.visibleHeaderLines()
if !t.headerFirst {
// offset for info line
if t.noSeparatorLine() {
lineOffset = 1
} else {
lineOffset = 2
}
} else {
// adjust for too-small window
numItems := t.areaLines - numLines
if !t.noSeparatorLine() {
numItems -= 1
}
if numItems < 0 {
numLines += numItems
}
}
my = util.Constrain(my-lineOffset, -1, numLines)
mx -= 2 // offset gutter
if my >= 0 && my < numLines && mx >= 0 {
t.headerClickLine = my
t.headerClickColumn = mx
kuremu marked this conversation as resolved.
Show resolved Hide resolved
evt := tui.HeaderClick
return doActions(actionsFor(evt))
}
}
}
case actReload, actReloadSync:
Expand Down
1 change: 1 addition & 0 deletions src/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ const (
Result
Jump
JumpCancel
HeaderClick
)

func (t EventType) AsEvent() Event {
Expand Down