From 68ca8d686190d62b06e4b51d51541bd1263f0dd9 Mon Sep 17 00:00:00 2001 From: Tulip Blossom Date: Sat, 21 Dec 2024 00:55:25 -0300 Subject: [PATCH] chore: remove progress bar and dependencies --- cmd/root.go | 17 ----- cmd/update.go | 40 +++--------- drv/brew/brew.go | 6 +- drv/distrobox/distrobox.go | 16 ++--- drv/flatpak/flatpak.go | 16 ++--- drv/generic/generic.go | 8 --- go.mod | 4 -- go.sum | 8 --- pkg/logging/userHandler.go | 8 ++- pkg/percent/colorpicker.go | 70 -------------------- pkg/percent/incrementer.go | 22 ++----- pkg/percent/incrementer_test.go | 13 +--- pkg/percent/progressmanager.go | 112 ++------------------------------ pkg/session/session.go | 9 +-- uupd.service | 2 +- 15 files changed, 55 insertions(+), 296 deletions(-) delete mode 100644 pkg/percent/colorpicker.go diff --git a/cmd/root.go b/cmd/root.go index f8e476d..5a7fa43 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -9,10 +9,8 @@ import ( "path" "path/filepath" - "github.com/jedib0t/go-pretty/v6/text" "github.com/spf13/cobra" appLogging "github.com/ublue-os/uupd/pkg/logging" - "golang.org/x/term" ) func assertRoot(cmd *cobra.Command, args []string) { @@ -118,19 +116,4 @@ func init() { rootCmd.PersistentFlags().StringVar(&fLogFile, "log-file", "-", "File where user-facing logs will be written to") rootCmd.PersistentFlags().StringVar(&fLogLevel, "log-level", "info", "Log level for user-facing logs") rootCmd.PersistentFlags().BoolVar(&fNoLogging, "quiet", false, "Make logs quiet") - - interactiveProgress := true - if fLogFile != "-" { - interactiveProgress = false - } - isTerminal := term.IsTerminal(int(os.Stdout.Fd())) - if !isTerminal { - interactiveProgress = false - } - if !text.ANSICodesSupported { - interactiveProgress = false - text.DisableColors() - } - - rootCmd.Flags().BoolP("no-progress", "p", !interactiveProgress, "Do not show progress bars") } diff --git a/cmd/update.go b/cmd/update.go index 055efad..2f22aa7 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -5,7 +5,6 @@ import ( "log/slog" "os" - "github.com/jedib0t/go-pretty/v6/progress" "github.com/spf13/cobra" "github.com/ublue-os/uupd/checks" "github.com/ublue-os/uupd/drv/brew" @@ -98,34 +97,15 @@ func Update(cmd *cobra.Command, args []string) { if mainSystemDriverConfig.Enabled { totalSteps += mainSystemDriver.Steps() } - pw := percent.NewProgressWriter() - pw.SetNumTrackersExpected(1) - pw.SetAutoStop(false) - progressEnabled, err := cmd.Flags().GetBool("no-progress") - if err != nil { - slog.Error("Failed to get no-progress flag", "error", err) - return - } - // Move this to its actual boolean value (~no-progress) - progressEnabled = !progressEnabled - - if progressEnabled { - go pw.Render() - percent.ResetOscProgress() - } + // FIXME: check if is interactive + percent.ResetOscProgress() // -1 because 0 index - tracker := percent.NewIncrementTracker(&progress.Tracker{Message: "Updating", Units: progress.UnitsDefault, Total: int64(totalSteps - 1)}, totalSteps-1) - pw.AppendTracker(tracker.Tracker) + tracker := &percent.Incrementer{MaxIncrements: totalSteps - 1} - var trackerConfig = &drv.TrackerConfiguration{ - Tracker: tracker, - Writer: &pw, - Progress: progressEnabled, - } - flatpakUpdater.Tracker = trackerConfig - distroboxUpdater.Tracker = trackerConfig + flatpakUpdater.Tracker = tracker + distroboxUpdater.Tracker = tracker var outputs = []drv.CommandOutput{} @@ -149,7 +129,7 @@ func Update(cmd *cobra.Command, args []string) { if mainSystemDriverConfig.Enabled { slog.Debug(fmt.Sprintf("%s module", mainSystemDriverConfig.Title), slog.String("module_name", mainSystemDriverConfig.Title), slog.Any("module_configuration", mainSystemDriverConfig)) - percent.ChangeTrackerMessageFancy(pw, tracker, progressEnabled, percent.TrackerMessage{Title: mainSystemDriverConfig.Title, Description: mainSystemDriverConfig.Description}) + percent.ReportStatusChange(tracker, percent.TrackerMessage{Title: mainSystemDriverConfig.Title, Description: mainSystemDriverConfig.Description}) var out *[]drv.CommandOutput out, err = mainSystemDriver.Update() outputs = append(outputs, *out...) @@ -158,7 +138,7 @@ func Update(cmd *cobra.Command, args []string) { if brewUpdater.Config.Enabled { slog.Debug(fmt.Sprintf("%s module", brewUpdater.Config.Title), slog.String("module_name", brewUpdater.Config.Title), slog.Any("module_configuration", brewUpdater.Config)) - percent.ChangeTrackerMessageFancy(pw, tracker, progressEnabled, percent.TrackerMessage{Title: brewUpdater.Config.Title, Description: brewUpdater.Config.Description}) + percent.ReportStatusChange(tracker, percent.TrackerMessage{Title: brewUpdater.Config.Title, Description: brewUpdater.Config.Description}) var out *[]drv.CommandOutput out, err = brewUpdater.Update() outputs = append(outputs, *out...) @@ -181,10 +161,8 @@ func Update(cmd *cobra.Command, args []string) { tracker.IncrementSection(err) } - if progressEnabled { - pw.Stop() - percent.ResetOscProgress() - } + // FIXME: detect interactive session + percent.ResetOscProgress() if verboseRun { slog.Info("Verbose run requested") diff --git a/drv/brew/brew.go b/drv/brew/brew.go index 1abf928..0fc0e93 100644 --- a/drv/brew/brew.go +++ b/drv/brew/brew.go @@ -89,9 +89,9 @@ func (up BrewUpdater) New(config UpdaterInitConfiguration) (BrewUpdater, error) up.Config.Logger = config.Logger.With(slog.String("module", strings.ToLower(up.Config.Title))) up.BrewPrefix = EnvOrFallback(up.Config.Environment, "HOMEBREW_PREFIX", "/home/linuxbrew/.linuxbrew") - up.BrewPrefix = EnvOrFallback(up.Config.Environment, "HOMEBREW_REPOSITORY", fmt.Sprintf("%s/Homebrew", up.BrewPrefix)) - up.BrewPrefix = EnvOrFallback(up.Config.Environment, "HOMEBREW_CELLAR", fmt.Sprintf("%s/Cellar", up.BrewPrefix)) - up.BrewPrefix = EnvOrFallback(up.Config.Environment, "HOMEBREW_PATH", fmt.Sprintf("%s/bin/brew", up.BrewPrefix)) + up.BrewRepo = EnvOrFallback(up.Config.Environment, "HOMEBREW_REPOSITORY", fmt.Sprintf("%s/Homebrew", up.BrewPrefix)) + up.BrewCellar = EnvOrFallback(up.Config.Environment, "HOMEBREW_CELLAR", fmt.Sprintf("%s/Cellar", up.BrewPrefix)) + up.BrewPath = EnvOrFallback(up.Config.Environment, "HOMEBREW_PATH", fmt.Sprintf("%s/bin/brew", up.BrewPrefix)) if up.Config.DryRun { return up, nil diff --git a/drv/distrobox/distrobox.go b/drv/distrobox/distrobox.go index 85818ed..70a99fa 100644 --- a/drv/distrobox/distrobox.go +++ b/drv/distrobox/distrobox.go @@ -11,7 +11,7 @@ import ( type DistroboxUpdater struct { Config DriverConfiguration - Tracker *TrackerConfiguration + Tracker *percent.Incrementer binaryPath string users []session.User usersEnabled bool @@ -61,18 +61,18 @@ func (up DistroboxUpdater) Update() (*[]CommandOutput, error) { var finalOutput = []CommandOutput{} if up.Config.DryRun { - percent.ChangeTrackerMessageFancy(*up.Tracker.Writer, up.Tracker.Tracker, up.Tracker.Progress, percent.TrackerMessage{Title: up.Config.Title, Description: up.Config.Description}) - up.Tracker.Tracker.IncrementSection(nil) + percent.ReportStatusChange(up.Tracker, percent.TrackerMessage{Title: up.Config.Title, Description: up.Config.Description}) + up.Tracker.IncrementSection(nil) var err error = nil for _, user := range up.users { - up.Tracker.Tracker.IncrementSection(err) - percent.ChangeTrackerMessageFancy(*up.Tracker.Writer, up.Tracker.Tracker, up.Tracker.Progress, percent.TrackerMessage{Title: up.Config.Title, Description: *up.Config.UserDescription + " " + user.Name}) + up.Tracker.IncrementSection(err) + percent.ReportStatusChange(up.Tracker, percent.TrackerMessage{Title: up.Config.Title, Description: *up.Config.UserDescription + " " + user.Name}) } return &finalOutput, nil } - percent.ChangeTrackerMessageFancy(*up.Tracker.Writer, up.Tracker.Tracker, up.Tracker.Progress, percent.TrackerMessage{Title: up.Config.Title, Description: up.Config.Description}) + percent.ReportStatusChange(up.Tracker, percent.TrackerMessage{Title: up.Config.Title, Description: up.Config.Description}) cli := []string{up.binaryPath, "upgrade", "-a"} out, err := session.RunUID(up.Config.Logger, slog.LevelDebug, 0, cli, nil) tmpout := CommandOutput{}.New(out, err) @@ -83,9 +83,9 @@ func (up DistroboxUpdater) Update() (*[]CommandOutput, error) { err = nil for _, user := range up.users { - up.Tracker.Tracker.IncrementSection(err) + up.Tracker.IncrementSection(err) context := *up.Config.UserDescription + " " + user.Name - percent.ChangeTrackerMessageFancy(*up.Tracker.Writer, up.Tracker.Tracker, up.Tracker.Progress, percent.TrackerMessage{Title: up.Config.Title, Description: *up.Config.UserDescription + " " + user.Name}) + percent.ReportStatusChange(up.Tracker, percent.TrackerMessage{Title: up.Config.Title, Description: *up.Config.UserDescription + " " + user.Name}) cli := []string{up.binaryPath, "upgrade", "-a"} out, err := session.RunUID(up.Config.Logger, slog.LevelDebug, user.UID, cli, nil) tmpout = CommandOutput{}.New(out, err) diff --git a/drv/flatpak/flatpak.go b/drv/flatpak/flatpak.go index 3a99c56..7cbfd79 100644 --- a/drv/flatpak/flatpak.go +++ b/drv/flatpak/flatpak.go @@ -12,7 +12,7 @@ import ( type FlatpakUpdater struct { Config DriverConfiguration - Tracker *TrackerConfiguration + Tracker *percent.Incrementer binaryPath string users []session.User usersEnabled bool @@ -62,18 +62,18 @@ func (up FlatpakUpdater) Update() (*[]CommandOutput, error) { var finalOutput = []CommandOutput{} if up.Config.DryRun { - percent.ChangeTrackerMessageFancy(*up.Tracker.Writer, up.Tracker.Tracker, up.Tracker.Progress, percent.TrackerMessage{Title: up.Config.Title, Description: up.Config.Description}) - up.Tracker.Tracker.IncrementSection(nil) + percent.ReportStatusChange(up.Tracker, percent.TrackerMessage{Title: up.Config.Title, Description: up.Config.Description}) + up.Tracker.IncrementSection(nil) var err error = nil for _, user := range up.users { - up.Tracker.Tracker.IncrementSection(err) - percent.ChangeTrackerMessageFancy(*up.Tracker.Writer, up.Tracker.Tracker, up.Tracker.Progress, percent.TrackerMessage{Title: up.Config.Title, Description: *up.Config.UserDescription + " " + user.Name}) + up.Tracker.IncrementSection(err) + percent.ReportStatusChange(up.Tracker, percent.TrackerMessage{Title: up.Config.Title, Description: *up.Config.UserDescription + " " + user.Name}) } return &finalOutput, nil } - percent.ChangeTrackerMessageFancy(*up.Tracker.Writer, up.Tracker.Tracker, up.Tracker.Progress, percent.TrackerMessage{Title: up.Config.Title, Description: up.Config.Description}) + percent.ReportStatusChange(up.Tracker, percent.TrackerMessage{Title: up.Config.Title, Description: up.Config.Description}) cli := []string{up.binaryPath, "update", "-y", "--noninteractive"} flatpakCmd := exec.Command(cli[0], cli[1:]...) out, err := session.RunLog(up.Config.Logger, slog.LevelDebug, flatpakCmd) @@ -85,9 +85,9 @@ func (up FlatpakUpdater) Update() (*[]CommandOutput, error) { err = nil for _, user := range up.users { - up.Tracker.Tracker.IncrementSection(err) + up.Tracker.IncrementSection(err) context := *up.Config.UserDescription + " " + user.Name - percent.ChangeTrackerMessageFancy(*up.Tracker.Writer, up.Tracker.Tracker, up.Tracker.Progress, percent.TrackerMessage{Title: up.Config.Title, Description: context}) + percent.ReportStatusChange(up.Tracker, percent.TrackerMessage{Title: up.Config.Title, Description: context}) cli := []string{up.binaryPath, "update", "-y"} out, err := session.RunUID(up.Config.Logger, slog.LevelDebug, user.UID, cli, nil) tmpout = CommandOutput{}.New(out, err) diff --git a/drv/generic/generic.go b/drv/generic/generic.go index e885f48..512847c 100644 --- a/drv/generic/generic.go +++ b/drv/generic/generic.go @@ -5,8 +5,6 @@ import ( "os" "strings" - "github.com/jedib0t/go-pretty/v6/progress" - "github.com/ublue-os/uupd/pkg/percent" "github.com/ublue-os/uupd/pkg/session" ) @@ -74,12 +72,6 @@ type DriverConfiguration struct { UserDescription *string } -type TrackerConfiguration struct { - Tracker *percent.IncrementTracker - Writer *progress.Writer - Progress bool -} - type UpdateDriver interface { Steps() int Check() (bool, error) diff --git a/go.mod b/go.mod index cc7f8bf..0128381 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,8 @@ go 1.22.9 require ( github.com/godbus/dbus/v5 v5.1.0 - github.com/jedib0t/go-pretty/v6 v6.6.3 github.com/shirou/gopsutil/v4 v4.24.10 github.com/spf13/cobra v1.8.1 - golang.org/x/term v0.26.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -16,9 +14,7 @@ require ( github.com/go-ole/go-ole v1.2.6 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect - github.com/mattn/go-runewidth v0.0.15 // indirect github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect - github.com/rivo/uniseg v0.2.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect github.com/tklauser/numcpus v0.6.1 // indirect diff --git a/go.sum b/go.sum index e589ba7..a12d58d 100644 --- a/go.sum +++ b/go.sum @@ -12,18 +12,12 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= -github.com/jedib0t/go-pretty/v6 v6.6.3 h1:nGqgS0tgIO1Hto47HSaaK4ac/I/Bu7usmdD3qvs0WvM= -github.com/jedib0t/go-pretty/v6 v6.6.3/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= -github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= -github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw= github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= -github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= -github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shirou/gopsutil/v4 v4.24.10 h1:7VOzPtfw/5YDU+jLEoBwXwxJbQetULywoSV4RYY7HkM= github.com/shirou/gopsutil/v4 v4.24.10/go.mod h1:s4D/wg+ag4rG0WO7AiTj2BeYCRhym0vM7DHbZRxnIT8= @@ -45,8 +39,6 @@ golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU= -golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/pkg/logging/userHandler.go b/pkg/logging/userHandler.go index 1452a6b..e828218 100644 --- a/pkg/logging/userHandler.go +++ b/pkg/logging/userHandler.go @@ -96,7 +96,13 @@ func (h *UserHandler) Handle(ctx context.Context, r slog.Record) error { colorize(lightGray, r.Time.Format(timeFormat)), level, colorize(white, r.Message), - colorize(darkGray, "\n"+trimmedBytes), + colorize(darkGray, + func() string { + if len(trimmedBytes) > 0 && trimmedBytes != "{}" { + return colorize(darkGray, "\n"+trimmedBytes) + } + return "" + }()), ) return nil diff --git a/pkg/percent/colorpicker.go b/pkg/percent/colorpicker.go deleted file mode 100644 index 32043e4..0000000 --- a/pkg/percent/colorpicker.go +++ /dev/null @@ -1,70 +0,0 @@ -package percent - -import ( - "math" - - . "github.com/jedib0t/go-pretty/v6/text" -) - -// Accent color portal return as of xdg-desktop-portal-gnome 47.1 -type Accent struct { - Type string `json:"type"` - Data []struct { - Type string `json:"type"` - Data [3]float64 `json:"data"` - } `json:"data"` -} - -// Colors taken straight from GNOME 47 accent colors using this command: -// busctl --user call org.freedesktop.portal.Desktop /org/freedesktop/portal/desktop org.freedesktop.portal.Settings ReadOne 'ss' 'org.freedesktop.appearance' 'accent-color' -// This is as close as we can map the colors as possible afaik - Pink and Magenta DO look a like, and thats kind of a problem -var colorMap = map[Color][3]float64{ - FgHiBlack: {0, 0, 0}, - FgHiBlue: {0.207843, 0.517647, 0.894118}, - FgHiCyan: {0.129412, 0.564706, 0.643137}, - FgHiGreen: {0.227451, 0.580392, 0.290196}, - FgHiYellow: {0.784314, 0.533333, 0}, - FgHiRed: {0.901961, 0.176471, 0.258824}, - FgHiMagenta: {0.568627, 0.254902, 0.67451}, - FgHiWhite: {0.435294, 0.513726, 0.588235}, -} - -// Calculates the Euclidean distance between two colors -func colorDistance(c1, c2 [3]float64) float64 { - return math.Sqrt( - math.Pow(c1[0]-c2[0], 2) + - math.Pow(c1[1]-c2[1], 2) + - math.Pow(c1[2]-c2[2], 2), - ) -} - -func findClosestColor(rgb [3]float64) (Color, Color) { - var closestColor Color - minDistance := math.MaxFloat64 - - for color, predefinedRGB := range colorMap { - distance := colorDistance(rgb, predefinedRGB) - if distance < minDistance { - minDistance = distance - closestColor = color - } - } - - nonHiColor, isHiColor := hiToNonHiMap[closestColor] - if isHiColor { - return closestColor, nonHiColor - } - - return closestColor, closestColor -} - -var hiToNonHiMap = map[Color]Color{ - FgHiBlack: FgBlack, - FgHiRed: FgRed, - FgHiGreen: FgGreen, - FgHiYellow: FgYellow, - FgHiBlue: FgBlue, - FgHiMagenta: FgMagenta, - FgHiCyan: FgCyan, - FgHiWhite: FgWhite, -} diff --git a/pkg/percent/incrementer.go b/pkg/percent/incrementer.go index ebbf36f..725c7d4 100644 --- a/pkg/percent/incrementer.go +++ b/pkg/percent/incrementer.go @@ -1,29 +1,17 @@ package percent -import "github.com/jedib0t/go-pretty/v6/progress" - type Incrementer struct { DoneIncrements int MaxIncrements int } -type IncrementTracker struct { - Tracker *progress.Tracker - Incrementer *Incrementer -} - -func (it *IncrementTracker) IncrementSection(err error) { - if int64(it.Incrementer.DoneIncrements)+int64(1) > int64(it.Incrementer.MaxIncrements) { +func (it *Incrementer) IncrementSection(err error) { + if int64(it.DoneIncrements)+int64(1) > int64(it.MaxIncrements) { return } - it.Incrementer.DoneIncrements += 1 - if err == nil { - it.Tracker.Increment(1) - } else { - it.Tracker.IncrementWithError(1) - } + it.DoneIncrements += 1 } -func (it *IncrementTracker) CurrentStep() int { - return it.Incrementer.DoneIncrements +func (it *Incrementer) CurrentStep() int { + return it.DoneIncrements } diff --git a/pkg/percent/incrementer_test.go b/pkg/percent/incrementer_test.go index 954f430..0d1422b 100644 --- a/pkg/percent/incrementer_test.go +++ b/pkg/percent/incrementer_test.go @@ -4,20 +4,11 @@ import ( "math" "testing" - "github.com/jedib0t/go-pretty/v6/progress" "github.com/ublue-os/uupd/pkg/percent" ) -func InitIncrementer(max int) percent.IncrementTracker { - tracker := progress.Tracker{Message: "Updating", Units: progress.UnitsDefault, Total: int64(max)} - incrementer := percent.Incrementer{ - MaxIncrements: max, - DoneIncrements: 0, - } - return percent.IncrementTracker{ - Tracker: &tracker, - Incrementer: &incrementer, - } +func InitIncrementer(max int) percent.Incrementer { + return percent.Incrementer{MaxIncrements: max} } func TestOverflow(t *testing.T) { diff --git a/pkg/percent/progressmanager.go b/pkg/percent/progressmanager.go index ab945a1..a14f26a 100644 --- a/pkg/percent/progressmanager.go +++ b/pkg/percent/progressmanager.go @@ -1,119 +1,21 @@ package percent import ( - "encoding/json" - "fmt" "log/slog" - "math" - "os" - "strconv" - "time" - - "github.com/jedib0t/go-pretty/v6/progress" - "github.com/jedib0t/go-pretty/v6/text" - "github.com/ublue-os/uupd/pkg/session" ) -var CuteColors = progress.StyleColors{ - Message: text.Colors{text.FgWhite}, - Error: text.Colors{text.FgRed}, - Percent: text.Colors{text.FgHiBlue}, - Pinned: text.Colors{text.BgHiBlack, text.FgWhite, text.Bold}, - Stats: text.Colors{text.FgHiBlack}, - Time: text.Colors{text.FgBlue}, - Tracker: text.Colors{text.FgHiBlue}, - Value: text.Colors{text.FgBlue}, - Speed: text.Colors{text.FgBlue}, -} - -func NewProgressWriter() progress.Writer { - pw := progress.NewWriter() - - pw.SetTrackerLength(25) - pw.Style().Visibility.TrackerOverall = true - pw.Style().Visibility.Time = true - pw.Style().Visibility.Tracker = true - pw.Style().Visibility.Value = true - pw.SetMessageLength(32) - pw.SetSortBy(progress.SortByPercentDsc) - pw.SetStyle(progress.StyleBlocks) - pw.SetTrackerPosition(progress.PositionRight) - pw.SetUpdateFrequency(time.Millisecond * 100) - pw.Style().Options.PercentFormat = "%4.1f%%" - - pw.Style().Colors = CuteColors - - var targetUser int - baseUser, exists := os.LookupEnv("SUDO_UID") - if !exists || baseUser == "" { - targetUser = 0 - } else { - var err error - targetUser, err = strconv.Atoi(baseUser) - if err != nil { - slog.Error("Failed parsing provided user as UID", slog.String("user_value", baseUser)) - return pw - } - } - - if targetUser != 0 { - var accentColorSet progress.StyleColors - // Get accent color: https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Settings.html - cli := []string{"busctl", "--user", "--json=short", "call", "org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop", "org.freedesktop.portal.Settings", "ReadOne", "ss", "org.freedesktop.appearance", "accent-color"} - out, err := session.RunUID(nil, slog.LevelDebug, targetUser, cli, nil) - if err != nil { - return pw - } - var accent Accent - err = json.Unmarshal(out, &accent) - if err != nil { - return pw - } - - raw_color := accent.Data[0].Data - - highlightColor, lowColor := findClosestColor(raw_color) - - validHighlightColor := text.Colors{highlightColor} - validLowColor := text.Colors{lowColor} - - accentColorSet.Percent = validHighlightColor - accentColorSet.Tracker = validHighlightColor - accentColorSet.Time = validLowColor - accentColorSet.Value = validLowColor - accentColorSet.Speed = validLowColor - pw.Style().Colors = accentColorSet - } - return pw -} - -func NewIncrementTracker(tracker *progress.Tracker, max_increments int) *IncrementTracker { - return &IncrementTracker{ - Tracker: tracker, - Incrementer: &Incrementer{MaxIncrements: max_increments}, - } -} - type TrackerMessage struct { Title string Description string } -func ChangeTrackerMessageFancy(writer progress.Writer, tracker *IncrementTracker, progress bool, message TrackerMessage) { - if !progress { - slog.Info("Updating", - slog.String("title", message.Title), - slog.String("description", message.Description), - slog.Int64("progress", tracker.Tracker.Value()), - slog.Int64("total", tracker.Tracker.Total), - ) - return - } - percentage := math.Round((float64(tracker.Tracker.Value()) / float64(tracker.Tracker.Total)) * 100) - fmt.Printf("\033]9;4;1;%d\a", int(percentage)) - finalMessage := fmt.Sprintf("Updating %s (%s)", message.Description, message.Title) - writer.SetMessageLength(len(finalMessage)) - tracker.Tracker.UpdateMessage(finalMessage) +func ReportStatusChange(tracker *Incrementer, message TrackerMessage) { + slog.Info("Updating", + slog.String("title", message.Title), + slog.String("description", message.Description), + slog.Int("progress", tracker.CurrentStep()), + slog.Int("total", tracker.MaxIncrements), + ) } func ResetOscProgress() { diff --git a/pkg/session/session.go b/pkg/session/session.go index 5f9bacc..12fe655 100644 --- a/pkg/session/session.go +++ b/pkg/session/session.go @@ -26,17 +26,18 @@ func RunLog(logger *slog.Logger, level slog.Level, command *exec.Cmd) ([]byte, e stdout, _ := command.StdoutPipe() stderr, _ := command.StderrPipe() multiReader := io.MultiReader(stdout, stderr) + actuallogger := slog.Default() - if err := command.Wait(); err != nil { - logger.Warn("Error occoured starting external command", slog.Any("error", err)) + if err := command.Start(); err != nil { + actuallogger.Warn("Error occoured starting external command", slog.Any("error", err)) } scanner := bufio.NewScanner(multiReader) scanner.Split(bufio.ScanLines) for scanner.Scan() { - logger.With(slog.Bool("subcommand", true)).Log(context.TODO(), level, scanner.Text()) + actuallogger.Log(context.TODO(), level, scanner.Text()) } if err := command.Wait(); err != nil { - logger.Warn("Error occoured while waiting for external command", slog.Any("error", err)) + actuallogger.Warn("Error occoured while waiting for external command", slog.Any("error", err)) } return scanner.Bytes(), scanner.Err() diff --git a/uupd.service b/uupd.service index 6bbf5a0..4f71287 100644 --- a/uupd.service +++ b/uupd.service @@ -3,4 +3,4 @@ Description=Universal Blue Update Oneshot Service [Service] Type=oneshot -ExecStart=/usr/bin/uupd -c +ExecStart=/usr/bin/uupd --log-level debug