plot

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2022 License: BSD-3-Clause Imports: 7 Imported by: 2

Documentation

Overview

Package plot providers simple plots using ascii and ANSI escape sequences.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func HorizontalBar

func HorizontalBar(f *sgr.Formatter, value float64, color sgr.Color) interface{}

HorizontalBar returns an object which will show a horizontal bar when printed using a function in package fmt. The parameter value must be in the range 0 to 1 inclusive, and sets the percentage of the bar that will be filled.

To set the total width of the bar, use the width specifier in the format string.

Example
package main

import (
	"fmt"

	"git.sr.ht/~rj/sgr"
	"git.sr.ht/~rj/sgr/plot"
)

func main() {
	// A SGR formatter that forces ANSI escape sequences.
	f := sgr.NewFormatterWithANSI()

	for i := 0; i <= 5; i++ {
		v := float64(i) * 0.2
		fmt.Printf("%#v\n",
			fmt.Sprintf("[%20f] %3.0f%%\n",
				plot.HorizontalBar(f, v, sgr.Green),
				v*100),
		)
	}

}
Output:

"[\x1b[32;42m\x1b[0m                    ]   0%\n"
"[\x1b[32;42m    \x1b[0m                ]  20%\n"
"[\x1b[32;42m        \x1b[0m            ]  40%\n"
"[\x1b[32;42m            \x1b[0m        ]  60%\n"
"[\x1b[32;42m                \x1b[0m    ]  80%\n"
"[\x1b[32;42m                    \x1b[0m] 100%\n"
Example (Bad_value)
package main

import (
	"fmt"

	"git.sr.ht/~rj/sgr"
	"git.sr.ht/~rj/sgr/plot"
)

func main() {
	// A SGR formatter that disables ANSI escape sequences.
	f := (*sgr.Formatter)(nil)

	// Use a value that is not 0 to 1 inclusive.
	// Precondition violation.
	value := 1.5

	fmt.Printf("[%20f] %3.0f%%\n",
		plot.HorizontalBar(f, value, sgr.Green),
		value*100)

}
Output:

[%!(BADVALUE)1.5] 150%
Example (No_ansi)
package main

import (
	"fmt"

	"git.sr.ht/~rj/sgr"
	"git.sr.ht/~rj/sgr/plot"
)

func main() {
	// A SGR formatter that disables ANSI escape sequences.
	f := (*sgr.Formatter)(nil)

	for i := 0; i <= 5; i++ {
		v := float64(i) * 0.2
		fmt.Printf("[%20f] %3.0f%%\n",
			plot.HorizontalBar(f, v, sgr.Green),
			v*100)
	}

}
Output:

[                    ]   0%
[####                ]  20%
[########            ]  40%
[############        ]  60%
[################    ]  80%
[####################] 100%

func OverrideHaveUTF8 added in v0.12.0

func OverrideHaveUTF8(v bool)

OverrideHaveUTF8 overrides the package's decision about whether the terminal supports unicode. Unicode support is checked by inspecting the environment variable LANG. Users should not typically call this function except for testing.

func ProgressBar added in v0.7.0

func ProgressBar(f *sgr.Formatter, value float64, fg, bg sgr.Color) interface{}

ProgressBar returns an object which will show a progress bar when printed using a function in package fmt. The parameter value must be in the range 0 to 1 inclusive, and sets the percentage of the bar that will be filled.

To set the total width of the bar, use the width specifier in the format string.

Example
package main

import (
	"fmt"

	"git.sr.ht/~rj/sgr"
	"git.sr.ht/~rj/sgr/plot"
)

func main() {
	// A SGR formatter that forces ANSI escape sequences.
	f := sgr.NewFormatterWithANSI()

	// Restrict characters to ASCII for this test.
	plot.OverrideHaveUTF8(false)

	// Show progress bars of increasing length.
	for i := 0; i <= 5; i++ {
		v := float64(i) * 0.2
		fmt.Printf("%#v\n",
			fmt.Sprintf("[%20f] %3.0f%%",
				plot.ProgressBar(f, v, sgr.Green, sgr.Default),
				v*100),
		)
	}

}
Output:

"[\x1b[32;49m                    \x1b[0m]   0%"
"[\x1b[32;49m===>                \x1b[0m]  20%"
"[\x1b[32;49m=======>            \x1b[0m]  40%"
"[\x1b[32;49m===========>        \x1b[0m]  60%"
"[\x1b[32;49m===============>    \x1b[0m]  80%"
"[\x1b[32;49m===================>\x1b[0m] 100%"
Example (No_ansi)
package main

import (
	"fmt"

	"git.sr.ht/~rj/sgr"
	"git.sr.ht/~rj/sgr/plot"
)

func main() {
	// A SGR formatter that disables ANSI escape sequences.
	f := (*sgr.Formatter)(nil)

	// Show progress bars of increasing length.
	for i := 0; i <= 5; i++ {
		v := float64(i) * 0.2
		fmt.Printf("[%20f] %3.0f%%\n",
			plot.ProgressBar(f, v, sgr.Green, sgr.Default),
			v*100)
	}

}
Output:

[                    ]   0%
[===>                ]  20%
[=======>            ]  40%
[===========>        ]  60%
[===============>    ]  80%
[===================>] 100%

Types

This section is empty.

Jump to

Keyboard shortcuts

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