File: utils.go

package info (click to toggle)
pat 0.19.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,296 kB
  • sloc: javascript: 3,891; sh: 124; makefile: 11
file content (119 lines) | stat: -rw-r--r-- 2,713 bytes parent folder | download
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package cli

import (
	"bufio"
	"context"
	"fmt"
	"io"
	"io/fs"
	"os"
	"path/filepath"
	"strings"
	"unicode"

	"github.com/la5nta/pat/app"
	"github.com/la5nta/pat/internal/debug"
	"github.com/la5nta/wl2k-go/fbb"
	"github.com/la5nta/wl2k-go/mailbox"
)

var stdin *bufio.Reader

func readLine() string {
	if stdin == nil {
		stdin = bufio.NewReader(os.Stdin)
	}

	str, _ := stdin.ReadString('\n')
	return strings.TrimSpace(str)
}

func isTerminal(f *os.File) bool {
	info, err := f.Stat()
	if err != nil {
		return true // Fail-safe
	}
	return (info.Mode() & os.ModeCharDevice) != 0
}

func prompt2(w io.Writer, question, defaultValue string, options ...string) string {
	var suffix string
	if len(options) > 0 {
		// Ensure default is included in options if not already present
		allOptions := options
		defaultFound := false
		for _, opt := range options {
			if strings.EqualFold(opt, defaultValue) {
				defaultFound = true
				break
			}
		}
		if !defaultFound && defaultValue != "" {
			allOptions = append([]string{defaultValue}, options...)
		}

		// Use standard (Y/n) format where uppercase indicates default
		formatted := make([]string, len(allOptions))
		for i, opt := range allOptions {
			if strings.EqualFold(opt, defaultValue) {
				formatted[i] = strings.ToUpper(opt)
			} else {
				formatted[i] = strings.ToLower(opt)
			}
		}
		suffix = fmt.Sprintf(" (%s)", strings.Join(formatted, "/"))
	} else if defaultValue != "" {
		// Free-text field with default value
		suffix = fmt.Sprintf(" [%s]", defaultValue)
	}

	fmt.Fprintf(w, "%s%s: ", question, suffix)
	response := readLine()
	if response == "" {
		return defaultValue
	}
	return response
}

func prompt(question, defaultValue string, options ...string) string {
	return prompt2(os.Stdout, question, defaultValue, options...)
}

func SplitFunc(c rune) bool {
	return unicode.IsSpace(c) || c == ',' || c == ';'
}

func exitOnContextCancellation(ctx context.Context) (cancel func()) {
	done := make(chan struct{}, 1)
	go func() {
		select {
		case <-ctx.Done():
			fmt.Println()
			os.Exit(1)
		case <-done:
		}
	}()
	return func() {
		select {
		case done <- struct{}{}:
		default:
		}
	}
}

func openMessage(a *app.App, path string) (*fbb.Message, error) {
	// Search if only MID is specified.
	if filepath.Dir(path) == "." && filepath.Ext(path) == "" {
		debug.Printf("openMessage(%q): Searching...", path)
		path += mailbox.Ext
		fs.WalkDir(os.DirFS(a.Mailbox().MBoxPath), ".", func(p string, d fs.DirEntry, err error) error {
			if d.Name() != path {
				return nil
			}
			debug.Printf("openMessage(%q): Found %q", d.Name(), p)
			path = filepath.Join(a.Mailbox().MBoxPath, p)
			return io.EOF
		})
	}
	return mailbox.OpenMessage(path)
}