1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-24 05:30:07 +00:00

feat: add --json flag to analyze command for non-TTY environments (#533)

* feat: add --json flag to analyze command

* feat: implement JSON output mode for analyze

* refactor: rename jsonOutput flag to jsonMode to avoid conflict
This commit is contained in:
Noah Qin
2026-03-04 16:22:09 +08:00
committed by GitHub
parent d35f6d0665
commit c88691c2c8
2 changed files with 97 additions and 3 deletions

View File

@@ -4,6 +4,7 @@ package main
import (
"context"
"flag"
"fmt"
"os"
"os/exec"
@@ -16,6 +17,10 @@ import (
tea "github.com/charmbracelet/bubbletea"
)
var (
jsonMode = flag.Bool("json", false, "output analysis as JSON instead of TUI")
)
type dirEntry struct {
Name string
Path string
@@ -127,9 +132,11 @@ func (m model) inOverviewMode() bool {
}
func main() {
flag.Parse()
target := os.Getenv("MO_ANALYZE_PATH")
if target == "" && len(os.Args) > 1 {
target = os.Args[1]
if target == "" && len(flag.Args()) > 0 {
target = flag.Args()[0]
}
var abs string
@@ -148,12 +155,20 @@ func main() {
isOverview = false
}
if *jsonMode {
runJSONMode(abs, isOverview)
} else {
runTUIMode(abs, isOverview)
}
}
func runTUIMode(path string, isOverview bool) {
// Warm overview cache in background.
prefetchCtx, prefetchCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer prefetchCancel()
go prefetchOverviewCache(prefetchCtx)
p := tea.NewProgram(newModel(abs, isOverview), tea.WithAltScreen())
p := tea.NewProgram(newModel(path, isOverview), tea.WithAltScreen())
if _, err := p.Run(); err != nil {
fmt.Fprintf(os.Stderr, "analyzer error: %v\n", err)
os.Exit(1)