Skip to content

Commit

Permalink
Enhance wide editor
Browse files Browse the repository at this point in the history
  • Loading branch information
paololazzari committed Nov 10, 2023
1 parent da148f2 commit 6e3c789
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 49 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ To exit the application printing the input expression to stdout, use `Ctrl+S`.
| Positional Arguments | `Tab` | Move focus to file picker |
| Positional Arguments | `Shift+Tab` | Move focus to command options |
| Positional Arguments | `Enter` | Move focus to output |
| Positional Arguments | `Ctrl+O` | Open wide editor/Close wide editor |
| Positional Arguments | `Ctrl+O` | Open wide editor |
| Wide Editor | `Esc` | Close wide editor |
| Wide Editor | `Ctrl+O` | Close wide editor |
| Wide Editor | `Ctrl+Enter` | Enter newline |
| Wide Editor | `Enter` | Move focus to output |
| File picker | `Tab` | Move focus to command options |
| File picker | `Shift+Tab` | Move focus to positional arguments options |
| File picker | `Ctrl+O` | Open selected file/Close selected file |
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.1"
const version = "0.3.2"

func completionCommand() *cobra.Command {
return &cobra.Command{
Expand Down
141 changes: 94 additions & 47 deletions src/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,29 @@ const (

// User interface
type UI struct {
App *tview.Application
Label string
EndOfOptionsSeparator bool
CommandText *tview.TextView
OptionsInput *tview.InputField
EndOptionsText *tview.TextView
OpeningQuoteText *tview.TextView
ArgumentsInput *tview.InputField
ArgumentsInputWide *tview.TextArea
ClosingQuoteText *tview.TextView
EndArgumentsText *tview.TextView
FileOptionsStdin string
FileOptionsText *tview.TextView
FileOptionsTreeNode *tview.TreeNode
FileOptionsTreeView *tview.TreeView
FileOptionsInputMap map[string]bool
FileOptionsInputSlice []string
OutputView *tview.TextView
FileView *tview.TextView
Flex *tview.Flex
ActiveInput **tview.InputField
App *tview.Application
Label string
EndOfOptionsSeparator bool
CommandText *tview.TextView
OptionsInput *tview.InputField
EndOptionsText *tview.TextView
OpeningQuoteText *tview.TextView
ArgumentsInput *tview.InputField
ArgumentsInputWide *tview.TextArea
ArgumentsInputWideFlex *tview.Flex
ClosingQuoteText *tview.TextView
EndArgumentsText *tview.TextView
FileOptionsStdin string
FileOptionsText *tview.TextView
FileOptionsTreeNode *tview.TreeNode
FileOptionsTreeView *tview.TreeView
FileOptionsInputMap map[string]bool
FileOptionsInputSlice []string
OutputView *tview.TextView
FileView *tview.TextView
Flex *tview.Flex
ActiveInput **tview.InputField
ActiveFlex **tview.Flex
}

type nodeReference struct {
Expand Down Expand Up @@ -118,6 +120,11 @@ func argumentsInputWide() *tview.TextArea {
return t
}

// Returns the Flex used for wide positional arguments
func argumentsInputWideFlex() *tview.Flex {
return tview.NewFlex()
}

// Returns the TextView for the closing single quote
func closingQuoteText() *tview.TextView {
return tview.NewTextView().
Expand Down Expand Up @@ -201,31 +208,42 @@ func getFileOptionsText(a *[]string) string {
// UI constructor
func NewUI(program string, respectsEndOfOptions bool, stdin string) *UI {
ui := &UI{
App: tview.NewApplication(),
Label: program,
EndOfOptionsSeparator: respectsEndOfOptions,
CommandText: commandText(program),
OptionsInput: optionsInput(),
EndOptionsText: endOptionsText(),
OpeningQuoteText: openingQuoteText(),
ArgumentsInput: argumentsInput(),
ArgumentsInputWide: argumentsInputWide(),
ClosingQuoteText: closingQuoteText(),
EndArgumentsText: endArgumentsText(),
FileOptionsStdin: stdin,
FileOptionsText: fileOptionsText(),
FileOptionsTreeNode: fileOptionsTreeNode(),
FileOptionsTreeView: fileOptionsTreeView(),
FileOptionsInputMap: make(map[string]bool),
FileOptionsInputSlice: []string{},
OutputView: outputView(),
FileView: fileView(),
Flex: flex(),
ActiveInput: nil,
App: tview.NewApplication(),
Label: program,
EndOfOptionsSeparator: respectsEndOfOptions,
CommandText: commandText(program),
OptionsInput: optionsInput(),
EndOptionsText: endOptionsText(),
OpeningQuoteText: openingQuoteText(),
ArgumentsInput: argumentsInput(),
ArgumentsInputWide: argumentsInputWide(),
ArgumentsInputWideFlex: argumentsInputWideFlex(),
ClosingQuoteText: closingQuoteText(),
EndArgumentsText: endArgumentsText(),
FileOptionsStdin: stdin,
FileOptionsText: fileOptionsText(),
FileOptionsTreeNode: fileOptionsTreeNode(),
FileOptionsTreeView: fileOptionsTreeView(),
FileOptionsInputMap: make(map[string]bool),
FileOptionsInputSlice: []string{},
OutputView: outputView(),
FileView: fileView(),
Flex: flex(),
ActiveInput: nil,
ActiveFlex: nil,
}
return ui
}

// Helper function for getting active input text
func (ui *UI) getActiveInputText() string {
if ui.ActiveFlex == &ui.Flex {
return ui.ArgumentsInput.GetText()
} else {
return ui.ArgumentsInputWide.GetText()
}
}

// Helper function for evaluating expressions
func (ui *UI) evaluateExpression() func() {
return func() {
Expand All @@ -239,7 +257,7 @@ func (ui *UI) evaluateExpression() func() {
sb.WriteString(" ")
}
sb.WriteString(ui.OpeningQuoteText.GetText(false))
sb.WriteString(ui.ArgumentsInput.GetText())
sb.WriteString(ui.getActiveInputText())
sb.WriteString(ui.ClosingQuoteText.GetText(false))
sb.WriteString(" ")
if !ui.EndOfOptionsSeparator {
Expand Down Expand Up @@ -362,8 +380,9 @@ func (ui *UI) configArgumentsInput() {
ui.ClosingQuoteText.SetText("'")
}
case tcell.KeyCtrlO:
ui.ArgumentsInputWide.SetText(ui.ArgumentsInput.GetText(), false)
ui.App.SetRoot(ui.ArgumentsInputWide, true).
ui.ArgumentsInputWide.SetText(ui.ArgumentsInput.GetText(), true)
ui.ActiveFlex = &ui.ArgumentsInputWideFlex
ui.App.SetRoot(ui.ArgumentsInputWideFlex, true).
SetFocus(ui.ArgumentsInputWide)
}
return event
Expand All @@ -377,22 +396,42 @@ func (ui *UI) configArgumentsInputWide() {

ui.ArgumentsInputWide.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
key := event.Key()
// on ctrl+enter return a new enter event
if event.Modifiers() == 2 && event.Rune() == 10 {
return tcell.NewEventKey(tcell.KeyEnter, 10, 0)
}
switch key {
case tcell.KeyCtrlO:
ui.ActiveFlex = &ui.Flex
ui.App.SetRoot(ui.Flex, true).
SetFocus(ui.ArgumentsInput)
ui.ArgumentsInput.SetText(ui.ArgumentsInputWide.GetText())
case tcell.KeyEsc:
ui.ActiveFlex = &ui.Flex
ui.App.SetRoot(ui.Flex, true).
SetFocus(ui.ArgumentsInput)
ui.ArgumentsInput.SetText(ui.ArgumentsInputWide.GetText())
case tcell.KeyEnter:
ui.App.SetFocus(ui.OutputView)
return nil
}
return event
})
ui.ArgumentsInputWide.SetBorder(true)
ui.ArgumentsInputWide.SetBorderColor(playBorderColor)
}

// Function for configuring ArgumentsInputWideFlex Flex
func (ui *UI) configArgumentsInputWideFlex() {
ui.ArgumentsInputWideFlex.SetDirection(tview.FlexRow).
AddItem(ui.ArgumentsInputWide, 0, 1, false).
AddItem(ui.OutputView, 0, 1, false)
ui.ArgumentsInputWideFlex.SetBorder(true)
ui.ArgumentsInputWideFlex.SetTitle(" play ")
ui.ArgumentsInputWideFlex.SetTitleColor(playTitleColor)
ui.ArgumentsInputWideFlex.SetBorderColor(playBorderColor)
}

// Function for configuring FileOptionsText TextView
func (ui *UI) configFileOptionsInput() {
ui.FileOptionsText.SetChangedFunc(ui.changedText())
Expand Down Expand Up @@ -492,7 +531,13 @@ func (ui *UI) configFileOptionsTreeView() {
func (ui *UI) configOutputView() {
ui.OutputView.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyEsc {
ui.App.SetFocus(*ui.ActiveInput)
if ui.ActiveFlex == &ui.Flex {
ui.App.SetRoot(ui.Flex, true)
ui.App.SetFocus(*ui.ActiveInput)
} else {
ui.App.SetRoot(ui.ArgumentsInputWideFlex, true)
ui.App.SetFocus(ui.ArgumentsInputWide)
}
}
return event
})
Expand Down Expand Up @@ -563,6 +608,7 @@ func (ui *UI) InitUI() error {
ui.configOptionsInput()
ui.configArgumentsInput()
ui.configArgumentsInputWide()
ui.configArgumentsInputWideFlex()
ui.configFileOptionsInput()
ui.configFileOptionsTreeNode()
ui.configFileOptionsTreeView()
Expand All @@ -584,7 +630,7 @@ func (ui *UI) InitUI() error {
sb.WriteString(ui.OptionsInput.GetText())
sb.WriteString(endOptionsSeparator.GetText(false))
sb.WriteString(ui.OpeningQuoteText.GetText(false))
sb.WriteString(ui.ArgumentsInput.GetText())
sb.WriteString(ui.getActiveInputText())
sb.WriteString(ui.ClosingQuoteText.GetText(false))
sb.WriteString(endArgumentsSeparator.GetText(false))
sb.WriteString(strings.Join(ui.FileOptionsInputSlice, " "))
Expand All @@ -595,6 +641,7 @@ func (ui *UI) InitUI() error {
return event
})

ui.ActiveFlex = &ui.Flex
ui.App.SetRoot(ui.Flex, true).
SetFocus(ui.OptionsInput)

Expand Down

0 comments on commit 6e3c789

Please sign in to comment.