Rich command-line formater for vt100.
Go to file
xdrm-brackets 943e61119c upgrade hyperlink: can now CONTAIN and be interlaced with other syntaxes 2019-01-31 09:28:22 +01:00
cmd/clifmt add executable -help explanation 2019-01-27 16:40:21 +01:00
internal upgrade hyperlink: can now CONTAIN and be interlaced with other syntaxes 2019-01-31 09:28:22 +01:00
README.md minfix 2019-01-30 21:51:24 +01:00
printer.go add progress (Printpf) featuring :\n (1) channel & normal arguments\n (2) right padding to avoid overlaps\n 2019-01-29 18:55:42 +01:00
printer_test.go add progress (Printpf) featuring :\n (1) channel & normal arguments\n (2) right padding to avoid overlaps\n 2019-01-29 18:55:42 +01:00
progress.go fix progress rewrite : multiline vs. no newline at all 2019-01-30 21:32:06 +01:00
util.go add progress (Printpf) featuring :\n (1) channel & normal arguments\n (2) right padding to avoid overlaps\n 2019-01-29 18:55:42 +01:00

README.md

| extended terminal format |

Go version Go Report Card Go doc buddy branch

Simple utility written in go that extends the standard fmt.Sprintf and fmt.Printf functions. It allows you to set foreground/background color, bold, underlined and italic text.


I. How to use

1) Requirements

The package clifmt can be used as a go library or as an executable. In either case, you need :

  • any recent linux system (has not been tested over other OSes)
  • go installed (has not been tested under version 1.11)

2) Installation

Simply launch the following command in your favorite terminal

$ go get -u git.xdrm.io/go/clifmt

It will download the project sources into $GOPATH/src/git.xdrm.io/go/clifmt.

3) Usage

a) As a library

You must import the library into your program with

import "git.xdrm.io/go/clifmt"

Then, the following methods will be available

// Printf wraps the standard fmt.Printf() features but adds formatting capabilities
func Printf(fmt string, args ...interface{}) error
// Sprintf acts as 'Printf' but returns the string instead of printing it
func Sprintf(fmt string, args ...interface{}) (string, error)
// Printpf acts as 'Printf' but takes as arguments either standard fmt.Printf arguments, or channels that will update the output when provided with correct values.
func Printpf(fmt string, args ...interface{}) (error)

b) As an executable

You must run from your terminal

$ go get -u git.xdrm.io/go/clifmt/cmd/clifmt

The clifmt executable will be available in your $GOBIN directory.


II. Format syntax

1) Text style

The format has been designed with the markdown syntax in mind, but has some differences due to implementation issues and for stability.

The format is better described by the sample below than explanations :

// markdown-like
Printf("some normal text")
Printf("**some bold text**")
Printf("*some italic text*")
Printf("_some underline text_")
Printf("[link label](http://link_url)")

// colors
Printf("${red text}(red)")
Printf("${red text over blue background}(red:blue)")
Printf("${blue background text}(:blue)")

The code below will print the following result :

definition example result

Note: Color names (e.g. red, blue) can be replaced by their hexadecimal representation (e.g. #ff0000, #0000ff) or the short version (e.g. #f00, #00f).


III. Animations

The Printpf method allows you to pass channels among ordinary arguments. It allows you to animate the text you want to print each time you send data on a channel argument.

The example below shows a simple progress bar using markdown-like syntax, colors and animations :

package main

import (
	"git.xdrm.io/go/clifmt"
	"time"
)

func main() {
    // (1) animated values
	var (
		status   = make(chan interface{})
		color    = make(chan interface{})
		progress = make(chan interface{})
	)

    // (2) print your animated values
	go clifmt.Printpf("[${%s}(%s)] **%d**%%", status, color, progress)

    // (3) animate values
	status <- "download"
	color <- "red"
	for i := 0; i < 100; i++ {
		progress <- i
		time.Sleep(time.Millisecond * 200)
	}
	status <- "done"
	color <- "green"
}

The result is the following :

animation result


IV. Screenshots

Colorizing format example :

colorizing example

Markdown-like format example

markdown-like example


V. Incoming features

  • markdown-like formatting - bold, italic, underlined, (strike-through?)
  • global alignment - align text dynamically
  • progress-line - redrawing format to show, for instance an animated progress bar on the same line