chalk

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2026 License: MIT Imports: 19 Imported by: 0

README

chalk

Task-oriented I/O helper for Golang CLI. It wires together flag-driven input/output routing (local filesystem, S3, stdin/stdout) with a live terminal progress UI so you can focus on the processing logic.

Features

  • Resolves input from a local directory (-I ./path), an S3 bucket (-I s3://bucket), explicit file arguments, or stdin
  • Resolves output to a local directory (-O ./path), an S3 bucket (-O s3://bucket), a single file (-o out.txt), or stdout
  • Renders a live progress tree with elapsed timers, spinner, and per-task pass/fail status

Quick Start

package main

import (
  "context"

  "github.com/fogfish/chalk"
)

func process(ctx context.Context, path string, r io.Reader, w io.Writer) error {
  chalk.Task(0, path)

  // Subtask at level 1
  chalk.Task(1, "read")
  // ... read from fs
  chalk.Done()

  chalk.Task(1, "transform")
  // ... do work
  chalk.Done()

  chalk.Done()
  return nil
}

func main() {
  chalk.Start(process)
}

Run it:

mytool -I ./input -O ./output
mytool -I s3://my-bucket -O s3://out-bucket
cat file.txt | mytool

License

See LICENSE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Commit added in v0.0.7

func Commit[T any](key string, val T)

func Done

func Done(suffix ...string)

Done marks the current task as successfully completed. An optional note is appended after the task label, e.g. Done("(hits 50)").

func Fail

func Fail(err error)

Fail marks the current task as failed.

func NoColor added in v0.0.6

func NoColor()

NoColor disables color output in TTY mode, using plain black & white styles. Must be called before Start.

func NoTTY added in v0.0.6

func NoTTY()

NoTTY forces log-mode output (structured slog records, no colour, no spinner) even when stderr is an interactive terminal. Must be called before Start.

func Panic

func Panic(err error)

Panic fails all pending tasks and exits with code 1.

func Printf added in v0.0.4

func Printf(format string, args ...any)

Printf prints a formatted message indented under the current task.

func Recover added in v0.0.7

func Recover[T any](key string, def T) T

func Start

func Start(f spool.Spooler)

Start wires up the default Reporter, parses flags, and runs the provided Spooler. It is the main entry-point for CLI tools built on this library.

func Sub added in v0.0.8

func Sub(ctx context.Context) context.Context

Sub returns a child context with the task nesting level incremented by one. Pass the returned context to Task to start a nested sub-task.

func Task

func Task(ctx context.Context, label string, args ...any)

Task begins a new task at the nesting level carried by ctx. Use WithLevel to produce a context for sub-tasks. Any active tasks at the same or deeper level are auto-completed first.

Types

type Proxy added in v0.0.8

type Proxy struct{}
var Stdout Proxy

func (Proxy) Done added in v0.0.8

func (Proxy) Done(suffix ...string)

func (Proxy) Fail added in v0.0.8

func (Proxy) Fail(err error)

func (Proxy) Printf added in v0.0.8

func (Proxy) Printf(format string, args ...any)

func (Proxy) Sub added in v0.0.8

func (Proxy) Sub(ctx context.Context) context.Context

func (Proxy) Task added in v0.0.8

func (Proxy) Task(ctx context.Context, label string, args ...any)

type Reporter

type Reporter struct {
	// contains filtered or unexported fields
}

Reporter manages progress output using one of two printer strategies: ttyPrinter (colours + spinner) when stderr is an interactive terminal, or logPrinter (structured slog records) otherwise.

func Init added in v0.0.8

func Init() *Reporter

Init creates a Reporter that writes to stderr, automatically selecting the ttyPrinter or logPrinter strategy based on whether stderr is a terminal. If NoTTY has been called, logPrinter is always used regardless of the terminal.

func (*Reporter) Done

func (r *Reporter) Done(suffix ...string)

Done marks the current (innermost) task as successfully completed. An optional note is appended after the task label, e.g. Done("(hits 50)").

func (*Reporter) Fail

func (r *Reporter) Fail(err error)

Fail marks the current (innermost) task as failed. err is printed beneath the task line (ttyPrinter) or included as a structured field (logPrinter).

func (*Reporter) Printf added in v0.0.4

func (r *Reporter) Printf(format string, args ...any)

Printf prints a formatted message indented under the current task.

func (*Reporter) Quit

func (r *Reporter) Quit()

Quit stops any animation and marks all remaining tasks as done.

func (*Reporter) Sub added in v0.0.8

func (r *Reporter) Sub(ctx context.Context) context.Context

Sub returns a child context with the task nesting level incremented by one. Pass the returned context to Task to start a nested sub-task.

func (*Reporter) Task

func (r *Reporter) Task(ctx context.Context, label string, args ...any)

Task begins a new task at the nesting level carried by ctx. Use WithLevel to produce a context for sub-tasks. Any currently active tasks at the same or deeper level are automatically completed before the new task starts, which simplifies error handling — callers do not need to guarantee a matching Done/Fail on every code path.

type Stdio added in v0.0.8

type Stdio interface {
	Sub(ctx context.Context) context.Context
	Task(ctx context.Context, label string, args ...any)
	Done(suffix ...string)
	Fail(err error)
	Printf(format string, args ...any)
}

Stdio is the progress-reporting interface implemented by *Reporter. Accept Stdio in your own APIs to decouple callers from this package: any value whose method set matches Stdio satisfies the interface without importing chalk.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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