zutils/cmd/read.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

48 lines
No EOL
1.2 KiB
Go

package cmd
import (
"fmt"
"os"
"github.com/zemenawi/zutils/pkg/formatter"
)
var ReadHelp = CommandHelp{
Name: "read",
Help: "Read and display file with smart detection.\nAutomatically detects file type:\n - Markdown (.md) - Renders with colored headings, lists, code blocks\n - Code files - Syntax highlighting with line numbers\n - Other files - Plain text display",
Examples: []string{
"z read README.md # Read markdown file",
"z read main.go # Read code with syntax highlighting",
"z read package.json # Read JSON file",
},
}
func ReadCommand(args []string) error {
if len(args) < 1 {
return fmt.Errorf("missing file path\n\nUsage: z read <file>")
}
filePath := args[0]
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("cannot open file '%s': %s", filePath, err)
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
return fmt.Errorf("cannot read file info '%s': %s", filePath, err)
}
if fileInfo.IsDir() {
return fmt.Errorf("'%s' is a directory\n\nUse: z info dir %s", filePath, filePath)
}
handler := formatter.ReadHandlers.GetHandler(filePath)
if handler != nil {
return handler(args)
}
return formatter.ReadFileWithHighlight(file, filePath)
}