spark

package
v0.0.0-...-039c559 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2015 License: MIT Imports: 11 Imported by: 2

README

spark

Makes sparklines... animated sparklines!

example-sparklines

Usage

Create a SparkStream object:

sprk := spark.Spark(time.Millisecond * 60) // prints every 60ms
sprk.Start()

timeout := time.NewTicker(time.Second * 10)
for {
    select {
    case <-time.After(time.Millisecond * 20):
        val := float64(rand.Intn(10000000000))
        sprk.Add(val)

    case <-timeout.C:
        sprk.Stop() // stops printing
        return
    }
}

That's it! But you can also configure a few things:

sprk.Out = os.Stderr

Or change the units it prints

sprk.Units = spark.Bytes // print as {KB/MB/GB/TB}/s, i.e `100MB/s`
sprk.Units = "unicorns"  // will print as `unicorn/s`

Docs?

Godocs!

License

MIT license.

Documentation

Overview

Package spark prints real time data as sparklines in your terminal.

Index

Examples

Constants

View Source
const (
	// Bytes is a predefined unit type, will pretty print using humanized values.
	Bytes = "bytes"
)

Variables

This section is empty.

Functions

func Reader

func Reader(r io.Reader) io.Reader

Reader wraps the reads of r with a SparkStream. The stream will have Bytes units and refresh every 33ms.

It will stop printing when the reader returns an error.

func ReaderOut

func ReaderOut(r io.Reader, out *os.File) io.Reader

ReaderOut wraps the reads of r with a SparkStream. The stream will have Bytes units and refresh every 33ms.

It will stop printing when the reader returns an error.

func WriteSeeker

func WriteSeeker(ws io.WriteSeeker) (io.WriteSeeker, func())

WriteSeeker wraps the writes to w with a SparkStream. The stream will have Bytes units and refresh every 33ms.

It will stop printing when the writer returns an error.

func Writer

func Writer(w io.Writer) (io.Writer, func())

Writer wraps the writes to w with a SparkStream. The stream will have Bytes units and refresh every 33ms.

It will stop printing when the writer returns an error.

Types

type SparkStream

type SparkStream struct {
	Units string
	Out   *os.File
	// contains filtered or unexported fields
}

SparkStream prints sparklines for values it receives in real time. It scales up and down the visible window of values and prints the average values per second it has observed in recent history.

func Spark

func Spark(resolution time.Duration) *SparkStream

Spark creates a stream of sparklines that will print every lines bucketize with the given resolution.

By default, it prints to os.Stdout and is unitless.

Example (Random)
package main

import (
	"github.com/aybabtme/uniplot/spark"
	"log"
	"math/rand"
	"os"
	"time"
)

func main() {
	sprk := spark.Spark(time.Millisecond * 30)
	sprk.Out = os.Stderr
	sprk.Units = spark.Bytes
	sprk.Start()

	timeout := time.NewTicker(time.Second * 5)

loop:
	for {
		select {
		case <-time.After(time.Millisecond * 20):
			sprk.Add(float64(rand.Intn(10000000000)))
		case <-timeout.C:
			log.Printf("DONE!")
			break loop
		}
	}
	sprk.Stop()

}
Output:

Example (Saw)
package main

import (
	"github.com/aybabtme/uniplot/spark"
	"log"
	"os"
	"time"
)

func main() {
	sprk := spark.Spark(time.Millisecond * 60)
	sprk.Out = os.Stderr
	sprk.Units = spark.Bytes
	sprk.Start()

	timeout := time.NewTicker(time.Second * 15)

	x := 0.0
loop:
	for {
		select {
		case <-time.After(time.Millisecond * 60):
			x += 0.1
			sprk.Add(x)
			if x >= 1.0 {
				x = 0.0
			}
		case <-timeout.C:
			log.Printf("DONE!")
			break loop
		}
	}
	sprk.Stop()

}
Output:

Example (Sine)
package main

import (
	"github.com/aybabtme/uniplot/spark"
	"log"
	"math"
	"os"
	"time"
)

func main() {
	sprk := spark.Spark(time.Millisecond * 60)
	sprk.Out = os.Stderr
	sprk.Units = spark.Bytes
	sprk.Start()

	timeout := time.NewTicker(time.Second * 15)

	x := 0.0
loop:
	for {
		select {
		case <-time.After(time.Millisecond * 60):
			y := math.Sin(x)
			x += 0.1
			sprk.Add(y)
		case <-timeout.C:
			log.Printf("DONE!")
			break loop
		}
	}
	sprk.Stop()

}
Output:

func (*SparkStream) Add

func (s *SparkStream) Add(v float64)

Add puts the value in the current bucket of sparklines. The value will appear part of the next update of the spark stream.

func (*SparkStream) Start

func (s *SparkStream) Start()

Start starts the printing of sparklines.

func (*SparkStream) Stop

func (s *SparkStream) Stop()

Stop stops the printing of sparklines.

Directories

Path Synopsis
This module is a Terminal API for the Go Programming Language.
This module is a Terminal API for the Go Programming Language.

Jump to

Keyboard shortcuts

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