main
simonkellet 1 year 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
.*
*.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() {
//TODO: Fill this out
fmt.Fprintf(os.Stderr, "Usage:\n")
os.Exit(1)
}
func formatFileSize(fi fs.FileInfo) (size string) {
switch fileSizeFloat := float64(fi.Size()); {
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)
@ -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 := strconv.Itoa(fi.ModTime().Day())
month := fi.ModTime().Month().String()
@ -68,10 +69,14 @@ func PrintHeaders() {
headersOutput.Printf("%11s\n", "Name")
}
func GetDir() {
}
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
var flagEnableL = flag.Bool("l", false, "Enable ls -l output")
var flagEnableA = flag.Bool("a", true, "Enable ls -a output")
flag.Parse()
@ -79,52 +84,48 @@ func main() {
color.NoColor = true
}
dirOutput := color.New(color.FgCyan, color.Bold)
fileOutput := color.New(color.FgWhite, color.Bold)
if len(os.Args) == 1 {
dir = "."
} else {
dir = os.Args[1]
if *flagEnableL {
PrintHeaders()
}
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 {
usage()
}
if !*flagNoList {
PrintHeaders()
fmt.Fprintf(os.Stderr, "%s\n", err)
}
for _, file := range files {
fileName := file.Name()
fileType := file.Type()
dirSize := "-"
dirDash := "-"
fileSize := ""
fileModTime := ""
fi, err := os.Stat(fileName)
if err != nil {
usage()
fmt.Fprintf(os.Stderr, "%s\n", err)
}
fileSize = formatFileSize(fi)
fileModTime = getFileMod(fi)
if !*flagNoList {
if file.IsDir() {
dirOutput.Printf("%s %7s %7s\t%s\n", fileType, dirSize, fileModTime, fileName) //leave this
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 {
fmt.Printf("%7s %7s %4s", fileType, fileSize, fileModTime)
fileOutput.Printf("\t%s\n", fileName)
fileOutput.Printf("%s ", fileName)
}
} else {
if file.IsDir() {
dirOutput.Printf("%s\n", 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 {
fileOutput.Printf("%s\n", fileName)
fmt.Printf("%7s %7s %4s", fileType, fileSize, fileModTime)
fileOutput.Printf("\t%s\n", fileName)
}
}
}
fmt.Println()
}

Loading…
Cancel
Save