Skip to content

Commit

Permalink
Add Ctrl+S key bind for closing application printing the input expres…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
paololazzari committed Oct 27, 2023
1 parent 00b82ac commit be8a802
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ If you want to use `play` in read-only mode, thus avoding any file changes (such
$ docker run --rm -it -v "$(pwd)":/play:ro plazzari/play:latest <program>
```

To exit the application, use `Ctrl+C`.

To exit the application printing the input expression to stdout, use `Ctrl+S`.

## Key bindings

| Component | Key | Description |
|-----------------|---------------|-------------|
| Any | `Ctrl+C` | Exit application |
| Any | `Ctrl+S` | Exit application and print input expression to stdout |
| Command Options | `Tab` | Move focus to positional arguments |
| Command Options | `Shift+Tab` | Move focus to file picker |
| Command Options | `Enter` | Move focus to output |
Expand Down
2 changes: 1 addition & 1 deletion cmd/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"golang.org/x/term"
)

const version = "0.3.0"
const version = "0.3.1"

func completionCommand() *cobra.Command {
return &cobra.Command{
Expand Down
26 changes: 26 additions & 0 deletions src/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ui

import (
"bufio"
"fmt"

"io/ioutil"
"os"
Expand Down Expand Up @@ -569,6 +570,31 @@ func (ui *UI) InitUI() error {
ui.configFileView()
ui.configFlex()

// on Ctrl+S shut down the application and print the expression to stdout
ui.App.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
key := event.Key()
switch key {
case tcell.KeyCtrlS:
endOptionsSeparator, _, _, _ := ui.endOptionsSeparator()
endArgumentsSeparator, _, _, _ := ui.endArgumentsSeparator()

var sb strings.Builder
sb.WriteString(ui.Label)
sb.WriteString(" ")
sb.WriteString(ui.OptionsInput.GetText())
sb.WriteString(endOptionsSeparator.GetText(false))
sb.WriteString(ui.OpeningQuoteText.GetText(false))
sb.WriteString(ui.ArgumentsInput.GetText())
sb.WriteString(ui.ClosingQuoteText.GetText(false))
sb.WriteString(endArgumentsSeparator.GetText(false))
sb.WriteString(strings.Join(ui.FileOptionsInputSlice, " "))

ui.App.Stop()
fmt.Println(sb.String())
}
return event
})

ui.App.SetRoot(ui.Flex, true).
SetFocus(ui.OptionsInput)

Expand Down

0 comments on commit be8a802

Please sign in to comment.