Documentation
¶
Overview ¶
Package scan provides a robust, context-aware command and line scanner.
It is designed to replace bufio.Scanner for interactive CLI applications, offering features like:
- Context cancellation support (breaking blocked reads).
- "Fake EOF" detection for Windows consoles (filtering transient interrupts).
- Configurable buffering and line handling callbacks.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(*Scanner)
Option configures the Scanner.
func WithBackoff ¶
WithBackoff configures the duration to wait before retrying interruptions or errors.
func WithBufferSize ¶
WithBufferSize sets the size of the internal read buffer.
func WithClearHandler ¶
func WithClearHandler(fn func()) Option
WithClearHandler sets the callback for when the line buffer is cleared (e.g. on interrupt).
func WithErrorHandler ¶
WithErrorHandler sets the callback for non-fatal errors.
func WithLineHandler ¶
WithLineHandler sets the callback for complete lines.
func WithThreshold ¶
WithForceExitThreshold sets the number of consecutive EOFs required to stop scanning.
func WithUnsafeMode ¶
WithUnsafeMode disables the EOF threshold check.
type Scanner ¶
type Scanner struct {
// contains filtered or unexported fields
}
Scanner reads lines from an io.Reader (like Stdin) with robust error handling and "Fake EOF" protection for Windows environments.
func NewScanner ¶
NewScanner creates a new robust scanner.
Example ¶
package main
import (
"context"
"fmt"
"os"
"github.com/aretw0/procio/scan"
)
func main() {
// Read from Stdin with context cancellation
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
scanner := scan.NewScanner(os.Stdin,
scan.WithLineHandler(func(line string) {
fmt.Println("User typed:", line)
}),
)
// Run in a goroutine if you need non-blocking start
go scanner.Start(ctx)
}
func (*Scanner) Start ¶
Start runs the read loop until context cancellation or persistent failure.
Example ¶
package main
import (
"context"
"fmt"
"strings"
"github.com/aretw0/procio/scan"
)
func main() {
// Simulate input
input := "hello\nworld"
reader := strings.NewReader(input)
// Create scanner
scanner := scan.NewScanner(reader,
scan.WithLineHandler(func(line string) {
fmt.Printf("Received: %s\n", line)
}),
)
// Block until EOF
scanner.Start(context.Background())
}
Output: Received: hello Received: world