Skip to content

Commit

Permalink
Automatically resize arguments input based on text size
Browse files Browse the repository at this point in the history
  • Loading branch information
paololazzari committed Jan 24, 2024
1 parent b0519e2 commit 83e0482
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
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.2"
const version = "0.3.3"

func completionCommand() *cobra.Command {
return &cobra.Command{
Expand Down
56 changes: 45 additions & 11 deletions src/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type UI struct {
FileOptionsInputSlice []string
OutputView *tview.TextView
FileView *tview.TextView
ChildFlex *tview.Flex
Flex *tview.Flex
ActiveInput **tview.InputField
ActiveFlex **tview.Flex
Expand Down Expand Up @@ -174,6 +175,11 @@ func fileView() *tview.TextView {
return t
}

// Returns the Flex used for layout
func childFlex() *tview.Flex {
return tview.NewFlex()
}

// Returns the Flex used for layout
func flex() *tview.Flex {
return tview.NewFlex()
Expand Down Expand Up @@ -228,6 +234,7 @@ func NewUI(program string, respectsEndOfOptions bool, stdin string) *UI {
FileOptionsInputSlice: []string{},
OutputView: outputView(),
FileView: fileView(),
ChildFlex: childFlex(),
Flex: flex(),
ActiveInput: nil,
ActiveFlex: nil,
Expand Down Expand Up @@ -278,6 +285,18 @@ func (ui *UI) evaluateExpression() func() {
}
}

// Helper function to resize flex based on argument input size
func (ui *UI) resizeChildFlexIfNeeded() {
argumentsInputLength := len(ui.ArgumentsInput.GetText())
if argumentsInputLength >= 40 {
ui.ChildFlex.ResizeItem(ui.ArgumentsInput, 0, 3)
} else if argumentsInputLength > 19 && argumentsInputLength < 40 {
ui.ChildFlex.ResizeItem(ui.ArgumentsInput, 0, 1)
} else if argumentsInputLength <= 19 {
ui.ChildFlex.ResizeItem(ui.ArgumentsInput, 22, 1)
}
}

// Callback function for InputField
func (ui *UI) changedInputField() func(string) {
return func(text string) {
Expand Down Expand Up @@ -364,6 +383,13 @@ func (ui *UI) configArgumentsInput() {
switch key {
case tcell.KeyRune:
ui.OutputView.ScrollToBeginning()
ui.resizeChildFlexIfNeeded()
case tcell.KeyDelete:
ui.OutputView.ScrollToBeginning()
ui.resizeChildFlexIfNeeded()
case tcell.KeyBackspace2:
ui.OutputView.ScrollToBeginning()
ui.resizeChildFlexIfNeeded()
case tcell.KeyTab:
ui.App.SetFocus(ui.FileOptionsTreeView)
case tcell.KeyBacktab:
Expand Down Expand Up @@ -406,11 +432,13 @@ func (ui *UI) configArgumentsInputWide() {
ui.App.SetRoot(ui.Flex, true).
SetFocus(ui.ArgumentsInput)
ui.ArgumentsInput.SetText(ui.ArgumentsInputWide.GetText())
ui.resizeChildFlexIfNeeded()
case tcell.KeyEsc:
ui.ActiveFlex = &ui.Flex
ui.App.SetRoot(ui.Flex, true).
SetFocus(ui.ArgumentsInput)
ui.ArgumentsInput.SetText(ui.ArgumentsInputWide.GetText())
ui.resizeChildFlexIfNeeded()
case tcell.KeyEnter:
ui.App.SetFocus(ui.OutputView)
return nil
Expand Down Expand Up @@ -578,28 +606,33 @@ func (ui *UI) endArgumentsSeparator() (*tview.TextView, int, int, bool) {
}
}

// Function for configuring ChildFlex Flex
func (ui *UI) configChildFlex() {
ui.ChildFlex.SetDirection(tview.FlexColumn).
AddItem(ui.CommandText, len(ui.Label)+4, 1, false).
AddItem(ui.OptionsInput, 17, 1, false).
AddItem(ui.endOptionsSeparator()).
AddItem(ui.OpeningQuoteText, 1, 1, false).
AddItem(ui.ArgumentsInput, 22, 1, false).
AddItem(ui.ClosingQuoteText, 1, 1, false).
AddItem(ui.endArgumentsSeparator()).
AddItem(ui.FileOptionsText, 0, 1, false).
AddItem(tview.NewBox(), 2, 1, false)
}

// Function for configuring Flex Flex
func (ui *UI) configFlex() {

ui.Flex.AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(tview.NewBox(), 2, 1, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(ui.CommandText, len(ui.Label)+4, 1, false).
AddItem(ui.OptionsInput, 17, 1, false).
AddItem(ui.endOptionsSeparator()).
AddItem(ui.OpeningQuoteText, 1, 1, false).
AddItem(ui.ArgumentsInput, 22, 1, false).
AddItem(ui.ClosingQuoteText, 1, 1, false).
AddItem(ui.endArgumentsSeparator()).
AddItem(ui.FileOptionsText, 0, 1, false).
AddItem(tview.NewBox(), 2, 1, false), 3, 1, false).
AddItem(ui.ChildFlex, 3, 1, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(ui.OutputView, 0, 10, false).
AddItem(ui.FileOptionsTreeView, 0, 2, false), 0, 1, false), 0, 1, false)
ui.Flex.SetBorder(true)
ui.Flex.SetTitle(" play ")
ui.Flex.SetTitleColor(playTitleColor)
ui.Flex.SetBorderColor(playBorderColor)

}

// Initialize UI
Expand All @@ -614,6 +647,7 @@ func (ui *UI) InitUI() error {
ui.configFileOptionsTreeView()
ui.configOutputView()
ui.configFileView()
ui.configChildFlex()
ui.configFlex()

// on Ctrl+S shut down the application and print the expression to stdout
Expand Down

0 comments on commit 83e0482

Please sign in to comment.