| 155 | } |
| 156 | |
| 157 | func main() { |
| 158 | initView() |
| 159 | args.setDefault() |
| 160 | RootCmd := &cobra.Command{ |
| 161 | Use: "ftv {File_Name}", |
| 162 | Version: "0.8.1", |
| 163 | Short: "Fast table viewer for delimited file in terminal", |
| 164 | Run: func(cmd *cobra.Command, cmdargs []string) { |
| 165 | if args.Sep == "\\t" { |
| 166 | args.Sep = " " |
| 167 | } |
| 168 | if len([]rune(args.Sep)) > 0 { |
| 169 | b.sep = []rune(args.Sep)[0] |
| 170 | } |
| 171 | |
| 172 | // Configure memory limit |
| 173 | if args.MemoryMB > 0 { |
| 174 | b.setMemoryLimit(int64(args.MemoryMB) * 1024 * 1024) // Convert MB to bytes |
| 175 | } |
| 176 | // else use default (unlimited - 0) |
| 177 | |
| 178 | info, err := os.Stdin.Stat() |
| 179 | fatalError(err) |
| 180 | |
| 181 | // Determine if we should use async loading |
| 182 | useAsync := args.AsyncLoad |
| 183 | |
| 184 | //check whether from a console pipe |
| 185 | if info.Mode()&os.ModeCharDevice != 0 { |
| 186 | // FILE MODE |
| 187 | if len(cmdargs) < 1 { |
| 188 | stopView() |
| 189 | _ = cmd.Help() |
| 190 | return |
| 191 | } |
| 192 | //get file name form console |
| 193 | args.FileName = cmdargs[0] |
| 194 | |
| 195 | // Check if file exists before attempting to load |
| 196 | if _, err := os.Stat(args.FileName); os.IsNotExist(err) { |
| 197 | stopView() |
| 198 | fmt.Printf("⚠️ File not found: %s\n", args.FileName) |
| 199 | os.Exit(1) |
| 200 | } else if err != nil { |
| 201 | stopView() |
| 202 | fmt.Printf("⚠️ Cannot access file: %s\n", err) |
| 203 | os.Exit(1) |
| 204 | } |
| 205 | |
| 206 | if useAsync { |
| 207 | err = loadAndDisplayAsync(func(b *Buffer, updateChan chan<- bool, doneChan chan<- error) { |
| 208 | go loadFileToBufferAsync(args.FileName, b, updateChan, doneChan) |
| 209 | }, "File") |
| 210 | fatalError(err) |
| 211 | } else { |
| 212 | err = loadAndDisplaySync(func(b *Buffer) error { |
| 213 | return loadFileToBuffer(args.FileName, b) |
| 214 | }, "File") |