Skip to content

Commit

Permalink
Make end of options separator conditional in preparation for other pr…
Browse files Browse the repository at this point in the history
…ograms
  • Loading branch information
paololazzari committed Oct 12, 2023
1 parent 46b8337 commit 4b6326a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
13 changes: 7 additions & 6 deletions cmd/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"runtime"

ui "github.com/paololazzari/play/src/ui"
program "github.com/paololazzari/play/src/util"
"github.com/spf13/cobra"
"golang.org/x/term"
)

const version = "0.1.0"
const version = "0.2.0"

func completionCommand() *cobra.Command {
return &cobra.Command{
Expand Down Expand Up @@ -47,23 +48,23 @@ var (
Use: "grep",
Short: `Play with grep`,
Run: func(cmd *cobra.Command, args []string) {
run("grep")
run(program.NewProgram("grep", true))
},
}

sedCmd = &cobra.Command{
Use: "sed",
Short: `Play with sed`,
Run: func(cmd *cobra.Command, args []string) {
run("sed")
run(program.NewProgram("sed", true))
},
}

awkCmd = &cobra.Command{
Use: "awk",
Short: `Play with awk`,
Run: func(cmd *cobra.Command, args []string) {
run("awk")
run(program.NewProgram("awk", true))
},
}
)
Expand All @@ -86,7 +87,7 @@ func validateProgramExists(program string) {
}
}

func run(command string) error {
func run(program program.Program) error {

var userInterface *ui.UI
input := ""
Expand All @@ -107,7 +108,7 @@ func run(command string) error {
}
}

userInterface = ui.NewUI(command, input)
userInterface = ui.NewUI(program.Name, program.RespectsEndOfOptions, input)
userInterface.InitUI()
userInterface.Run()
return nil
Expand Down
57 changes: 42 additions & 15 deletions src/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ const (
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
PaddingText *tview.TextView
EndArgumentsText *tview.TextView
FileOptionsStdin string
FileOptionsText *tview.TextView
FileOptionsTreeNode *tview.TreeNode
Expand Down Expand Up @@ -64,10 +65,15 @@ func optionsInput() *tview.InputField {
SetPlaceholderStyle(tcell.StyleDefault)
}

// Returns the TextView with the separator
// Returns the TextView for the options separator
func endOptionsText() *tview.TextView {
return tview.NewTextView().
SetText(" -- ").
SetTextColor(titleColor)
}

// Returns the TextView for the arguments separator
func endArgumentsText() *tview.TextView {
return tview.NewTextView().
SetTextColor(titleColor)
}

Expand Down Expand Up @@ -102,11 +108,6 @@ func closingQuoteText() *tview.TextView {
SetText("'")
}

// Returns the TextView used for padding
func paddingText() *tview.TextView {
return tview.NewTextView()
}

// Returns the TextView for the input files
func fileOptionsText() *tview.TextView {
return tview.NewTextView().
Expand Down Expand Up @@ -182,18 +183,19 @@ func getFileOptionsText(a *[]string) string {
}

// UI constructor
func NewUI(label string, stdin string) *UI {
func NewUI(program string, respectsEndOfOptions bool, stdin string) *UI {
ui := &UI{
App: tview.NewApplication(),
Label: label,
CommandText: commandText(label),
Label: program,
EndOfOptionsSeparator: respectsEndOfOptions,
CommandText: commandText(program),
OptionsInput: optionsInput(),
EndOptionsText: endOptionsText(),
OpeningQuoteText: openingQuoteText(),
ArgumentsInput: argumentsInput(),
ArgumentsInputWide: argumentsInputWide(),
ClosingQuoteText: closingQuoteText(),
PaddingText: paddingText(),
EndArgumentsText: endArgumentsText(),
FileOptionsStdin: stdin,
FileOptionsText: fileOptionsText(),
FileOptionsTreeNode: fileOptionsTreeNode(),
Expand All @@ -215,11 +217,18 @@ func (ui *UI) evaluateExpression() func() {
sb.WriteString(ui.Label)
sb.WriteString(" ")
sb.WriteString(ui.OptionsInput.GetText())
sb.WriteString(" -- ")
if ui.EndOfOptionsSeparator {
sb.WriteString(" -- ")
} else {
sb.WriteString(" ")
}
sb.WriteString(ui.OpeningQuoteText.GetText(false))
sb.WriteString(ui.ArgumentsInput.GetText())
sb.WriteString(ui.ClosingQuoteText.GetText(false))
sb.WriteString(" ")
if !ui.EndOfOptionsSeparator {
sb.WriteString(" -- ")
}
if t := ui.FileOptionsText.GetText(false); t != "<input files>" {
if len(ui.FileOptionsStdin) > 0 && len(ui.FileOptionsInputSlice) == 0 {
sb.WriteString("<<<")
Expand Down Expand Up @@ -461,18 +470,36 @@ func (ui *UI) configFileView() {
})
}

// Helper function
func (ui *UI) endOptionsSeparator() (*tview.TextView, int, int, bool) {
if ui.EndOfOptionsSeparator {
return ui.EndOptionsText.SetText(" -- "), 4, 1, false
} else {
return ui.EndOptionsText.SetText(" "), 1, 1, false
}
}

// Helper function
func (ui *UI) endArgumentsSeparator() (*tview.TextView, int, int, bool) {
if ui.EndOfOptionsSeparator {
return ui.EndArgumentsText.SetText(" "), 1, 1, false
} else {
return ui.EndArgumentsText.SetText(" -- "), 4, 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.EndOptionsText, 4, 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.PaddingText, 1, 1, false).
AddItem(ui.endArgumentsSeparator()).
AddItem(ui.FileOptionsText, 0, 1, false).
AddItem(tview.NewBox(), 2, 1, false), 3, 1, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexColumn).
Expand Down
15 changes: 15 additions & 0 deletions src/util/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ import (
"runtime"
)

// Program being used
type Program struct {
Name string
RespectsEndOfOptions bool
}

// Program constructor
func NewProgram(name string, respectsEndOfOptions bool) Program {
program := Program{
Name: name,
RespectsEndOfOptions: respectsEndOfOptions,
}
return program
}

// Execute the given command in either bash or powershell depending on the detected os
func shellout(command string, silent bool) (string, string, error) {
var stdout bytes.Buffer
Expand Down

0 comments on commit 4b6326a

Please sign in to comment.