github.com/krzko/gosh@v0.0.0-20241128233202-5abd4ef06633/internal/utils/formatter/table.go (about) 1 // internal/utils/formatter/table.go 2 package formatter 3 4 import ( 5 "fmt" 6 "os" 7 "regexp" 8 "strings" 9 "time" 10 11 "github.com/krzko/gosh/internal/utils/color" 12 "github.com/olekukonko/tablewriter" 13 "golang.org/x/term" 14 ) 15 16 type TableFormatter struct { 17 theme *color.Theme 18 } 19 20 type FileInfo struct { 21 Name string 22 Size int64 23 Mode os.FileMode 24 ModTime time.Time 25 IsDir bool 26 Owner string 27 Group string 28 Permissions string 29 } 30 31 func New() *TableFormatter { 32 return &TableFormatter{ 33 theme: color.DefaultTheme(), 34 } 35 } 36 37 func stripANSI(str string) string { 38 ansi := regexp.MustCompile(`\x1b\[[0-9;]*m`) 39 return ansi.ReplaceAllString(str, "") 40 } 41 42 func (t *TableFormatter) FormatLongList(entries []FileInfo) error { 43 table := tablewriter.NewWriter(os.Stdout) 44 45 // Configure table 46 table.SetHeader([]string{"Permissions", "Owner", "Group", "Size", "Modified", "Name"}) 47 table.SetAutoWrapText(false) 48 table.SetAutoFormatHeaders(true) 49 table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) 50 table.SetAlignment(tablewriter.ALIGN_LEFT) 51 table.SetCenterSeparator("") 52 table.SetColumnSeparator(" ") 53 table.SetRowSeparator("") 54 table.SetHeaderLine(false) 55 table.SetBorder(false) 56 table.SetTablePadding("\t") 57 table.SetNoWhiteSpace(true) 58 59 // Add entries 60 for _, entry := range entries { 61 row := []string{ 62 t.theme.ColorizePermissions(entry.Permissions), 63 entry.Owner, 64 entry.Group, 65 formatSize(entry.Size), 66 entry.ModTime.Format("Jan _2 15:04"), 67 t.theme.ColorizeName(entry.Name, entry.IsDir, entry.Mode), 68 } 69 table.Append(row) 70 } 71 72 table.Render() 73 return nil 74 } 75 76 // FormatSimpleList provides a simpler listing format 77 func (t *TableFormatter) FormatSimpleList(entries []FileInfo) error { 78 for _, entry := range entries { 79 fmt.Println(t.theme.ColorizeName(entry.Name, entry.IsDir, entry.Mode)) 80 } 81 return nil 82 } 83 84 func (t *TableFormatter) FormatCompact(entries []FileInfo, width int) error { 85 if len(entries) == 0 { 86 return nil 87 } 88 89 // Get terminal width if not provided 90 if width <= 0 { 91 if w, _, err := term.GetSize(int(os.Stdout.Fd())); err == nil { 92 width = w 93 } else { 94 width = 80 // fallback width 95 } 96 } 97 98 // First pass: calculate maximum name length 99 maxLen := 0 100 for _, entry := range entries { 101 if len(entry.Name) > maxLen { 102 maxLen = len(entry.Name) 103 } 104 } 105 106 // Add minimum spacing between columns 107 colWidth := maxLen + 2 108 cols := width / colWidth 109 if cols < 1 { 110 cols = 1 111 } 112 113 // Calculate number of rows needed 114 numEntries := len(entries) 115 rows := (numEntries + cols - 1) / cols 116 117 // Create a matrix of entries 118 matrix := make([][]string, rows) 119 for i := range matrix { 120 matrix[i] = make([]string, cols) 121 } 122 123 // Fill the matrix 124 for i, entry := range entries { 125 col := i / rows 126 row := i % rows 127 name := entry.Name 128 if entry.IsDir { 129 name = t.theme.ColorizeName(name, true, entry.Mode) 130 } else { 131 name = t.theme.ColorizeName(name, false, entry.Mode) 132 } 133 if col < cols { 134 matrix[row][col] = name 135 } 136 } 137 138 // Print the matrix 139 for _, row := range matrix { 140 for colIdx, name := range row { 141 if name == "" { 142 continue 143 } 144 if colIdx > 0 { 145 fmt.Print(strings.Repeat(" ", colWidth-len(stripANSI(row[colIdx-1])))) 146 } 147 fmt.Print(name) 148 } 149 fmt.Println() 150 } 151 152 return nil 153 } 154 155 // FormatCompactList provides a compact multi-column listing 156 func (t *TableFormatter) FormatCompactList(entries []FileInfo) error { 157 termWidth := 80 // You might want to get actual terminal width 158 maxNameLength := 0 159 160 // Find the longest name 161 for _, entry := range entries { 162 if len(entry.Name) > maxNameLength { 163 maxNameLength = len(entry.Name) 164 } 165 } 166 167 // Add padding 168 columnWidth := maxNameLength + 2 169 columns := termWidth / columnWidth 170 if columns == 0 { 171 columns = 1 172 } 173 174 // Print entries in columns 175 for i, entry := range entries { 176 if i > 0 && i%columns == 0 { 177 fmt.Println() 178 } 179 fmt.Printf("%-*s", columnWidth, t.theme.ColorizeName(entry.Name, entry.IsDir, entry.Mode)) 180 } 181 fmt.Println() 182 183 return nil 184 } 185 186 func formatSize(size int64) string { 187 const unit = 1024 188 if size < unit { 189 return fmt.Sprintf("%d", size) 190 } 191 192 div, exp := int64(unit), 0 193 for n := size / unit; n >= unit; n /= unit { 194 div *= unit 195 exp++ 196 } 197 return fmt.Sprintf("%.1f%c", float64(size)/float64(div), "KMGTPE"[exp]) 198 }