2026-05-02 00:43:22 +03:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
"github.com/zemenawi/zutils/pkg/formatter"
|
|
|
|
|
)
|
|
|
|
|
|
2026-05-02 01:12:55 +03:00
|
|
|
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",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-02 00:43:22 +03:00
|
|
|
func ReadCommand(args []string) error {
|
|
|
|
|
if len(args) < 1 {
|
2026-05-02 01:12:55 +03:00
|
|
|
return fmt.Errorf("missing file path\n\nUsage: z read <file>")
|
2026-05-02 00:43:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filePath := args[0]
|
|
|
|
|
|
|
|
|
|
file, err := os.Open(filePath)
|
|
|
|
|
if err != nil {
|
2026-05-02 01:12:55 +03:00
|
|
|
return fmt.Errorf("cannot open file '%s': %s", filePath, err)
|
2026-05-02 00:43:22 +03:00
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
fileInfo, err := file.Stat()
|
|
|
|
|
if err != nil {
|
2026-05-02 01:12:55 +03:00
|
|
|
return fmt.Errorf("cannot read file info '%s': %s", filePath, err)
|
2026-05-02 00:43:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if fileInfo.IsDir() {
|
2026-05-02 01:12:55 +03:00
|
|
|
return fmt.Errorf("'%s' is a directory\n\nUse: z info dir %s", filePath, filePath)
|
2026-05-02 00:43:22 +03:00
|
|
|
}
|
|
|
|
|
|
2026-05-02 01:12:55 +03:00
|
|
|
handler := formatter.ReadHandlers.GetHandler(filePath)
|
|
|
|
|
if handler != nil {
|
|
|
|
|
return handler(args)
|
2026-05-02 00:43:22 +03:00
|
|
|
}
|
|
|
|
|
|
2026-05-02 01:12:55 +03:00
|
|
|
return formatter.ReadFileWithHighlight(file, filePath)
|
|
|
|
|
}
|