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 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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

\fIclick-header\fR
.RS
Triggered when a mouse click occurs within the header. Sets \fBFZF_CLICK_HEADER_LINE\fR and \fBFZF_CLICK_HEADER_COLUMN\fR environment variables.

e.g.
\fBprintf "head1\\nhead2" | fzf --header-lines=2 --bind click-header:transform-prompt:"printf \\${FZF_CLICK_HEADER_LINE}x\\${FZF_CLICK_HEADER_COLUMN}"\fR
junegunn marked this conversation as resolved.
Show resolved Hide resolved

.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 "click-header":
add(tui.ClickHeader)
case "alt-enter", "alt-return":
chords[tui.CtrlAltKey('m')] = key
case "alt-space":
Expand Down
44 changes: 41 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
clickHeaderLine int
clickHeaderColumn int
}

type selectedItem struct {
Expand Down Expand Up @@ -845,6 +847,10 @@ func (t *Terminal) environ() []string {
if t.listenPort != nil {
env = append(env, fmt.Sprintf("FZF_PORT=%d", *t.listenPort))
}
if t.clickHeaderLine > 0 {
env = append(env, fmt.Sprintf("FZF_CLICK_HEADER_LINE=%d", t.clickHeaderLine))
env = append(env, fmt.Sprintf("FZF_CLICK_HEADER_COLUMN=%d", t.clickHeaderColumn))
}
env = append(env, "FZF_QUERY="+string(t.input))
env = append(env, "FZF_ACTION="+t.lastAction.Name())
env = append(env, "FZF_KEY="+t.lastKey)
Expand Down Expand Up @@ -3991,10 +3997,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 +4015,38 @@ 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.clickHeaderLine = my + 1
t.clickHeaderColumn = mx + 1
evt := tui.ClickHeader
res := doActions(actionsFor(evt))
t.clickHeaderLine = 0
t.clickHeaderColumn = 0
return res
}
}
}
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
ClickHeader
)

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