From 0ea2ce3674bc191cfd8e43b3be837846cd0030c2 Mon Sep 17 00:00:00 2001 From: eNV25 Date: Tue, 17 Jan 2023 21:57:56 +0400 Subject: [PATCH] cmd/ncdu: fix screen corruption when logging Before this change if logs were not redirected, logging would corrupt the terminal screen. This commit stores the logs (max ~100 lines) in an array and print them when the program exits. --- cmd/ncdu/ncdu.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/cmd/ncdu/ncdu.go b/cmd/ncdu/ncdu.go index 6fdb210a5..be91907a3 100644 --- a/cmd/ncdu/ncdu.go +++ b/cmd/ncdu/ncdu.go @@ -19,6 +19,7 @@ import ( "github.com/rclone/rclone/cmd/ncdu/scan" "github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs/fspath" + "github.com/rclone/rclone/fs/log" "github.com/rclone/rclone/fs/operations" "github.com/rivo/uniseg" "github.com/spf13/cobra" @@ -911,6 +912,28 @@ func (u *UI) Show() error { if err != nil { return fmt.Errorf("screen init: %w", err) } + + // Hijack fs.LogPrint so that it doesn't corrupt the screen. + if logPrint := fs.LogPrint; !log.Redirected() { + type log struct { + text string + level fs.LogLevel + } + var logs []log + fs.LogPrint = func(level fs.LogLevel, text string) { + if len(logs) > 100 { + logs = logs[len(logs)-100:] + } + logs = append(logs, log{level: level, text: text}) + } + defer func() { + fs.LogPrint = logPrint + for i := range logs { + logPrint(logs[i].level, logs[i].text) + } + }() + } + defer u.s.Fini() // scan the disk in the background