Skip to content

Commit

Permalink
feat: use \n to print
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Yu <[email protected]>
  • Loading branch information
Yu-Jack committed Nov 28, 2024
1 parent 918d5a7 commit 0167253
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 53 deletions.
16 changes: 1 addition & 15 deletions package/harvester-os/files/usr/sbin/harv-install
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,7 @@ get_url()
attempts=5
until [ "$n" -ge "$attempts" ]
do
# Because `curl` command uses `\r` to overwrite the progress bar,
# so we should also consider `\r` when splitting the output.
# Hence, we use `startcurl` and `endcurl` to trigger replacing the progress bar.
# But, when we use `\r` to display, the header will be overwritten, so we need to print the header manually.
echo "% Total % Received % Xferd Average Speed Time Time Time Current"
echo " Dload Upload Total Spent Left Speed"
echo "startcurl"
curl -o $TO -fL ${FROM} 2>&1
curl_result=$?
echo "endcurl"

if [ "$curl_result" -eq 0 ]; then
break
fi

curl -o $TO -fL ${FROM} && break
n=$((n+1))
echo "Failed to download, retry attempt ${n} out of ${attempts}"
sleep 2
Expand Down
39 changes: 1 addition & 38 deletions pkg/console/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,26 +430,11 @@ func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
func printToPanelAndLog(g *gocui.Gui, panel string, logPrefix string, reader io.Reader, lock *sync.Mutex) {
scanner := bufio.NewScanner(reader)
scanner.Split(ScanLines)
currentPrinter := printToPanel

for scanner.Scan() {
logrus.Infof("%s: %s", logPrefix, scanner.Text())
lock.Lock()
text := scanner.Text()

if strings.Contains(text, "startcurl") {
currentPrinter = printCurlProgressBarToPanel
lock.Unlock()
continue
}
if strings.Contains(text, "endcurl") {
currentPrinter = printToPanel
printToPanel(g, " ", panel)
lock.Unlock()
continue
}

currentPrinter(g, text, panel)
printToPanel(g, scanner.Text(), panel)
lock.Unlock()
}
}
Expand Down Expand Up @@ -661,28 +646,6 @@ func printToPanel(g *gocui.Gui, message string, panelName string) {
<-ch
}

func printCurlProgressBarToPanel(g *gocui.Gui, message string, panelName string) {
// block printToPanel call in the same goroutine.
// This ensures messages are printed out in the calling order.
ch := make(chan struct{})

g.Update(func(g *gocui.Gui) error {

defer func() {
ch <- struct{}{}
}()

v, err := g.View(panelName)
if err != nil {
return err
}
v.Write(append([]byte{'\r'}, []byte(message)...))
return nil
})

<-ch
}

func getRemoteConfig(configURL string) (*config.HarvesterConfig, error) {
client := newProxyClient()
b, err := getURL(client, configURL)
Expand Down

0 comments on commit 0167253

Please sign in to comment.