refactor(display): move open logic into own file

This commit is contained in:
2025-11-29 00:00:02 +01:00
parent 78fde49329
commit 9cafb293d0
2 changed files with 44 additions and 35 deletions
+44
View File
@@ -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
}
-35
View File
@@ -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
}