zutils/main.go
selamanapps 5b334071e0 feat: add user-friendly help system and new commands
New Commands:
- z read - Smart file reader with auto-detection (markdown, syntax highlighting)
- z sys - System overview (CPU, RAM, Disk, processes)
- z find - File search with pattern/size/time filters
- z search - User-friendly grep alternative
- z usage - Process resource usage monitor

Enhancements:
- Unified help system with consistent error messages
- Help on error shows usage and examples for each command
- "Command not found" suggests similar commands
- Directory info now detects .gitignore and shows ignored files
- File handler registry for extensible file type detection
- Added Italic, Dim, Underline color constants
2026-05-02 01:12:55 +03:00

127 lines
3.6 KiB
Go

package main
import (
"fmt"
"os"
"strings"
"github.com/zemenawi/zutils/cmd"
"github.com/zemenawi/zutils/pkg/colors"
"github.com/zemenawi/zutils/pkg/types"
)
func main() {
registry := types.NewCommandRegistry()
registry.Register(&types.Command{
Name: "read",
Description: "Read and display file with smart detection",
Handler: cmd.ReadCommand,
})
registry.Register(&types.Command{
Name: "sys",
Description: "Display system information overview",
Handler: cmd.SystemCommand,
})
registry.Register(&types.Command{
Name: "find",
Description: "Find files with pattern and filter support",
Handler: cmd.FindCommand,
})
registry.Register(&types.Command{
Name: "search",
Description: "Search for text pattern in files",
Handler: cmd.SearchCommand,
})
registry.Register(&types.Command{
Name: "usage",
Description: "Show resource usage for a process",
Handler: cmd.UsageCommand,
})
cmd.RegisterInfoCommands(registry)
registry.Register(&types.Command{
Name: "version",
Description: "Show version information",
Handler: cmd.VersionCommand,
})
args := os.Args[1:]
if len(args) == 0 {
printUsage(registry)
os.Exit(1)
}
if args[0] == "info" {
infoCmd, exists := registry.Get("info")
if !exists {
printUsage(registry)
os.Exit(1)
}
err := infoCmd.Handler(args[1:])
if err != nil {
cmd.PrintErrorAndHelp("info", err)
os.Exit(1)
}
return
}
commandName := args[0]
cmdArgs := args[1:]
if command, exists := registry.Get(commandName); exists {
err := command.Handler(cmdArgs)
if err != nil {
cmd.PrintErrorAndHelp(commandName, err)
os.Exit(1)
}
} else {
fmt.Printf("\n %sCommand not found: %s%s\n\n", colors.Red, args[0], colors.Reset)
fmt.Printf(" Did you mean one of these?\n\n")
suggestions := findSimilarCommands(args[0], registry.GetAll())
for _, s := range suggestions {
fmt.Printf(" %s%s%s - %s\n", colors.Yellow, s.Name, colors.Reset, s.Description)
}
fmt.Printf("\n")
printUsage(registry)
os.Exit(1)
}
}
func printUsage(registry *types.CommandRegistry) {
fmt.Printf("\n %s%sz%s - User-friendly terminal utilities\n\n", colors.Bold, colors.Cyan, colors.Reset)
fmt.Printf(" %sUsage:%s z <command> [arguments]\n\n", colors.Bold, colors.Reset)
fmt.Printf(" %sAvailable Commands:%s\n", colors.Bold, colors.Reset)
for _, command := range registry.GetAll() {
fmt.Printf(" %s%-10s%s %s\n", colors.Green, command.Name, colors.Reset, command.Description)
}
fmt.Printf("\n %sQuick Examples:%s\n", colors.Bold, colors.Reset)
fmt.Printf(" %sz read README.md%s # Display file with smart formatting\n", colors.Gray, colors.Reset)
fmt.Printf(" %sz sys%s # Show system overview\n", colors.Gray, colors.Reset)
fmt.Printf(" %sz find . -name '*.go'%s # Find files\n", colors.Gray, colors.Reset)
fmt.Printf(" %sz search pattern path%s # Search in files\n", colors.Gray, colors.Reset)
fmt.Printf(" %sz info .%s # Directory info with gitignore support\n", colors.Gray, colors.Reset)
fmt.Printf(" %sz info main.go%s # File information\n", colors.Gray, colors.Reset)
fmt.Printf("\n Type %sz <command>%s without arguments to see help\n\n", colors.Gray, colors.Reset)
}
func findSimilarCommands(input string, commands []*types.Command) []*types.Command {
var suggestions []*types.Command
for _, cmd := range commands {
if strings.HasPrefix(cmd.Name, input[:1]) {
suggestions = append(suggestions, cmd)
}
}
if len(suggestions) > 3 {
suggestions = suggestions[:3]
}
return suggestions
}