mirror of
https://github.com/skidoodle/ctx.git
synced 2026-04-27 18:57:41 +02:00
af9e0a5372
Implement CopyFile for darwin, linux, and windows and call it from main to copy generated output to the clipboard. Enable CGO for builds, update goreleaser and release workflow to use macOS, and add a cross-platform test workflow that verifies the clipboard.
89 lines
2.0 KiB
Go
89 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/skidoodle/ctx/clipboard"
|
|
)
|
|
|
|
const defaultOutputFilename = "ctx.txt"
|
|
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "ctx: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run() error {
|
|
flag.Usage = usage
|
|
configFlag := flag.Bool("config", false, "open global ignore file for editing")
|
|
outputFlag := flag.String("o", defaultOutputFilename, "output filename")
|
|
|
|
flag.Parse()
|
|
|
|
if *configFlag {
|
|
return openConfigFile()
|
|
}
|
|
|
|
args := flag.Args()
|
|
if len(args) == 0 {
|
|
usage()
|
|
return nil
|
|
}
|
|
|
|
targetDir := args[0]
|
|
absPath, err := filepath.Abs(targetDir)
|
|
if err != nil {
|
|
return fmt.Errorf("resolving path: %w", err)
|
|
}
|
|
|
|
start := time.Now()
|
|
|
|
cfg, err := loadConfig()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "ctx: warning: could not load config, using defaults (%v)\n", err)
|
|
cfg = parseConfig(defaultIgnoreText)
|
|
}
|
|
|
|
scanner := NewScanner(absPath, cfg)
|
|
files, err := scanner.Scan()
|
|
if err != nil {
|
|
return fmt.Errorf("scanning directory: %w", err)
|
|
}
|
|
|
|
if len(files) == 0 {
|
|
return fmt.Errorf("no files found in %s", absPath)
|
|
}
|
|
|
|
tokenCount, err := writeOutput(absPath, files, *outputFlag)
|
|
if err != nil {
|
|
return fmt.Errorf("writing output: %w", err)
|
|
}
|
|
|
|
if err := clipboard.CopyFile(*outputFlag); err != nil {
|
|
fmt.Fprintf(os.Stderr, "ctx: warning: clipboard copy failed: %v\n", err)
|
|
} else {
|
|
fmt.Printf("ctx: copied %s to clipboard\n", *outputFlag)
|
|
}
|
|
|
|
duration := time.Since(start).Round(time.Millisecond)
|
|
fmt.Printf("ctx: generated %s (%d files, ~%d tokens) in %v\n",
|
|
*outputFlag, len(files), tokenCount, duration)
|
|
|
|
return nil
|
|
}
|
|
|
|
func usage() {
|
|
fmt.Fprintf(os.Stderr, "Usage: ctx [options] <directory>\n\n")
|
|
fmt.Fprintf(os.Stderr, "Options:\n")
|
|
fmt.Fprintf(os.Stderr, " -config Open global ignore file for editing\n")
|
|
fmt.Fprintf(os.Stderr, " -o <file> Output filename (default %q)\n", defaultOutputFilename)
|
|
fmt.Fprintf(os.Stderr, "\nExamples:\n")
|
|
fmt.Fprintf(os.Stderr, " ctx . # Generate and copy file\n")
|
|
}
|