-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code refactor and added the feature to grab the erorrs from other pro…
…gram
- Loading branch information
Showing
6 changed files
with
188 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package executor | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var PYTHON_EXEPECTED_ERRORS_REGEX = regexp.MustCompile(`KeyboardInterrupt|SystemExit|GeneratorExit`) | ||
|
||
func Execute() (string, string) { | ||
// Split the command into tokens | ||
commandTokens := strings.Split(Command, " ") | ||
// grab the executable name | ||
executable := commandTokens[0] | ||
cmd := exec.Command(executable, commandTokens[1:]...) | ||
var errorBuff bytes.Buffer | ||
// print the stdout | ||
cmd.Stdout = os.Stdout | ||
// Storing the error in buffer inf any | ||
cmd.Stderr = &errorBuff | ||
err := cmd.Start() | ||
// Exit if commands cannot be run | ||
if err != nil { | ||
log.Println("Please fix the following error") | ||
log.Fatal(err) | ||
} | ||
cmd.Wait() | ||
errorString := errorBuff.String() | ||
// If erorr string is not null then the command exited with the error | ||
if errorString != "" { | ||
fmt.Printf("\nError >>>>>>>>>>\n\n %s \n <<<<<<<<<< Error\n", errorString) | ||
// Depending on the executable return the error string | ||
switch executable { | ||
case "go": | ||
data := strings.Split(errorString, "\n")[0] | ||
if len(strings.Split(data, ": ")) > 1 { | ||
return strings.Join(strings.Split(data, ": ")[1:], " "), executable | ||
} | ||
case "python", "python3": | ||
// expected errors | ||
if !PYTHON_EXEPECTED_ERRORS_REGEX.MatchString(errorString) { | ||
data := strings.Split(errorString, "\n") | ||
return data[len(data)-2], executable | ||
} | ||
case "default": | ||
log.Println("Unrecogized executable") | ||
} | ||
} | ||
return "", "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package executor | ||
|
||
var Command string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package ui | ||
|
||
import ( | ||
"goaround/widgets" | ||
"log" | ||
|
||
"github.com/gdamore/tcell/v2" | ||
"github.com/rivo/tview" | ||
) | ||
|
||
var app = tview.NewApplication() | ||
|
||
// Initialize the loading widget | ||
func initLoading() *widgets.LoadingWD { | ||
lwd := widgets.NewLoadingWidget() | ||
lwd.SetTitle("[red]Please wait, Querying stack overflow api") | ||
lwd.SetBorder(true) | ||
lwd.SetBorderColor(tcell.ColorSnow) | ||
lwd.SetDynamicColors(true) | ||
return lwd | ||
} | ||
|
||
// Initialize the question widget | ||
func initQuestion() *widgets.QuestionWD { | ||
qwd := widgets.NewQuestionWidget() | ||
qwd.SetSelectedBackgroundColor(tcell.ColorDarkCyan) | ||
qwd.ShowSecondaryText(false) | ||
qwd.SetBorder(true) | ||
qwd.SetBorderColor(tcell.ColorSnow) | ||
return qwd | ||
} | ||
|
||
func InIt() { | ||
qwd := initQuestion() | ||
lwd := initLoading() | ||
errorHandler := func(err error) { | ||
app.Stop() | ||
log.Fatal(err) | ||
} | ||
qwd.SetSelectedFunc(func(a int, b, c string, d rune) { | ||
// When the questions are not loaded secondary text of the question will be set to error | ||
// in this case we simply want to return | ||
if c == "error" { | ||
return | ||
} | ||
doneChan := make(chan int) | ||
awd, err := widgets.NewAnswerWidget(qwd.GetSelectedQuestion(a)) | ||
if err != nil { | ||
errorHandler(err) | ||
} | ||
awd.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { | ||
if event.Key() == tcell.KeyBackspace2 || event.Key() == tcell.KeyBackspace { | ||
app.SetRoot(qwd.Render(), true) | ||
return nil | ||
} | ||
return event | ||
}) | ||
awd.SetWrap(true) | ||
awd.SetDynamicColors(true) | ||
awd.SetBorderColor(tcell.ColorSnow) | ||
awd.SetToggleHighlights(true) | ||
// call the go routine to populate the answers | ||
go awd.Populate(doneChan, errorHandler) | ||
go lwd.Load(app, func() { | ||
app.SetRoot(awd, true) | ||
}, doneChan) | ||
app.SetRoot(lwd, true) | ||
}) | ||
doneChan := make(chan int) | ||
// go the go routine to populate questions | ||
go qwd.Populate(doneChan, errorHandler) | ||
go lwd.Load(app, func() { | ||
app.SetRoot(qwd.Render(), true) | ||
}, doneChan) | ||
if err := app.SetRoot(lwd, true).EnableMouse(false).Run(); err != nil { | ||
panic(err) | ||
} | ||
} |