package main import ( "flag" "fmt" "io/fs" "os" "strconv" "github.com/fatih/color" ) func usage() { //TODO: Fill this out fmt.Fprintf(os.Stderr, "Usage:\n") os.Exit(1) } func FormatFileSize(fi fs.FileInfo) (size string) { fileSizeFloat := float64(fi.Size()) switch { case fileSizeFloat > (1024 * 1024 * 1024): //GB fileSizeFloat /= (1024 * 1024 * 1024) size = strconv.FormatFloat(fileSizeFloat, 'f', 1, 64) return size + "G" case fileSizeFloat > (1024 * 1024): //MB fileSizeFloat /= (1024 * 1024) size = strconv.FormatFloat(fileSizeFloat, 'f', 1, 64) return size + "M" case fileSizeFloat > 1024: //KB fileSizeFloat /= 1024 size = strconv.FormatFloat(fileSizeFloat, 'f', 1, 64) return size + "K" case fileSizeFloat > 0: //B size = strconv.FormatFloat(fileSizeFloat, 'f', 0, 64) return size default: return "0" } } func GetFileMod(fi fs.FileInfo) (mod string) { //day month short time HH:MM day := strconv.Itoa(fi.ModTime().Day()) month := fi.ModTime().Month().String() timeH := strconv.Itoa(fi.ModTime().Hour()) timeM := strconv.Itoa(fi.ModTime().Minute()) if len(timeH) <= 1 { timeH = "0" + timeH } if len(timeM) <= 1 { timeM = "0" + timeM } mod = day + " " + month[0:3] + " " + timeH + ":" + timeM return mod } func PrintHeaders() { headersOutput := color.New(color.FgHiBlack).Add(color.Underline) //headersOutput.Printf("%-10s %7s %5s %11s\n", "Type", "Size", "Time", "Name") //leave this headersOutput.Printf("%-10s ", "Type") headersOutput.Printf("%7s ", "Size") headersOutput.Printf("%5s ", "Time") headersOutput.Printf("%11s\n", "Name") } func GetDir() { } func main() { var flagNoColor = flag.Bool("no-colour", false, "Disable color output") var flagEnableL = flag.Bool("l", false, "Enable ls -l output") var flagEnableA = flag.Bool("a", true, "Enable ls -a output") flag.Parse() if *flagNoColor { color.NoColor = true } if *flagEnableL { PrintHeaders() } dirOutput := color.New(color.FgCyan, color.Bold) fileOutput := color.New(color.FgWhite, color.Bold) files, err := os.ReadDir(".") if err != nil { fmt.Fprintf(os.Stderr, "%s\n", err) } for _, file := range files { fileName := file.Name() dirDash := "-" fileSize := "" fileModTime := "" fi, err := os.Stat(fileName) if err != nil { fmt.Fprintf(os.Stderr, "%s\n", err) } fileSize = FormatFileSize(fi) fileModTime = GetFileMod(fi) fileType := fi.Mode() if *flagEnableL { //PRINT JUST FILE FORMAT if file.IsDir() && (*flagEnableA && file.Name()[:1] == ".") { dirOutput.Printf("%s ", fileName) } else { fileOutput.Printf("%s ", fileName) } } else { //PRINT LIST FORMAT if file.IsDir() && (*flagEnableA && file.Name()[:1] == ".") { //print -l dirOutput.Printf("%s %7s %7s\t%s\n", fileType, dirDash, fileModTime, fileName) } else { fmt.Printf("%7s %7s %4s", fileType, fileSize, fileModTime) fileOutput.Printf("\t%s\n", fileName) } } } fmt.Println() }