From 9cafb293d068bd737a567e2990d36722301508a3 Mon Sep 17 00:00:00 2001 From: 2mal3 <56305732+2mal3@users.noreply.github.com> Date: Sat, 29 Nov 2025 00:00:02 +0100 Subject: [PATCH] refactor(display): move open logic into own file --- display/pkg/file_handlers.go | 44 ++++++++++++++++++++++++++++++++++++ display/pkg/main.go | 35 ---------------------------- 2 files changed, 44 insertions(+), 35 deletions(-) create mode 100644 display/pkg/file_handlers.go diff --git a/display/pkg/file_handlers.go b/display/pkg/file_handlers.go new file mode 100644 index 0000000..ed0dba3 --- /dev/null +++ b/display/pkg/file_handlers.go @@ -0,0 +1,44 @@ +package pkg + +import ( + "errors" + "fmt" + "os" + "os/exec" + "plg-mudics/shared" +) + +var runningHelpProgram *exec.Cmd = nil + +func OpenPresentation(path string) error { + tempDirPath, err := os.MkdirTemp("", "plg-mudics-libreoffice-profile-") + if err != nil { + return fmt.Errorf("failed to create temporary profile directory: %w", err) + } + + runningHelpProgram = exec.Command("soffice", "--show", path, "--nologo", "--norestore", fmt.Sprintf("-env:UserInstallation=file:///%s", tempDirPath)) + result := shared.RunShellCommand(runningHelpProgram) + killedByParent := -1 + if result.ExitCode != 0 && result.ExitCode != killedByParent { + return errors.New(result.Stderr) + } + return nil +} + +func OpenPDF(path string) error { + runningHelpProgram = exec.Command("xreader", path, "--presentation") + result := shared.RunShellCommandNonBlocking(runningHelpProgram) + killedByParent := -1 + if result.ExitCode != 0 && result.ExitCode != killedByParent { + return fmt.Errorf("could not open pdf: %s (%d)", result.Stderr, result.ExitCode) + } + return nil +} + +func CloseRunningProgram() error { + if runningHelpProgram == nil { + return nil + } + err := runningHelpProgram.Process.Kill() + return err +} diff --git a/display/pkg/main.go b/display/pkg/main.go index f781e2a..99e4eec 100644 --- a/display/pkg/main.go +++ b/display/pkg/main.go @@ -47,33 +47,6 @@ func GetDeviceMac() (string, error) { return "", fmt.Errorf("no suitable MAC address found") } -var runningHelpProgram *exec.Cmd = nil - -func OpenPresentation(path string) error { - tempDirPath, err := os.MkdirTemp("", "plg-mudics-libreoffice-profile-") - if err != nil { - return fmt.Errorf("failed to create temporary profile directory: %w", err) - } - - runningHelpProgram = exec.Command("soffice", "--show", path, "--nologo", "--norestore", fmt.Sprintf("-env:UserInstallation=file:///%s", tempDirPath)) - result := shared.RunShellCommand(runningHelpProgram) - killedByParent := -1 - if result.ExitCode != 0 && result.ExitCode != killedByParent { - return errors.New(result.Stderr) - } - return nil -} - -func OpenPDF(path string) error { - runningHelpProgram = exec.Command("xreader", path, "--presentation") - result := shared.RunShellCommandNonBlocking(runningHelpProgram) - killedByParent := -1 - if result.ExitCode != 0 && result.ExitCode != killedByParent { - return fmt.Errorf("could not open pdf: %s (%d)", result.Stderr, result.ExitCode) - } - return nil -} - type KeyAction int const ( @@ -167,11 +140,3 @@ func ResolveStorageFilePath(pathParam string) (string, bool, error) { return fullPath, true, nil } - -func CloseRunningProgram() error { - if runningHelpProgram == nil { - return nil - } - err := runningHelpProgram.Process.Kill() - return err -}