▍ humdrum codex / dots v1.0.0
1.2 KB raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package cli

import (
	"bufio"
	"fmt"
	"io"
	"strings"

	"github.com/humdrum-tiv/dots-cli/internal/day"
)

// FormatChanges renders the pre-write diff table shown before confirmation.
func FormatChanges(changes []day.Change, typeNames map[string]string, blockSize int) string {
	name := func(id *string) string {
		if id == nil {
			return "(cleared)"
		}
		if n, ok := typeNames[*id]; ok {
			return n
		}
		return *id
	}
	var b strings.Builder
	for _, c := range changes {
		from := "(empty)"
		if c.FromID != nil {
			from = name(c.FromID)
		}
		fmt.Fprintf(&b, "  %-6s %s → %s\n", day.SlotLabel(c.Index, blockSize), from, name(c.ToID))
	}
	return b.String()
}

// Confirm enforces the no-silent-overwrite rule: --yes bypasses, a real TTY
// prompts y/N (default N), and non-TTY without --yes refuses outright.
func Confirm(prompt string, yes bool, in io.Reader, out io.Writer, isTTY bool) (bool, error) {
	if yes {
		return true, nil
	}
	if !isTTY {
		return false, fmt.Errorf("would change existing blocks — re-run with --yes (non-interactive)")
	}
	fmt.Fprintf(out, "%s [y/N] ", prompt)
	line, _ := bufio.NewReader(in).ReadString('\n')
	return strings.TrimSpace(strings.ToLower(line)) == "y", nil
}