Skip to content

Commit 79675a3

Browse files
committed
made terminal only open one window #680
1 parent 5bf6171 commit 79675a3

File tree

6 files changed

+42
-49
lines changed

6 files changed

+42
-49
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,14 @@ You can control xbar behaviour by modifying the `/Library/Application Support/xb
361361
{
362362
"autoupdate": true,
363363
"terminal": {
364-
"appleScriptTemplate2": ""
364+
"appleScriptTemplate3": ""
365365
}
366366
}
367367
```
368368

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

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

app/app.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ func (app *app) RefreshAll() {
229229
}
230230
for _, plugin := range app.plugins {
231231
// Setup plugin
232+
plugin.AppleScriptTemplate = app.settings.Terminal.AppleScriptTemplate3
232233
plugin.OnCycle = app.onCycle
233234
plugin.OnRefresh = app.onRefresh
234235
if app.Verbose {
@@ -353,7 +354,7 @@ func (app *app) newXbarMenu(plugin *plugins.Plugin, asSubmenu bool) *menu.Menu {
353354
})
354355
if plugin != nil {
355356
items = append(items, menu.Text("Run in terminal…", keys.CmdOrCtrl("t"), func(_ *menu.CallbackData) {
356-
err := plugin.RunInTerminal(app.settings.Terminal.AppleScriptTemplate2)
357+
err := plugin.RunInTerminal(app.settings.Terminal.AppleScriptTemplate3)
357358
if err != nil {
358359
_, err2 := app.runtime.Dialog.Message(&dialog.MessageDialog{
359360
Type: dialog.ErrorDialog,

app/settings.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"encoding/json"
55
"io/ioutil"
6+
"log"
67
"os"
78
"path/filepath"
89
"sync"
@@ -20,24 +21,30 @@ type settings struct {
2021
AutoUpdate bool `json:"autoupdate"`
2122

2223
Terminal struct {
23-
AppleScriptTemplate2 string `json:"appleScriptTemplate2"`
24+
AppleScriptTemplate3 string `json:"appleScriptTemplate3"`
2425
} `json:"terminal"`
2526
}
2627

2728
func (s *settings) setDefaults() {
28-
if s.Terminal.AppleScriptTemplate2 == "" {
29-
s.Terminal.AppleScriptTemplate2 = `
30-
activate application "Terminal"
31-
tell application "Terminal"
32-
if not (exists window 1) then reopen
33-
set quotedScriptName to quoted form of "{{ .Command }}"
34-
{{ if .Params }}
35-
set commandLine to {{ .Vars }} & " " & quotedScriptName & " " & {{ .Params }}
36-
{{ else }}
37-
set commandLine to {{ .Vars }} & " " & quotedScriptName
38-
{{ end }}
39-
do script commandLine
40-
end tell
29+
if s.Terminal.AppleScriptTemplate3 == "" {
30+
s.Terminal.AppleScriptTemplate3 = `
31+
set quotedScriptName to quoted form of "{{ .Command }}"
32+
{{ if .Params }}
33+
set commandLine to {{ .Vars }} & " " & quotedScriptName & " " & {{ .Params }}
34+
{{ else }}
35+
set commandLine to {{ .Vars }} & " " & quotedScriptName
36+
{{ end }}
37+
if application "Terminal" is running then
38+
tell application "Terminal"
39+
do script commandLine
40+
activate
41+
end tell
42+
else
43+
tell application "Terminal"
44+
do script commandLine in window 1
45+
activate
46+
end tell
47+
end if
4148
`
4249
}
4350
}
@@ -60,6 +67,7 @@ func loadSettings(path string) (*settings, error) {
6067
return nil, errors.Wrap(err, "Unmarshal")
6168
}
6269
s.setDefaults()
70+
log.Printf("### - settings: %+v\n", s)
6371
return s, nil
6472
}
6573

pkg/plugins/action.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (i *Item) Action() ActionFunc {
4141
actions = append(actions, actionHref(debugf, i.Params.Href))
4242
}
4343
if i.Params.Shell != "" {
44-
actions = append(actions, actionShell(debugf, i, i.Params.Shell, i.Params.ShellParams, i.Plugin.Variables))
44+
actions = append(actions, actionShell(debugf, i, i.Plugin.AppleScriptTemplate, i.Params.Shell, i.Params.ShellParams, i.Plugin.Variables))
4545
}
4646
if i.Params.Refresh {
4747
shouldDelayBeforeRefresh := false
@@ -115,24 +115,15 @@ func actionHref(debugf DebugFunc, href string) ActionFunc {
115115
}
116116

117117
// actionShell gets an ActionFunc that runs a shell command.
118-
func actionShell(debugf DebugFunc, item *Item, command string, params, envVars []string) ActionFunc {
118+
func actionShell(debugf DebugFunc, item *Item, appleScriptTemplate, command string, params, envVars []string) ActionFunc {
119119
if item.Params.Terminal {
120-
return actionShellTerminal(debugf, item, command, params, envVars)
120+
return actionShellTerminal(debugf, item, appleScriptTemplate, command, params, envVars)
121121
}
122122
return func(ctx context.Context) {
123123
var commandExec string
124124
var commandArgs []string
125-
// if item.Params.Terminal {
126-
// shell := os.Getenv("SHELL")
127-
// if shell == "" {
128-
// shell = "/bin/bash"
129-
// }
130-
// commandExec = shell
131-
// commandArgs = append([]string{command}, params...)
132-
// } else {
133125
commandExec = command
134126
commandArgs = params
135-
//}
136127
debugf("exec: %s %s", commandExec, strings.Join(commandArgs, " "))
137128
cmd := exec.CommandContext(context.Background(), commandExec, commandArgs...)
138129
cmd.SysProcAttr = &syscall.SysProcAttr{
@@ -156,30 +147,17 @@ func actionShell(debugf DebugFunc, item *Item, command string, params, envVars [
156147
}
157148

158149
// actionShellTerminal runs shell commands where terminal=true.
159-
func actionShellTerminal(debugf DebugFunc, item *Item, command string, params, envVars []string) ActionFunc {
150+
func actionShellTerminal(debugf DebugFunc, item *Item, appleScriptTemplate, command string, params, envVars []string) ActionFunc {
160151
return func(ctx context.Context) {
161152
debugf("exec: RunInTerminal...")
162-
script := `
163-
activate application "Terminal"
164-
tell application "Terminal"
165-
if not (exists window 1) then reopen
166-
set quotedScriptName to quoted form of "{{ .Command }}"
167-
{{ if .Params }}
168-
set commandLine to {{ .Vars }} & " " & quotedScriptName & " " & {{ .Params }}
169-
{{ else }}
170-
set commandLine to {{ .Vars }} & " " & quotedScriptName
171-
{{ end }}
172-
do script commandLine
173-
end tell
174-
`
175153
command := strconv.Quote(command)
176154
command = command[1 : len(command)-1] // trim quotes off
177155
for i := range params {
178156
params[i] = strconv.Quote(params[i])
179157
params[i] = params[i][1 : len(params[i])-1] // trim quotes off
180158
}
181159
paramsStr := strconv.Quote(strings.Join(params, " "))
182-
err := item.Plugin.runInTerminal(script, command, paramsStr, envVars)
160+
err := item.Plugin.runInTerminal(appleScriptTemplate, command, paramsStr, envVars)
183161
if err != nil {
184162
debugf("exec: RunInTerminal: err=%s", err)
185163
return

pkg/plugins/item_params.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"strings"
66

77
"github.com/leaanthony/go-ansi-parser"
8-
98
"github.com/pkg/errors"
109
)
1110

@@ -76,6 +75,9 @@ type ItemParams struct {
7675
// Terminal indicates whether to run the shell command in a terminal or not.
7776
// Default is false.
7877
Terminal bool `json:"terminal"`
78+
// // appleScriptTemplate3 is the template for the AppleScript
79+
// // to run this action in a terminal.
80+
// AppleScriptTemplate string `json:"-"`
7981
// Refresh indicates whether clicking this item will cause the plugin
8082
// to refresh or not.
8183
Refresh bool `json:"refresh"`

pkg/plugins/plugin.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ type Plugin struct {
6969
// Called in TriggerRefresh() when updating the plugin menu to the
7070
// refreshing state, before refreshSignal is triggered.
7171
cycleSignal chan (struct{})
72+
73+
// appleScriptTemplate3 is the template for the AppleScript
74+
// to run this action in a terminal.
75+
AppleScriptTemplate string
7276
}
7377

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

282-
func (p *Plugin) runInTerminal(appleScriptTemplate2, command, paramsStr string, vars []string) error {
283-
tpl, err := template.New("appleScriptTemplate2").Parse(appleScriptTemplate2)
286+
func (p *Plugin) runInTerminal(appleScriptTemplate3, command, paramsStr string, vars []string) error {
287+
tpl, err := template.New("appleScriptTemplate3").Parse(appleScriptTemplate3)
284288
if err != nil {
285289
return err
286290
}
@@ -315,8 +319,8 @@ func (p *Plugin) runInTerminal(appleScriptTemplate2, command, paramsStr string,
315319

316320
// RunInTerminal runs this plugin in a terminal using the template
317321
// apple script.
318-
func (p *Plugin) RunInTerminal(appleScriptTemplate2 string) error {
319-
return p.runInTerminal(appleScriptTemplate2, p.Command, "", p.Variables)
322+
func (p *Plugin) RunInTerminal(appleScriptTemplate3 string) error {
323+
return p.runInTerminal(appleScriptTemplate3, p.Command, "", p.Variables)
320324
}
321325

322326
// refresh runs the plugin and parses the output, updating the

0 commit comments

Comments
 (0)