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

Refactor pager commands & synchronous shell commands (no-op) #1243

Merged
merged 4 commits into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
119 changes: 57 additions & 62 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,30 @@ func (app *app) loop() {
}
}

type cleanFunc func()
func (app *app) runCmdSync(cmd *exec.Cmd, pause_after bool) {
app.nav.previewChan <- ""
app.nav.dirPreviewChan <- nil

var noopCleanFunc = func() {}
if err := app.ui.suspend(); err != nil {
log.Printf("suspend: %s", err)
}
defer func() {
if err := app.ui.resume(); err != nil {
app.quit()
os.Exit(3)
}
}()

if err := cmd.Run(); err != nil {
app.ui.echoerrf("running shell: %s", err)
}
if pause_after {
anyKey()
}

app.ui.loadFile(app, true)
app.nav.renew()
}

// This function is used to run a shell command. Modes are as follows:
//
Expand All @@ -485,8 +506,7 @@ var noopCleanFunc = func() {}
// % No No Yes Yes Yes Statline for input/output
// ! Yes No Yes Yes Yes Pause and then resume
// & No Yes No No No Do nothing
// $| No No Pipe Yes Yes Pause, Pipe execute, return cleanup to resume
func (app *app) runShell(s string, args []string, prefix string) cleanFunc {
func (app *app) runShell(s string, args []string, prefix string) {
app.nav.exportFiles()
app.ui.exportSizes()
exportOpts()
Expand All @@ -496,49 +516,19 @@ func (app *app) runShell(s string, args []string, prefix string) cleanFunc {
var out io.Reader
var err error
switch prefix {
case "$|":
var stdin io.WriteCloser
stdin, err = cmd.StdinPipe()
if err != nil {
log.Printf("writing stdin: %s", err)
}
app.cmdIn = stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

app.nav.previewChan <- ""
app.nav.dirPreviewChan <- nil

if err := app.ui.suspend(); err != nil {
log.Printf("suspend: %s", err)
}

err = cmd.Start()
case "$", "!":
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

app.nav.previewChan <- ""
app.nav.dirPreviewChan <- nil

if err := app.ui.suspend(); err != nil {
log.Printf("suspend: %s", err)
}
defer func() {
if err := app.ui.resume(); err != nil {
app.quit()
os.Exit(3)
return
}
}()
defer app.nav.renew()
app.runCmdSync(cmd, prefix == "!")
return
}

err = cmd.Run()
case "%":
shellSetPG(cmd)
// We are running the command asynchroniously
if prefix == "%" {
if app.ui.cmdPrefix == ">" {
return noopCleanFunc
return
}
stdin, err := cmd.StdinPipe()
if err != nil {
Expand All @@ -551,21 +541,13 @@ func (app *app) runShell(s string, args []string, prefix string) cleanFunc {
}
out = stdout
cmd.Stderr = cmd.Stdout
fallthrough
case "&":
shellSetPG(cmd)
err = cmd.Start()
}

if err != nil {
shellSetPG(cmd)
if err = cmd.Start(); err != nil {
app.ui.echoerrf("running shell: %s", err)
}

switch prefix {
case "!":
anyKey()
}

// Asynchronous shell invocations return immediately without waiting for the
// command to finish, so there is no point refreshing the preview if nothing
// has changed yet.
Expand Down Expand Up @@ -615,17 +597,30 @@ func (app *app) runShell(s string, args []string, prefix string) cleanFunc {
log.Printf("running shell: %s", err)
}
}()
case "$|":
return func() {
if err := cmd.Wait(); err != nil {
log.Printf("running shell: %s", err)
}
app.nav.renew()
if err := app.ui.resume(); err != nil {
app.quit()
os.Exit(3)
}
}
}
return noopCleanFunc

}

func (app *app) runPagerOnText(text io.Reader) {
app.nav.exportFiles()
app.ui.exportSizes()
exportOpts()

cmd := shellCommand(envPager, nil)

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

stdin, err := cmd.StdinPipe()
if err != nil {
app.ui.echoerrf("obtaining stdin pipe: %s", err)
return
}

go func() {
io.Copy(stdin, text)
stdin.Close()
}()

app.runCmdSync(cmd, false)
}
15 changes: 3 additions & 12 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -2502,20 +2502,11 @@ func (e *callExpr) eval(app *app, args []string) {
app.ui.cmdAccLeft = acc
update(app)
case "maps":
cleanUp := app.runShell(envPager, nil, "$|")
io.Copy(app.cmdIn, listBinds(gOpts.keys))
app.cmdIn.Close()
cleanUp()
app.runPagerOnText(listBinds(gOpts.keys))
case "cmaps":
cleanUp := app.runShell(envPager, nil, "$|")
io.Copy(app.cmdIn, listBinds(gOpts.cmdkeys))
app.cmdIn.Close()
cleanUp()
app.runPagerOnText(listBinds(gOpts.cmdkeys))
case "jumps":
cleanUp := app.runShell(envPager, nil, "$|")
io.Copy(app.cmdIn, listJumps(app.nav.jumpList, app.nav.jumpListInd))
app.cmdIn.Close()
cleanUp()
app.runPagerOnText(listJumps(app.nav.jumpList, app.nav.jumpListInd))
default:
cmd, ok := gOpts.cmds[e.name]
if !ok {
Expand Down