Categorygithub.com/lianggaoqiang/single-line-print
repositorypackage
1.1.1
Repository: https://github.com/lianggaoqiang/single-line-print.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# README

single-line-print https://github.com/lianggaoqiang/single-line-print/blob/main/LICENSE

Single-Line-Print is a cross-platform terminal single line printing program implemented with Go.

It features the following benefits:

  • Ease of use and pretty good cross-platform performance (Linux, Windows, MacOS and so on).
  • Custom output and run mode. With this ability, you can disable input during the printing process, hide cursor, and even maintain stable output when dynamically modifying the terminal window.
  • Available two styles: Simple Printer and Writer. With this ability, you can print directly with Printer, or you can use buffering function of Writer to process extra long text.

Usage

import slp "github.com/lianggaoqiang/single-line-print"

func main() {
	p := slp.NewPrinter()
	for i := 0; i <= 100; i++ {
		p.Print(fmt.Sprintf("> Downloading from remote: %d%%\n\n", i))
		time.Sleep(time.Millisecond * 30)
	}
	p.Stop()
}


Why need p.Stop?

  1. When initializing with either NewPrinter or NewWriter, the resulting instance will default to disabling terminal input and hiding the cursor during the printing process.To change this behavior, see Use Flag
    - After printing is completed, if you want to restore terminal input and redisplay the cursor, you can call Stop to achieve this goal.

  2. There can only be one instance of printer or writer globally, and before creating another instance, the existing instance must be registered using Stop to avoid the program from crashing and throwing an error.


Use Writer

If you want to utilize it as a writer, you can obtain an instance that implements io.Writer through NewWriter.

import slp "github.com/lianggaoqiang/single-line-print"

func main() {
	w := slp.NewWriter()
	bw := bufio.NewWriter(w)
	for i := 0; i <= 100; i++ {
		s := fmt.Sprintf("now the number is %d!\n", i)
		bw.Write([]byte(s))
		bw.Flush()
		time.Sleep(time.Millisecond * 10)
	}
	w.Stop()
}

Use Flag

NewPrinter and NewWriter default to disable terminal input and hide the cursor. If you want to change this behavior, you can use NewPrinterWithFlag or NewWriterWithFlag instead. There are three flag bits available, and their functions are as follows:

  • HideCursor: if set, the cursor will be hidden during printing or writing.
  • DisableInput: if set, input will be disabled during printing or writing.
  • ResizeReactively: if set, terminal window size will be got before each printing or writing. This will ensure more stable terminal output when the window is resized dynamically during printing.
import slp "github.com/lianggaoqiang/single-line-print"

func main() {
	p := slp.NewPrinterWithFlag(
		slp.HideCursor | slp.DisableInput | slp.ResizeReactively,
	)
	// do some prints
	p.Stop()
}

FAQ

  1. Previous output contents was accidentally cleared?
    If you print some contents between two single line prints without using Stop, this may happen. To avoid this situation, you can use Reload before reusing an existing instance.

Installation

go get github.com/lianggaoqiang/single-line-print