main
simonkellet 10 months ago
parent f1b8318c1c
commit b4083e455c
  1. 2
      .gitignore
  2. 5
      build.sh
  3. BIN
      gls
  4. BIN
      go-ls
  5. 67
      main.go

2
.gitignore vendored

@ -1 +1,3 @@
*.java *.java
.*
*.txt

@ -0,0 +1,5 @@
#!/bin/bash
set -xe
go build -o gls main.go

BIN
gls

Binary file not shown.

BIN
go-ls

Binary file not shown.

@ -11,13 +11,14 @@ import (
) )
func usage() { func usage() {
//TODO: Fill this out
fmt.Fprintf(os.Stderr, "Usage:\n") fmt.Fprintf(os.Stderr, "Usage:\n")
os.Exit(1) os.Exit(1)
} }
func formatFileSize(fi fs.FileInfo) (size string) { func FormatFileSize(fi fs.FileInfo) (size string) {
switch fileSizeFloat := float64(fi.Size()); { fileSizeFloat := float64(fi.Size())
switch {
case fileSizeFloat > (1024 * 1024 * 1024): //GB case fileSizeFloat > (1024 * 1024 * 1024): //GB
fileSizeFloat /= (1024 * 1024 * 1024) fileSizeFloat /= (1024 * 1024 * 1024)
size = strconv.FormatFloat(fileSizeFloat, 'f', 1, 64) size = strconv.FormatFloat(fileSizeFloat, 'f', 1, 64)
@ -42,7 +43,7 @@ func formatFileSize(fi fs.FileInfo) (size string) {
} }
} }
func getFileMod(fi fs.FileInfo) (mod string) { func GetFileMod(fi fs.FileInfo) (mod string) {
//day month short time HH:MM //day month short time HH:MM
day := strconv.Itoa(fi.ModTime().Day()) day := strconv.Itoa(fi.ModTime().Day())
month := fi.ModTime().Month().String() month := fi.ModTime().Month().String()
@ -68,10 +69,14 @@ func PrintHeaders() {
headersOutput.Printf("%11s\n", "Name") headersOutput.Printf("%11s\n", "Name")
} }
func GetDir() {
}
func main() { func main() {
var flagNoColor = flag.Bool("no-colour", false, "Disable color output") var flagNoColor = flag.Bool("no-colour", false, "Disable color output")
var flagNoList = flag.Bool("no-l", false, "Disable ls -l output") var flagEnableL = flag.Bool("l", false, "Enable ls -l output")
var dir string var flagEnableA = flag.Bool("a", true, "Enable ls -a output")
flag.Parse() flag.Parse()
@ -79,52 +84,48 @@ func main() {
color.NoColor = true color.NoColor = true
} }
dirOutput := color.New(color.FgCyan, color.Bold) if *flagEnableL {
fileOutput := color.New(color.FgWhite, color.Bold) PrintHeaders()
if len(os.Args) == 1 {
dir = "."
} else {
dir = os.Args[1]
} }
files, err := os.ReadDir(dir) dirOutput := color.New(color.FgCyan, color.Bold)
fileOutput := color.New(color.FgWhite, color.Bold)
files, err := os.ReadDir(".")
if err != nil { if err != nil {
usage() fmt.Fprintf(os.Stderr, "%s\n", err)
}
if !*flagNoList {
PrintHeaders()
} }
for _, file := range files { for _, file := range files {
fileName := file.Name() fileName := file.Name()
fileType := file.Type() dirDash := "-"
dirSize := "-"
fileSize := "" fileSize := ""
fileModTime := "" fileModTime := ""
fi, err := os.Stat(fileName) fi, err := os.Stat(fileName)
if err != nil { if err != nil {
usage() fmt.Fprintf(os.Stderr, "%s\n", err)
} }
fileSize = formatFileSize(fi)
fileModTime = getFileMod(fi)
if !*flagNoList { fileSize = FormatFileSize(fi)
if file.IsDir() { fileModTime = GetFileMod(fi)
dirOutput.Printf("%s %7s %7s\t%s\n", fileType, dirSize, fileModTime, fileName) //leave this fileType := fi.Mode()
if *flagEnableL { //PRINT JUST FILE FORMAT
if file.IsDir() && (*flagEnableA && file.Name()[:1] == ".") {
dirOutput.Printf("%s ", fileName)
} else { } else {
fmt.Printf("%7s %7s %4s", fileType, fileSize, fileModTime) fileOutput.Printf("%s ", fileName)
fileOutput.Printf("\t%s\n", fileName)
} }
} else {
if file.IsDir() { } else { //PRINT LIST FORMAT
dirOutput.Printf("%s\n", fileName) if file.IsDir() && (*flagEnableA && file.Name()[:1] == ".") { //print -l
dirOutput.Printf("%s %7s %7s\t%s\n", fileType, dirDash, fileModTime, fileName)
} else { } else {
fileOutput.Printf("%s\n", fileName) fmt.Printf("%7s %7s %4s", fileType, fileSize, fileModTime)
fileOutput.Printf("\t%s\n", fileName)
} }
} }
} }
fmt.Println()
} }

Loading…
Cancel
Save