tasuku

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2024 License: MIT Imports: 8 Imported by: 0

README

go-tasuku

Tests

The minimal task visualizer for go.

This is a go implementation of the great privatenumber/tasuku, a task visualizer library for Node.js.

Install

go get -u github.com/kumpmati/go-tasuku

Features

🚧 This library is still very much in progress, so some features from the JavaScript counterpart haven't been implemented yet:

  • Single task visualization
  • TODO: parallel, nested & grouped tasks
  • TODO: clearing completed tasks

Usage

Task states

  • Loading - task has not completed yet
  • Warning - SetWarning was called
  • Error - task returned an error, or SetError was called
  • - Cancelled - task was cancelled manually
  • Success - task completed without any errors
Basic
result, err := tasuku.Task("my task", func(t *tasuku.TaskCtx) (int, error) { return 1, nil })
SetTitle
result, err := tasuku.Task("my task", func(t *tasuku.TaskCtx) (int, error) {
  <-time.After(time.Second * 5)
  t.SetTitle("this is taking longer than expected...")

  <-time.After(time.Second)
  t.SetTitle("done!")

  return 2, nil
})

// Terminal output
// ⣷ my task

// After 5 seconds
// ⣷ this is taking longer than expected...

// Completed
// ✔ done!
SetDetail

Adds an extra message below the title after the task has completed.

result, err := tasuku.Task("my task", func(t *tasuku.TaskCtx) (int, error) {
  t.SetDetail("some details")

  return 3, nil
})

// ✔ my task
//   → some details
SetWarning
result, err := tasuku.Task("my task", func(t *tasuku.TaskCtx) (int, error) {
  t.SetWarning("some warning")

  return 4, nil
})

// ⚠ my task
//   → some warning
SetError / Returning errors

To show an error, either return the error at the end, or call t.SetError inside the task.

result, err := tasuku.Task("my task", func(t *tasuku.TaskCtx) (int, error) {
  return 5, errors.new("some error")
})

// ✖ my task
//   → some error

or

result, err := tasuku.Task("my task", func(t *tasuku.TaskCtx) (int, error) {
  // caller will receive this error, since the task function returns a nil error
  t.SetError(errors.New("some error"))

  return 6, nil
})

// ✖ my task
//  → some error
result, err := tasuku.Task("my task", func(t *tasuku.TaskCtx) (int, error) {
  // this will set the task status to error during execution, but the
  // caller will only receive the "second error" in the return statement
  t.SetError(errors.New("first error"))

  return 7, errors.New("second error")
})

// ✖ my task
//  → second error

To clear any error set by t.SetError, you can call t.ClearError before returning from the task.

Cancel
result, err := tasuku.Task("my task", func(t *tasuku.TaskCtx) (int, error) {
  if condition {
    t.Cancel("cancellation reason")
    return 1, nil // return nil error so that the cancellation error is returned to caller
  }

  return 6, errors.New("custom error")
})

// condition == true:
// - my task
//  → cancellation reason

// condition == false:
// ✖ my task
//  → custom error

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Task

func Task[R any](title string, fn func(t *TaskCtx) (R, error)) (R, error)

Task runs the given function, and prints the status of the task to the terminal.

Types

type TaskCtx

type TaskCtx struct {
	Context context.Context
	// contains filtered or unexported fields
}

func (*TaskCtx) Cancel

func (tc *TaskCtx) Cancel(reason string)

Cancel cancels the task context and sets the task state to "cancelled". If `reason` is non-empty, it's set as the task detail.

func (*TaskCtx) ClearError added in v0.1.1

func (tc *TaskCtx) ClearError()

ClearError clears the current error and detail, and changes the state to "success"

func (*TaskCtx) SetDetail

func (tc *TaskCtx) SetDetail(text string)

SetDetail changes the detail text that is shown after the task completes.

func (*TaskCtx) SetError

func (tc *TaskCtx) SetError(err error)

SetError changes the task title and sets the status to "error".

func (*TaskCtx) SetTitle

func (tc *TaskCtx) SetTitle(text string)

SetTitle updates the title of the task.

func (*TaskCtx) SetWarning

func (tc *TaskCtx) SetWarning(text string)

SetWarning sets the task state to "warning", updates the task detail text.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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