|
|
|
@ -16,9 +16,15 @@ func usage() { |
|
|
|
|
os.Exit(1) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func FormatFileSize(fi fs.FileInfo) (size string) { |
|
|
|
|
fileSizeFloat := float64(fi.Size()) |
|
|
|
|
switch { |
|
|
|
|
func check(err error) { |
|
|
|
|
if err != nil { |
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err) |
|
|
|
|
//panic(err) //not sure about this :/
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func GetFileSize(fi fs.FileInfo) (size string) { |
|
|
|
|
switch fileSizeFloat := float64(fi.Size()); { |
|
|
|
|
case fileSizeFloat > (1024 * 1024 * 1024): //GB
|
|
|
|
|
fileSizeFloat /= (1024 * 1024 * 1024) |
|
|
|
|
size = strconv.FormatFloat(fileSizeFloat, 'f', 1, 64) |
|
|
|
@ -43,12 +49,17 @@ func FormatFileSize(fi fs.FileInfo) (size string) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func GetFileMod(fi fs.FileInfo) (mod string) { |
|
|
|
|
func GetFileModTime(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(day) == 1 { |
|
|
|
|
day = " " + day |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if len(timeH) <= 1 { |
|
|
|
|
timeH = "0" + timeH |
|
|
|
|
} |
|
|
|
@ -60,23 +71,77 @@ func GetFileMod(fi fs.FileInfo) (mod string) { |
|
|
|
|
return mod |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func GetFileType() { |
|
|
|
|
//TODO: this function returns the dxrxrx stuff!
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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 PrintWithoutL(content []fs.DirEntry, isFlagAEnable *bool) { |
|
|
|
|
dirOutput := color.New(color.FgMagenta, color.Bold) |
|
|
|
|
fileOutput := color.New(color.FgWhite, color.Bold) |
|
|
|
|
for _, item := range content { |
|
|
|
|
//check if the item is a hidden item (starts with . )
|
|
|
|
|
if item.Name()[:1] == "." && !*isFlagAEnable { |
|
|
|
|
continue //skip the file
|
|
|
|
|
} |
|
|
|
|
//check if item is dir
|
|
|
|
|
if item.IsDir() { |
|
|
|
|
dirOutput.Printf(" %s ", item.Name()) |
|
|
|
|
} else { |
|
|
|
|
fileOutput.Printf(" %s ", item.Name()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
fmt.Println() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func PrintWithL(content []fs.DirEntry, isFlagAEnable *bool) { |
|
|
|
|
dirOutput := color.New(color.FgMagenta, color.Bold) |
|
|
|
|
fileOutput := color.New(color.FgWhite, color.Bold) |
|
|
|
|
|
|
|
|
|
func GetDir() { |
|
|
|
|
//print headers
|
|
|
|
|
PrintHeaders() |
|
|
|
|
for _, item := range content { |
|
|
|
|
//we now need extra info from the files
|
|
|
|
|
fi, err := os.Stat(item.Name()) |
|
|
|
|
check(err) |
|
|
|
|
|
|
|
|
|
fType := item.Type().String() |
|
|
|
|
fDirSize := "-" |
|
|
|
|
fSize := GetFileSize(fi) |
|
|
|
|
fModTime := GetFileModTime(fi) |
|
|
|
|
|
|
|
|
|
//check if the item is a hidden item (starts with . )
|
|
|
|
|
if item.Name()[:1] == "." && !*isFlagAEnable { |
|
|
|
|
continue //skip the file
|
|
|
|
|
} |
|
|
|
|
//check if item is dir
|
|
|
|
|
if item.IsDir() { |
|
|
|
|
dirOutput.Printf("%s %7s %7s\t%s\n", fType, fDirSize, fModTime, item.Name()) //leave this
|
|
|
|
|
} else { |
|
|
|
|
fmt.Printf("%7s %7s %4s", fType, fSize, fModTime) |
|
|
|
|
fileOutput.Printf("\t%s\n", item.Name()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
fmt.Println() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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") |
|
|
|
|
var flagEnableA = flag.Bool("a", false, "Enable ls -a output") |
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
|
var flagNoColor = flag.Bool("no-colour", false, "Disable color output") |
|
|
|
|
var flagNoList = flag.Bool("no-l", false, "Disable ls -l output") |
|
|
|
|
var dir string |
|
|
|
|
|
|
|
|
|
flag.Parse() |
|
|
|
|
|
|
|
|
@ -84,48 +149,16 @@ func main() { |
|
|
|
|
color.NoColor = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if *flagEnableL { |
|
|
|
|
PrintHeaders() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//check if we can read the directory
|
|
|
|
|
pwd := os.Getenv("PWD") |
|
|
|
|
content, err := os.ReadDir(pwd) |
|
|
|
|
check(err) |
|
|
|
|
if !*flagEnableL { |
|
|
|
|
PrintWithoutL(content, flagEnableA) |
|
|
|
|
} else { |
|
|
|
|
PrintWithL(content, flagEnableA) |
|
|
|
|
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() |
|
|
|
|
} |
|
|
|
|