Skip to content

Commit

Permalink
made terminal only open one window #680
Browse files Browse the repository at this point in the history
  • Loading branch information
matryer committed Aug 28, 2021
1 parent 5bf6171 commit 79675a3
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 49 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,14 @@ You can control xbar behaviour by modifying the `/Library/Application Support/xb
{
"autoupdate": true,
"terminal": {
"appleScriptTemplate2": ""
"appleScriptTemplate3": ""
}
}
```

* Changes take effect next time xbar starts
* `autoupdate` - (boolean) whether to keep xbar automatically updated or not
* `terminal.appleScriptTemplate2` - (string) the AppleScript to use when **Run in terminal** option is used (use `"false"` to turn this feature off)
* `terminal.appleScriptTemplate3` - (string) the AppleScript to use when **Run in terminal** option is used (use `"false"` to turn this feature off)

You can delete this file and restart xbar to reset to defaults.

Expand Down
3 changes: 2 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func (app *app) RefreshAll() {
}
for _, plugin := range app.plugins {
// Setup plugin
plugin.AppleScriptTemplate = app.settings.Terminal.AppleScriptTemplate3
plugin.OnCycle = app.onCycle
plugin.OnRefresh = app.onRefresh
if app.Verbose {
Expand Down Expand Up @@ -353,7 +354,7 @@ func (app *app) newXbarMenu(plugin *plugins.Plugin, asSubmenu bool) *menu.Menu {
})
if plugin != nil {
items = append(items, menu.Text("Run in terminal…", keys.CmdOrCtrl("t"), func(_ *menu.CallbackData) {
err := plugin.RunInTerminal(app.settings.Terminal.AppleScriptTemplate2)
err := plugin.RunInTerminal(app.settings.Terminal.AppleScriptTemplate3)
if err != nil {
_, err2 := app.runtime.Dialog.Message(&dialog.MessageDialog{
Type: dialog.ErrorDialog,
Expand Down
36 changes: 22 additions & 14 deletions app/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"path/filepath"
"sync"
Expand All @@ -20,24 +21,30 @@ type settings struct {
AutoUpdate bool `json:"autoupdate"`

Terminal struct {
AppleScriptTemplate2 string `json:"appleScriptTemplate2"`
AppleScriptTemplate3 string `json:"appleScriptTemplate3"`
} `json:"terminal"`
}

func (s *settings) setDefaults() {
if s.Terminal.AppleScriptTemplate2 == "" {
s.Terminal.AppleScriptTemplate2 = `
activate application "Terminal"
tell application "Terminal"
if not (exists window 1) then reopen
set quotedScriptName to quoted form of "{{ .Command }}"
{{ if .Params }}
set commandLine to {{ .Vars }} & " " & quotedScriptName & " " & {{ .Params }}
{{ else }}
set commandLine to {{ .Vars }} & " " & quotedScriptName
{{ end }}
do script commandLine
end tell
if s.Terminal.AppleScriptTemplate3 == "" {
s.Terminal.AppleScriptTemplate3 = `
set quotedScriptName to quoted form of "{{ .Command }}"
{{ if .Params }}
set commandLine to {{ .Vars }} & " " & quotedScriptName & " " & {{ .Params }}
{{ else }}
set commandLine to {{ .Vars }} & " " & quotedScriptName
{{ end }}
if application "Terminal" is running then
tell application "Terminal"
do script commandLine
activate
end tell
else
tell application "Terminal"
do script commandLine in window 1
activate
end tell
end if
`
}
}
Expand All @@ -60,6 +67,7 @@ func loadSettings(path string) (*settings, error) {
return nil, errors.Wrap(err, "Unmarshal")
}
s.setDefaults()
log.Printf("### - settings: %+v\n", s)
return s, nil
}

Expand Down
32 changes: 5 additions & 27 deletions pkg/plugins/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (i *Item) Action() ActionFunc {
actions = append(actions, actionHref(debugf, i.Params.Href))
}
if i.Params.Shell != "" {
actions = append(actions, actionShell(debugf, i, i.Params.Shell, i.Params.ShellParams, i.Plugin.Variables))
actions = append(actions, actionShell(debugf, i, i.Plugin.AppleScriptTemplate, i.Params.Shell, i.Params.ShellParams, i.Plugin.Variables))
}
if i.Params.Refresh {
shouldDelayBeforeRefresh := false
Expand Down Expand Up @@ -115,24 +115,15 @@ func actionHref(debugf DebugFunc, href string) ActionFunc {
}

// actionShell gets an ActionFunc that runs a shell command.
func actionShell(debugf DebugFunc, item *Item, command string, params, envVars []string) ActionFunc {
func actionShell(debugf DebugFunc, item *Item, appleScriptTemplate, command string, params, envVars []string) ActionFunc {
if item.Params.Terminal {
return actionShellTerminal(debugf, item, command, params, envVars)
return actionShellTerminal(debugf, item, appleScriptTemplate, command, params, envVars)
}
return func(ctx context.Context) {
var commandExec string
var commandArgs []string
// if item.Params.Terminal {
// shell := os.Getenv("SHELL")
// if shell == "" {
// shell = "/bin/bash"
// }
// commandExec = shell
// commandArgs = append([]string{command}, params...)
// } else {
commandExec = command
commandArgs = params
//}
debugf("exec: %s %s", commandExec, strings.Join(commandArgs, " "))
cmd := exec.CommandContext(context.Background(), commandExec, commandArgs...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Expand All @@ -156,30 +147,17 @@ func actionShell(debugf DebugFunc, item *Item, command string, params, envVars [
}

// actionShellTerminal runs shell commands where terminal=true.
func actionShellTerminal(debugf DebugFunc, item *Item, command string, params, envVars []string) ActionFunc {
func actionShellTerminal(debugf DebugFunc, item *Item, appleScriptTemplate, command string, params, envVars []string) ActionFunc {
return func(ctx context.Context) {
debugf("exec: RunInTerminal...")
script := `
activate application "Terminal"
tell application "Terminal"
if not (exists window 1) then reopen
set quotedScriptName to quoted form of "{{ .Command }}"
{{ if .Params }}
set commandLine to {{ .Vars }} & " " & quotedScriptName & " " & {{ .Params }}
{{ else }}
set commandLine to {{ .Vars }} & " " & quotedScriptName
{{ end }}
do script commandLine
end tell
`
command := strconv.Quote(command)
command = command[1 : len(command)-1] // trim quotes off
for i := range params {
params[i] = strconv.Quote(params[i])
params[i] = params[i][1 : len(params[i])-1] // trim quotes off
}
paramsStr := strconv.Quote(strings.Join(params, " "))
err := item.Plugin.runInTerminal(script, command, paramsStr, envVars)
err := item.Plugin.runInTerminal(appleScriptTemplate, command, paramsStr, envVars)
if err != nil {
debugf("exec: RunInTerminal: err=%s", err)
return
Expand Down
4 changes: 3 additions & 1 deletion pkg/plugins/item_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"

"github.com/leaanthony/go-ansi-parser"

"github.com/pkg/errors"
)

Expand Down Expand Up @@ -76,6 +75,9 @@ type ItemParams struct {
// Terminal indicates whether to run the shell command in a terminal or not.
// Default is false.
Terminal bool `json:"terminal"`
// // appleScriptTemplate3 is the template for the AppleScript
// // to run this action in a terminal.
// AppleScriptTemplate string `json:"-"`
// Refresh indicates whether clicking this item will cause the plugin
// to refresh or not.
Refresh bool `json:"refresh"`
Expand Down
12 changes: 8 additions & 4 deletions pkg/plugins/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ type Plugin struct {
// Called in TriggerRefresh() when updating the plugin menu to the
// refreshing state, before refreshSignal is triggered.
cycleSignal chan (struct{})

// appleScriptTemplate3 is the template for the AppleScript
// to run this action in a terminal.
AppleScriptTemplate string
}

// CleanFilename gets a clean human readable representation of the
Expand Down Expand Up @@ -279,8 +283,8 @@ func (p *Plugin) CurrentCycleItem() *Item {
return p.Items.CycleItems[p.CycleIndex]
}

func (p *Plugin) runInTerminal(appleScriptTemplate2, command, paramsStr string, vars []string) error {
tpl, err := template.New("appleScriptTemplate2").Parse(appleScriptTemplate2)
func (p *Plugin) runInTerminal(appleScriptTemplate3, command, paramsStr string, vars []string) error {
tpl, err := template.New("appleScriptTemplate3").Parse(appleScriptTemplate3)
if err != nil {
return err
}
Expand Down Expand Up @@ -315,8 +319,8 @@ func (p *Plugin) runInTerminal(appleScriptTemplate2, command, paramsStr string,

// RunInTerminal runs this plugin in a terminal using the template
// apple script.
func (p *Plugin) RunInTerminal(appleScriptTemplate2 string) error {
return p.runInTerminal(appleScriptTemplate2, p.Command, "", p.Variables)
func (p *Plugin) RunInTerminal(appleScriptTemplate3 string) error {
return p.runInTerminal(appleScriptTemplate3, p.Command, "", p.Variables)
}

// refresh runs the plugin and parses the output, updating the
Expand Down

0 comments on commit 79675a3

Please sign in to comment.