How to clear current line in terminal

4,304 views
Skip to first unread message

Alex Mills

unread,
Sep 19, 2020, 2:44:55 PM9/19/20
to golang-nuts
Using Node.js with several projects I can use these:

const rl = require('readline');
process.stdout.write('current line')
rl.clearLine(process.stdout); 
rl.cursorTo(process.stdout, 0);

this will clear the current line in the terminal, so I can achieve something like a status line, but I cannot figure out how to do this with Golang, anyone know?

Joop Kiefte

unread,
Sep 19, 2020, 3:00:37 PM9/19/20
to al...@channelmeter.com, golan...@googlegroups.com
I tend to do that with ANSI terminal codes, there are pages that explain all codes
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/d52c998b-bc1e-46fa-901d-f3f992ef4918n%40googlegroups.com.

Alex Mills

unread,
Sep 19, 2020, 3:30:37 PM9/19/20
to Joop Kiefte, golan...@googlegroups.com
Yeah I tried all those ANSI codes, nothing seems to work :(

Joop Kiefte

unread,
Sep 19, 2020, 3:36:04 PM9/19/20
to al...@channelmeter.com, golan...@googlegroups.com
Can you give some example code?

owiru

Alexander Mills

unread,
Sep 19, 2020, 3:47:25 PM9/19/20
to golang-nuts
Yeah sure so it looks like:

clearCurrentLine()  // clears the previous status line
fmt.Println("foo")
writeStatusLine()   // writes new status line

where clearCurrentLine() is just like

func clearCurrentLine(){
//fmt.Printf("\033[0;") // clear current line
//fmt.Printf("\033[2K\r%d",0);
//fmt.Fprint(os.Stdout,"\033[y;0H")
//fmt.Fprint(os.Stdout, "\033[K")
//fmt.Print("\x1b[2k") // erase the current line
}

I tried all those ANSI codes, nothing quite worked?

Joop Kiefte

unread,
Sep 19, 2020, 4:05:01 PM9/19/20
to alexande...@gmail.com, golan...@googlegroups.com
package main

import "fmt"

func main() {
fmt.Println("Test 1")
fmt.Println("Test 2")
fmt.Print("\033[1A\033[K") //one up, remove line (should work after the newline of the Println)
}

Maybe this piece of code helps you?

owk41

Joop Kiefte

unread,
Sep 19, 2020, 4:07:43 PM9/19/20
to alexande...@gmail.com, golan...@googlegroups.com
func clearCurrentLine(){
     fmt.Print("\n\033[1A\033[K")
}

owk7j

Stephan Lukits

unread,
Nov 15, 2020, 5:00:47 PM11/15/20
to golan...@googlegroups.com
On 9/19/20 10:45 AM, Alex Mills wrote:
> [...]
> this will clear the current line in the terminal, so I can achieve
> something like a status line, but I cannot figure out how to do this
> with Golang, anyone know?

https://play.golang.org/p/B5ZShtSizEy

Stephen Illingworth

unread,
Nov 16, 2020, 11:33:44 AM11/16/20
to golang-nuts

This is how I would do it. Note that you must be careful not to insert newlines. If you do, terminal control becomes trickier.



package main

import "fmt"

// ClearLine is the CSI sequence to clear the entire of the current line.
const ClearLine = "\033[2K"

func main() {
    // print a string that is comparatively long
    fmt.Printf("hello world")

    // clear line
    fmt.Printf(ClearLine)

    // the line is cleared but the cursor is in the wrong place. the carriage
    // return moves the cursor to the beginning of the line.
    fmt.Printf("\r")

    // print a shorter string to show that the longer string has been cleared
    fmt.Printf("bye")

    // print a newline to move the cursor to next line of terminal
    fmt.Printf("\n")
Reply all
Reply to author
Forward
0 new messages