progress

package module
v0.0.0-...-68be126 Latest Latest
Warning

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

Go to latest
Published: May 15, 2019 License: MIT Imports: 5 Imported by: 2

README

Progress

GoDoc

About

Progress is a go library for creating a live progress bar into slack. Inspiration came from slack-progress.

Demo

Slack Progress Bar

Example

token := "super-secret-slack-token"
channel := "demo"

pbar := progress.New(token, channel, nil)

for i := 0; i <= pbar.Opts.TotalUnits; i++ {
    if err := pbar.Update(i); err != nil {
        log.Printf("Error updating progress bar: %s\n", err)
    }
}

Documentation

Overview

Package progress is a small library for creating a progress bar in slack

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrMaxPosExceeded is returned when the position passed to Progress.Update
	// is greater than Progress.Total or DefaultTotal if Progress.Total is 0.
	ErrMaxPosExceeded = errors.New("Maximum position exceeded")

	// ErrNegativePos is returned when a negative value is passed to Progress.Update.
	// All positions should be >= 0.
	ErrNegativePos = errors.New("Invalid position")
)

Functions

This section is empty.

Types

type Options

type Options struct {
	Fill        string // The character(s) used to fill in the progress bar
	Empty       string // The character(s) used to indicate empty space at the end of progress bar
	Width       int    // How many characters wide the progress bar should be. A value of 10 looks good on slack phone clients.
	TotalUnits  int    // Total possible units. Graph will always display 0-100%.
	Msg         string // The message template that will be sent to slack. Uses text/template for creating templates.
	Task        string // Name of the task we are showing progress for.
	AsUser      bool   // Whether or not to post as the user. If false posts as a generic bot and doesn't show edited next to messages. If true the opposite of both is true. Defaults to false.
	ShowEstTime bool   // Whether or not to show estimated time remaining
}

Options can be used to customize look of the progress bar. DefaultOptions() has pretty good defaults.

func DefaultOptions

func DefaultOptions(task string) *Options

DefaultOptions creates an Options struct with decent defaults.

type Progress

type Progress struct {
	Opts  *Options
	Start time.Time // When the task began running. Initialized to current time when New() is called.
	// contains filtered or unexported fields
}

Progress is a struct that creates the progress bar in slack

Example
package main

import (
	"log"

	"github.com/sfreiberg/progress"
)

func main() {
	token := "super-secret-slack-token"
	channel := "demo"

	pbar := progress.New(token, channel, nil)

	for i := 0; i <= pbar.Opts.TotalUnits; i++ {
		if err := pbar.Update(i); err != nil {
			log.Printf("Error updating progress bar: %s\n", err)
		}
	}
}
Output:

func New

func New(token, channel string, opts *Options) *Progress

New creates a new progress bar. If opts is nil then Progress will be created with DefaultOptions. The timer that is used for calculating time remaining is based on when this is instantiated so if it's not called around the time the task begins running it might report inaccurate results. You can fix this by setting Progress.Start manually.

func (*Progress) Update

func (p *Progress) Update(pos int) error

Update either posts a new progress bar if this is the first call or updates an existing progress bar.

Jump to

Keyboard shortcuts

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