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")
}

On Saturday, 19 September 2020 at 20:47:25 UTC+1 Alexander Mills wrote:

> 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?
>
>
>
> On Saturday, September 19, 2020 at 12:36:04 PM UTC-7 iko...@gmail.com 
> wrote:
>
>> Can you give some example code?
>>
>>   *Joop Kiefte* - Chat @ Spike 
>> <https://spikenow.com/r/a/?ref=spike-organic-signature&_ts=owiru>
>> [image: owiru]
>>
>> On September 19, 2020 at 19:29 GMT, Alex Mills <al...@channelmeter.com> 
>> wrote:
>>
>>
>> Yeah I tried all those ANSI codes, nothing seems to work :(
>>
>> On Sat, Sep 19, 2020 at 11:59 AM Joop Kiefte <iko...@gmail.com> wrote:
>>
>>> I tend to do that with ANSI terminal codes, there are pages that explain 
>>> all codes
>>>
>>>   *Joop Kiefte* - Chat @ Spike 
>>> <https://spikenow.com/r/a/?ref=spike-organic-signature&_ts=owh4j> [image: 
>>> owh4j]
>>>
>>> On September 19, 2020 at 18:44 GMT, Alex Mills <al...@channelmeter.com> 
>>> wrote:
>>>
>>>
>>> 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?
>>>
>>> -- 
>>> 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
>>>  
>>> <https://groups.google.com/d/msgid/golang-nuts/d52c998b-bc1e-46fa-901d-f3f992ef4918n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>>
>>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/370d5724-215b-4261-9181-7b368ec4a228n%40googlegroups.com.

Reply via email to