zutils/pkg/formatter/utils.go
selamanapps 800ab8464a feat: add syntax highlighting and markdown rendering
- Add 'z read' command with chroma syntax highlighting and line numbers
- Add 'z md' command for rendering markdown to terminal
- Add Italic, Dim, Underline color constants
- Move ReadFileContent to shared pkg/formatter/utils.go
- Custom markdown parser with colored headings, lists, checkboxes
- Code blocks in markdown use chroma syntax highlighting
2026-05-02 00:43:22 +03:00

61 lines
1.1 KiB
Go

package formatter
import (
"fmt"
"math"
"os"
)
// FormatSize converts bytes to human-readable format
func FormatSize(bytes int64) string {
if bytes == 0 {
return "0 B"
}
const unit = 1024
units := []string{"B", "KB", "MB", "GB", "TB", "PB"}
exp := int(math.Log(float64(bytes)) / math.Log(float64(unit)))
if exp > len(units)-1 {
exp = len(units) - 1
}
value := float64(bytes) / math.Pow(float64(unit), float64(exp))
return fmt.Sprintf("%.2f %s", value, units[exp])
}
// CenterText centers text within a given width
func CenterText(text string, width int) string {
if len(text) >= width {
return text[:width]
}
padding := width - len(text)
left := padding / 2
right := padding - left
return fmt.Sprintf("%s%s%s", repeat(" ", left), text, repeat(" ", right))
}
func repeat(s string, count int) string {
result := ""
for i := 0; i < count; i++ {
result += s
}
return result
}
func ReadFileContent(file *os.File) string {
content := make([]byte, 0)
buffer := make([]byte, 4096)
for {
n, err := file.Read(buffer)
if n > 0 {
content = append(content, buffer[:n]...)
}
if err != nil {
break
}
}
return string(content)
}