inital push after work
This commit is contained in:
parent
339e48df55
commit
f1b8318c1c
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
*.java
|
11
go.mod
Normal file
11
go.mod
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
module go-linux-tools/go-ls
|
||||||
|
|
||||||
|
go 1.20
|
||||||
|
|
||||||
|
require github.com/fatih/color v1.15.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.17 // indirect
|
||||||
|
golang.org/x/sys v0.6.0 // indirect
|
||||||
|
)
|
10
go.sum
Normal file
10
go.sum
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
|
||||||
|
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
|
||||||
|
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||||
|
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||||
|
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||||
|
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
|
||||||
|
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||||
|
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
130
main.go
Normal file
130
main.go
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
|
)
|
||||||
|
|
||||||
|
func usage() {
|
||||||
|
fmt.Fprintf(os.Stderr, "Usage:\n")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatFileSize(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)
|
||||||
|
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 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()
|
||||||
|
|
||||||
|
if *flagNoColor {
|
||||||
|
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]
|
||||||
|
}
|
||||||
|
|
||||||
|
files, err := os.ReadDir(dir)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
usage()
|
||||||
|
}
|
||||||
|
if !*flagNoList {
|
||||||
|
PrintHeaders()
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
fileName := file.Name()
|
||||||
|
fileType := file.Type()
|
||||||
|
dirSize := "-"
|
||||||
|
fileSize := ""
|
||||||
|
fileModTime := ""
|
||||||
|
|
||||||
|
fi, err := os.Stat(fileName)
|
||||||
|
if err != nil {
|
||||||
|
usage()
|
||||||
|
}
|
||||||
|
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
|
||||||
|
} else {
|
||||||
|
fmt.Printf("%7s %7s %4s", fileType, fileSize, fileModTime)
|
||||||
|
fileOutput.Printf("\t%s\n", fileName)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if file.IsDir() {
|
||||||
|
dirOutput.Printf("%s\n", fileName)
|
||||||
|
} else {
|
||||||
|
fileOutput.Printf("%s\n", fileName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user