diff --git a/README.md b/README.md index e31742a..7a1790e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ -# ESEF (ElasticSearch Explain Formatter) +# ESEF +*ElasticSearch Explain (API) Formatter* ## Build project locally ```shell script @@ -8,3 +9,6 @@ fyne package -os darwin # optional: -icon icon.png ``` + +## Credits +The app icon is from the [Material Icon Set](https://material.io/resources/icons/?search=tree&icon=account_tree&style=baseline). diff --git a/esef.go b/esef.go index 938725a..b7d959e 100644 --- a/esef.go +++ b/esef.go @@ -1,15 +1,15 @@ package main import ( - "fmt" - "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/widget" + + "./util" ) -const APP_TITLE = "ESEF v0.1-alpha" +const AppTitle = "ESEF v0.2" const CopiedOutputToClipboardMessageText = "Copied output to clipboard!" const InputLabelText = "Input" const OutputLabelText = "Output" @@ -31,7 +31,7 @@ func main() { func buildMainWindow() fyne.Window { application := app.New() - Window = application.NewWindow(APP_TITLE) + Window = application.NewWindow(AppTitle) Window.SetContent(container.NewBorder( nil, @@ -47,15 +47,17 @@ func buildMainWindow() fyne.Window { return Window } -func buildFormatArea() *fyne.Container { - return container.NewAdaptiveGrid( - 2, - widget.NewButton(SimpleFormatButtonText, func() { - handleSimpleFormatButtonClick() - }), - widget.NewButton(TreeFormatButtonText, func() { - handleTreeFormatButtonClick() - }), +func buildFormatArea() *widget.Card { + return widget.NewCard("Format", "", + container.NewAdaptiveGrid( + 2, + widget.NewButton(SimpleFormatButtonText, func() { + handleSimpleFormatButtonClick() + }), + widget.NewButton(TreeFormatButtonText, func() { + handleTreeFormatButtonClick() + }), + ), ) } @@ -75,35 +77,36 @@ func handleTreeFormatButtonClick() { OutputEntry.SetText(formatInput(true)) } -func buildOutputArea() *fyne.Container { +func buildOutputArea() *widget.Card { OutputEntry = widget.NewMultiLineEntry() OutputLabel = widget.NewLabel(OutputLabelText) OutputLabel.Alignment = fyne.TextAlignCenter - return container.NewBorder( - OutputLabel, - widget.NewButton(CopyButtonText, func() { - handleCopyOutputButtonClick() - }), - nil, - nil, - OutputEntry, + return widget.NewCard( + OutputLabelText, + "", + container.NewBorder( + nil, + widget.NewButton(CopyButtonText, func() { + handleCopyOutputButtonClick() + }), + nil, + nil, + OutputEntry, + ), ) } -func buildInputArea() *fyne.Container { +func buildInputArea() *widget.Card { InputEntry = widget.NewMultiLineEntry() - InputEntry.Text = GetExplainApiOutputExample() InputLabel = widget.NewLabel(InputLabelText) InputLabel.Alignment = fyne.TextAlignCenter - return container.NewBorder( - InputLabel, - nil, - nil, - nil, + return widget.NewCard( + InputLabelText, + "", InputEntry, ) } @@ -114,19 +117,22 @@ func handleCopyOutputButtonClick() { } func formatInput(useTreeFormat bool) string { - explainApiDocument := ExtractDataFromExplainAPI(InputEntry.Text) var formattedString string + explainApiDocument, err := util.ExtractDataFromExplainAPI(InputEntry.Text) + + if err != nil { + return err.Error() + } if useTreeFormat { - formattedString = FormatExplainApiDocument(explainApiDocument, useTreeFormat) + formattedString = util.FormatExplainApiDocument(explainApiDocument, useTreeFormat) } else { - formattedString = FormatExplainApiDocument(explainApiDocument, useTreeFormat) + formattedString = util.FormatExplainApiDocument(explainApiDocument, useTreeFormat) } - fmt.Println(formattedString) return formattedString } func sendOSNotification(message string) { - fyne.CurrentApp().SendNotification(fyne.NewNotification(APP_TITLE, message)) + fyne.CurrentApp().SendNotification(fyne.NewNotification(AppTitle, message)) } diff --git a/icon.png b/icon.png new file mode 100644 index 0000000..e43a2bc Binary files /dev/null and b/icon.png differ diff --git a/extractor.go b/util/extractor.go similarity index 62% rename from extractor.go rename to util/extractor.go index 41d3ec6..bc77b33 100644 --- a/extractor.go +++ b/util/extractor.go @@ -1,9 +1,12 @@ -package main +package util import ( "encoding/json" + "errors" ) +const ErrorMessage = "error during JSON Data extraction\n\nplease check your input string for any json errors" + type ExplainAPIDocument struct { Index string `json:"_index"` Type string `json:"_type"` @@ -18,18 +21,18 @@ type ExplainNode struct { Details []ExplainNode `json:"details"` } -func ExtractDataFromExplainAPI(explainAPIOutput string) ExplainAPIDocument { +func ExtractDataFromExplainAPI(explainAPIOutput string) (ExplainAPIDocument, error) { return extractDocumentFromJson(explainAPIOutput) } -func extractDocumentFromJson(inputJson string) ExplainAPIDocument { +func extractDocumentFromJson(inputJson string) (ExplainAPIDocument, error) { var explainAPIDocument ExplainAPIDocument byteData := []byte(inputJson) err := json.Unmarshal(byteData, &explainAPIDocument) if err != nil { - return ExplainAPIDocument{} + return ExplainAPIDocument{}, errors.New(ErrorMessage) } - return explainAPIDocument + return explainAPIDocument, nil } diff --git a/formatter.go b/util/formatter.go similarity index 93% rename from formatter.go rename to util/formatter.go index 9e8b4c4..ece1d0e 100644 --- a/formatter.go +++ b/util/formatter.go @@ -1,4 +1,4 @@ -package main +package util import ( "fmt" @@ -14,6 +14,7 @@ const LShape = "└─ " const EmptyStartString = "" const DocumentInfoHeaderFormat = "index: %s\ndocumentId: %s\nmatched: %t\nexplanation:\n%s" +const ExplainNoteFormat = "%s%s%f (%s)\n" func FormatExplainApiDocument(doc ExplainAPIDocument, useTreeFormat bool) string { var formattedExplanation string @@ -51,7 +52,7 @@ func formatExplainNodesToTreeFormat(previousIndentation string, isRootNode bool, isLastInTreeLevel := isLastInTreeLevel(i, numberOfNodes) lineSymbol := getLineSymbol(isLastInTreeLevel, isRootNode) - result += fmt.Sprintf("%s%s%f (%s)\n", previousIndentation, lineSymbol, node.Value, node.Description) + result += fmt.Sprintf(ExplainNoteFormat, previousIndentation, lineSymbol, node.Value, node.Description) if len(node.Details) > 0 { newIndentation := createNewIndentation(previousIndentation, isRootNode, isLastInTreeLevel)