From 2c5f017cab8fd503521b813f9a540329c5656a15 Mon Sep 17 00:00:00 2001 From: Matthias Rustler Date: Fri, 27 Dec 2024 11:45:13 +0100 Subject: [PATCH] enhance GUI to accept filename for file processing and display content --- cmd/iffmaster/main.go | 10 +++++----- internal/gui/gui.go | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/cmd/iffmaster/main.go b/cmd/iffmaster/main.go index 36a47ce..f9bae97 100644 --- a/cmd/iffmaster/main.go +++ b/cmd/iffmaster/main.go @@ -21,6 +21,8 @@ import ( ) func main() { + var filename string + showVersion := flag.Bool("version", false, "Display the version of iffmaster") flag.Parse() @@ -30,10 +32,8 @@ func main() { } if flag.NArg() > 0 { - //filename := flag.Arg(0) - //fmt.Println("Filename provided:", filename) - // You can add code here to handle the filename, e.g., open the file - } else { - gui.OpenGUI(Version) + filename = flag.Arg(0) } + + gui.OpenGUI(filename, Version) } diff --git a/internal/gui/gui.go b/internal/gui/gui.go index 38f5176..e115491 100644 --- a/internal/gui/gui.go +++ b/internal/gui/gui.go @@ -7,6 +7,8 @@ package gui import ( "bytes" "io" + "log" + "os" "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" @@ -36,8 +38,10 @@ type AppData struct { structTableView *widget.Table } -// OpenGUI layouts the main window and opens it -func OpenGUI(version string) { +// OpenGUI layouts the main window and opens it. +// If a filename is given, it reads the file and displays its content. +// The version string is used to display the version of the application. +func OpenGUI(filename string, version string) { var appData AppData var fileDlg *dialog.FileDialog @@ -110,5 +114,30 @@ func OpenGUI(version string) { appData.win.SetContent(appData.topContainer) appData.win.Resize(fyne.NewSize(800, 600)) + + readFileName(&appData, filename) + appData.win.ShowAndRun() } + +// readFileName reads the file with the given filename and displays its content. +// If the filename is empty, it does nothing. +func readFileName(appData *AppData, filename string) { + log.Print("readFileName: ", filename) + if filename != "" { + data, err := os.ReadFile(filename) + if err != nil { + dialog.ShowError(err, appData.win) + return + } + + appData.chunks, err = chunks.ReadIFFFile(bytes.NewReader(data), int64(len(data))) + if err != nil { + dialog.ShowError(err, appData.win) + return + } + + appData.nodeList = ConvertIFFChunkToListNode(appData.chunks) + appData.topContainer.Refresh() + } +}