writeline

package module
v0.0.0-...-269845d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

README

writeline

Simple Go lib for overwrite stdout lines. It maintains new lines and overwrites to them.

methods:

import "github.com/joexzh/writeline"

func main() {
    // new writeline creates and maintains 10 new lines.
    // It holds a default flush timer with 200 milliseconds duration   
    lw, err := writeline.New(10)
    
    // reset the flush timer duration
    lw.SetFlushDuration(100 * time.Millisecond)
    
    // Flush all changes immediately
    lw.Flush()
    
    // overwrite line 0
    lw.WriteLine(0, writeline.Style(writeline.Bold+writeline.Green, "hi"))
    
    // overwrite line 9
    lw.WriteLine(9, "Hello world")
    lw.Flush()
    
    // overwrite the last line
    lineNum, err := lw.WriteLastLine("hi")
    
    // create a new line at the bottom and write to it
    newLineNum, err := lw.WriteNewLine("foo")
    
    // total maintained lines
    lw.Lines() // 11
    
    // close writeline
    lw.Close()
}

Caution

Don't put any position control to the string. Such as \n, \033[K...

From New() through Close(), for all goroutines, don't use other write functions that effect the result of Stdout. Such as fmt.Print().

Documentation

Index

Constants

View Source
const (
	Bold      = "\033[1m"
	Dim       = "\033[2m"
	Underline = "\033[4m"
	Blink     = "\033[5m"
	// Reverse invert the foreground and background colors
	Reverse = "\033[7m"
	Hidden  = "\033[8m"

	ResetAllStyle = "\033[0m"

	Default      = "\033[39m"
	Black        = "\033[30m"
	Red          = "\033[31m"
	Green        = "\033[32m"
	Yellow       = "\033[33m"
	Blue         = "\033[34m"
	Magenta      = "\033[35m"
	Cyan         = "\033[36m"
	LightGray    = "\033[37m"
	DarkGray     = "\033[90m"
	LightRed     = "\033[91m"
	LightGreen   = "\033[92m"
	LightYellow  = "\033[93m"
	LightBlue    = "\033[94m"
	LightMagenta = "\033[95m"
	LightCyan    = "\033[96m"
	White        = "\033[97m"

	BgDefault      = "\033[49m"
	BgBlack        = "\033[40m"
	BgRed          = "\033[41m"
	BgGreen        = "\033[42m"
	BgYellow       = "\033[43m"
	BgBlue         = "\033[44m"
	BgMagenta      = "\033[45m"
	BgCyan         = "\033[46m"
	BgLightGray    = "\033[47m"
	BgDarkGray     = "\033[100m"
	BgLightRed     = "\033[101m"
	BgLightGreen   = "\033[102m"
	BgLightYellow  = "\033[103m"
	BgLightBlue    = "\033[104m"
	BgLightMagenta = "\033[105m"
	BgLightCyan    = "\033[106m"
	BgWhite        = "\033[107m"
)

Variables

This section is empty.

Functions

func Style

func Style(sType string, s string) string

Types

type LineWriter

type LineWriter struct {
	W *bufio.Writer
	// contains filtered or unexported fields
}

LineWriter maintains <n> new lines. Each line can be overwritten. Zero value is not ready for use.

func New

func New(lines int) (*LineWriter, error)

New creates a *LineWriter for os.Stdout, inits and maintains n lines. Also starts a flush timer with default duration of 200*time.Millisecond. Number of lines should > 0.

LineWriter.Close should be used after finished.

Note: This is a buffer method.

func (*LineWriter) Close

func (w *LineWriter) Close() error

Close end with a newline, stop timer, flush all remaining buffered string

func (*LineWriter) Flush

func (w *LineWriter) Flush() error

Flush all buffered string to the underlying io.Writer

func (*LineWriter) Lines

func (w *LineWriter) Lines() int

Lines returns total maintained lines

func (*LineWriter) SetFlushDuration

func (w *LineWriter) SetFlushDuration(d time.Duration)

func (*LineWriter) WriteLastLine

func (w *LineWriter) WriteLastLine(s string) (int, error)

WriteLastLine overwrites the last line. Returns the last line number.

Any position control string will lead to unexpected behavior. Such as \n, \033[K ...
Note: This is a buffer method.

func (*LineWriter) WriteLine

func (w *LineWriter) WriteLine(n int, s string) error

WriteLine overwrites the nth line.

Any position control string will lead to unexpected behavior. Such as \n, \033[K...
Note: This is a buffer method.

func (*LineWriter) WriteNewLine

func (w *LineWriter) WriteNewLine(s string) (int, error)

WriteNewLine Move cursor to new line at the bottom and write to it. Returns new line number.

Note: This is a buffer method.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL