-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add --tmux option to replace fzf-tmux script
- Loading branch information
Showing
11 changed files
with
289 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package fzf | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"github.com/junegunn/fzf/src/tui" | ||
"github.com/junegunn/fzf/src/util" | ||
) | ||
|
||
func runTmux(args []string, opts *Options) (int, error) { | ||
ns := time.Now().UnixNano() | ||
|
||
output := filepath.Join(os.TempDir(), fmt.Sprintf("fzf-tmux-output-%d", ns)) | ||
if err := mkfifo(output, 0666); err != nil { | ||
return ExitError, err | ||
} | ||
defer os.Remove(output) | ||
|
||
// Find fzf executable | ||
fzf := "fzf" | ||
if found, err := os.Executable(); err == nil { | ||
fzf = found | ||
} | ||
|
||
// Prepare arguments | ||
args = append([]string{"--bind=ctrl-z:ignore"}, args...) | ||
if opts.BorderShape == tui.BorderUndefined { | ||
args = append(args, "--border") | ||
} | ||
args = append(args, "--no-height") | ||
args = append(args, "--no-tmux") | ||
argStr := "" | ||
for _, arg := range args { | ||
// %q formatting escapes $'foo\nbar' to "foo\nbar" | ||
argStr += " '" + strings.ReplaceAll(arg, "'", "'\\''") + "'" | ||
} | ||
|
||
// Build command | ||
var command string | ||
if !util.IsTty() { | ||
input := filepath.Join(os.TempDir(), fmt.Sprintf("fzf-tmux-input-%d", ns)) | ||
if err := mkfifo(input, 0644); err != nil { | ||
return ExitError, err | ||
} | ||
defer os.Remove(input) | ||
|
||
go func() { | ||
inputFile, err := os.OpenFile(input, os.O_WRONLY, 0) | ||
if err != nil { | ||
return | ||
} | ||
io.Copy(inputFile, os.Stdin) | ||
inputFile.Close() | ||
}() | ||
|
||
command = fmt.Sprintf(`%q%s < %q > %q`, fzf, argStr, input, output) | ||
} else { | ||
command = fmt.Sprintf(`%q%s > %q`, fzf, argStr, output) | ||
} | ||
|
||
// Get current directory | ||
dir, err := os.Getwd() | ||
if err != nil { | ||
dir = "." | ||
} | ||
|
||
// Set tmux options for popup placement | ||
// C Both The centre of the terminal | ||
// R -x The right side of the terminal | ||
// P Both The bottom left of the pane | ||
// M Both The mouse position | ||
// W Both The window position on the status line | ||
// S -y The line above or below the status line | ||
tmuxArgs := []string{"display-popup", "-E", "-B", "-d", dir} | ||
switch opts.Tmux.position { | ||
case posUp: | ||
tmuxArgs = append(tmuxArgs, "-xC", "-y0") | ||
case posDown: | ||
tmuxArgs = append(tmuxArgs, "-xC", "-yS") | ||
case posLeft: | ||
tmuxArgs = append(tmuxArgs, "-x0", "-yC") | ||
case posRight: | ||
tmuxArgs = append(tmuxArgs, "-xR", "-yC") | ||
case posCenter: | ||
tmuxArgs = append(tmuxArgs, "-xC", "-yC") | ||
} | ||
tmuxArgs = append(tmuxArgs, "-w"+opts.Tmux.width.String()) | ||
tmuxArgs = append(tmuxArgs, "-h"+opts.Tmux.height.String()) | ||
|
||
// To ensure that the options are processed by a POSIX-compliant shell, | ||
// we need to write the command to a temporary file and execute it with sh. | ||
temp := writeTemporaryFile([]string{command}, "\n") | ||
defer os.Remove(temp) | ||
|
||
tmuxArgs = append(tmuxArgs, "sh", temp) | ||
cmd := exec.Command("tmux", tmuxArgs...) | ||
cmd.Stdin = tui.TtyIn() | ||
cmd.Stderr = os.Stderr | ||
cmd.Stdout = os.Stdout | ||
cmd.Env = os.Environ() | ||
cmd.Start() | ||
|
||
// Take the output | ||
go func() { | ||
outputFile, err := os.OpenFile(output, os.O_RDONLY, 0) | ||
if err != nil { | ||
return | ||
} | ||
io.Copy(os.Stdout, outputFile) | ||
outputFile.Close() | ||
}() | ||
|
||
if err := cmd.Wait(); err != nil { | ||
if exitError, ok := err.(*exec.ExitError); ok { | ||
return exitError.ExitCode(), err | ||
} | ||
} | ||
|
||
return ExitOk, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//go:build !windows | ||
|
||
package fzf | ||
|
||
import "golang.org/x/sys/unix" | ||
|
||
func mkfifo(path string, mode uint32) error { | ||
return unix.Mkfifo(path, mode) | ||
} |
Oops, something went wrong.