A beautiful TUI for browsing files and reading markdown/code: - Two-pane layout with file tree and content viewer - Markdown rendering via Glamour - Syntax highlighting via Chroma - Auto-detects light/dark terminal theme - Arrow key navigation, Esc to go back - Page Up/Down, Home/End for scrolling Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
115 lines
2.5 KiB
Go
115 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
const version = "0.1.0"
|
|
|
|
func main() {
|
|
// Parse command line arguments
|
|
rootPath := "."
|
|
themeOverride := ""
|
|
|
|
for i := 1; i < len(os.Args); i++ {
|
|
arg := os.Args[i]
|
|
switch {
|
|
case arg == "-h" || arg == "--help":
|
|
printHelp()
|
|
os.Exit(0)
|
|
case arg == "-v" || arg == "--version":
|
|
fmt.Printf("reader v%s\n", version)
|
|
os.Exit(0)
|
|
case arg == "--light":
|
|
themeOverride = "light"
|
|
case arg == "--dark":
|
|
themeOverride = "dark"
|
|
case strings.HasPrefix(arg, "--theme="):
|
|
themeOverride = strings.TrimPrefix(arg, "--theme=")
|
|
case arg == "--save-theme":
|
|
// Save current detected theme
|
|
theme := DetectTheme()
|
|
if err := SaveThemePreference(theme.Name); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error saving theme: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Printf("Saved theme preference: %s\n", theme.Name)
|
|
os.Exit(0)
|
|
case !strings.HasPrefix(arg, "-"):
|
|
rootPath = arg
|
|
}
|
|
}
|
|
|
|
// Detect or set theme
|
|
var theme Theme
|
|
switch themeOverride {
|
|
case "light":
|
|
theme = LightTheme
|
|
case "dark":
|
|
theme = DarkTheme
|
|
default:
|
|
theme = DetectTheme()
|
|
}
|
|
SetTheme(theme)
|
|
|
|
// Verify path exists
|
|
info, err := os.Stat(rootPath)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// If it's a file, use its parent directory
|
|
if !info.IsDir() {
|
|
rootPath = "."
|
|
}
|
|
|
|
// Create and run the program
|
|
p := tea.NewProgram(
|
|
NewModel(rootPath),
|
|
tea.WithAltScreen(),
|
|
tea.WithMouseCellMotion(),
|
|
)
|
|
|
|
if _, err := p.Run(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func printHelp() {
|
|
help := `reader - A beautiful terminal file browser & markdown viewer
|
|
|
|
Usage:
|
|
reader [path] Open at path (default: current directory)
|
|
reader --light Force light theme
|
|
reader --dark Force dark theme
|
|
reader --save-theme Save detected theme as default
|
|
|
|
Options:
|
|
-h, --help Show this help
|
|
-v, --version Show version
|
|
--light Use light theme
|
|
--dark Use dark theme
|
|
--save-theme Detect and save theme preference
|
|
|
|
Keys:
|
|
↑/↓ Navigate / scroll
|
|
Enter Open file or folder
|
|
←/→ Collapse/expand folders
|
|
Esc Back to file browser
|
|
? Show help
|
|
q Quit
|
|
|
|
Theme is auto-detected from your terminal. Override with flags
|
|
or save a preference with --save-theme.
|
|
|
|
Config: ~/.config/reader/theme`
|
|
|
|
fmt.Println(help)
|
|
}
|