Skip to content

Commit

Permalink
Here's the stuff, have fun
Browse files Browse the repository at this point in the history
  • Loading branch information
ReCore-sys committed Feb 28, 2022
0 parents commit 6f598bb
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 0 deletions.
Binary file added cat/cat.exe
Binary file not shown.
3 changes: 3 additions & 0 deletions cat/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/ReCore-sys/cat

go 1.18
86 changes: 86 additions & 0 deletions cat/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"os"
)

var (
red = "\033[31m"
green = "\033[32m"
yellow = "\033[33m"
blue = "\033[34m"
reset = "\033[0m"
)

func notation(size int64) string {
// Get the largest notation for a byte size in kb/mb/gb/ect.
var notation string
var bcount float64
if size < 1024 {
notation = "bytes"
bcount = float64(size)
} else if size < 1024*1024 {
notation = "KB"
bcount = float64(size) / 1024
} else if size < 1024*1024*1024 {
notation = "MB"
bcount = float64(size) / 1024 / 1024
} else if size < 1024*1024*1024*1024 {
notation = "GB"
bcount = float64(size) / 1024 / 1024 / 1024
} else {
notation = "TB"
bcount = float64(size) / 1024 / 1024 / 1024 / 1024
}
return fmt.Sprintf("%.2f %s", bcount, notation)
}

func main() {
// When the file is run, the user will give us a path to a file.
// We will read the file and print it to the screen.
// We will also print the file's size to the screen.
// Open the file.
if len(os.Args) == 1 {
fmt.Println("Please provide a file path.")
os.Exit(1)
} else if len(os.Args) > 1 {
path := os.Args[1]
file, err := os.Open(path)
if err != nil {
fmt.Println(err)
return
}
stats, err := file.Stat()
if err != nil {
log.Println(err)
}
// Close the file when we're done.
defer file.Close()

// Read the file.
bytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println(err)
return
}
print(red + "--- File Contents ---\n" + reset)
// Print the file's contents as a string to the screen.
fmt.Println(string(bytes))
print(red + "--- File Data ---\n" + reset)
// Size
size := stats.Size()
notation := notation(size)

fmt.Println("Size:", notation)
// Last Modified
print("Last Modified: ")
fmt.Println(stats.ModTime().Format("Mon Jan _2 15:04:05 2006"))
// File Permissions
println("Permissions: ", stats.Mode().String())

print(red + "--- End of File ---\n" + reset)
}
}
7 changes: 7 additions & 0 deletions ls/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/ReCore-sys/ls

go 1.18

require golang.org/x/term v0.0.0-20210927222741-03fcf44c2211

require golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
4 changes: 4 additions & 0 deletions ls/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Binary file added ls/ls.exe
Binary file not shown.
226 changes: 226 additions & 0 deletions ls/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"math"
"os"
pathlib "path/filepath"
"regexp"
"strings"

"golang.org/x/term"
)

func round(x float64) int {
return int(math.Floor(x + 0/5))
}
func getdircontent(dir string) (content []string) {
content = make([]string, 0)
files, _ := ioutil.ReadDir(dir)

for _, f := range files {
content = append(content, f.Name())
}
return content
}

const (
red = "\033[31m"
grn = "\033[32m"
white = "\033[37m"
)

func insert(a []string, c string, i int) []string {
return append(a[:i], append([]string{c}, a[i:]...)...)
}

func main() {
// Get the current directory

inpdir := os.Args

path, err := os.Getwd()
if len(inpdir) > 1 {
usrdir := inpdir[1]
// Check if the specified directory is valid when appeneded to the current working directory
if _, err := os.Stat(path + usrdir); !os.IsNotExist(err) {
path = path + usrdir
} else {
if _, err := os.Stat(usrdir); !os.IsNotExist(err) {
path = usrdir
} else {
fmt.Println("Invalid directory")
os.Exit(1)
}

}
}
// remove any dot slashes from the path
path = strings.Replace(path, "./", "", -1)

print("Contents of " + path + ":\n")

if err != nil {
print("Error reading dir: ")
log.Fatal(err)
}
files := getdircontent(path)
width, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
log.Fatal(err)
}
/*Using golang and the term, os, fmt and ioutil packages, create a script that displays all the files and folders from the terminal's working directory in 4 collumns, using red for folders and green for files. make sure the collumns always stretch across the entire terminal's width.*/

spacing := math.Floor(float64(width / 4))
var printstr string
for i, file := range files {
rawfile := path + "/" + file
icon := iconer(rawfile)
namelen := len(file)
if icon != "" {
namelen += 2
file = icon + "\u2002" + file
}

var displayname string
if namelen < int(spacing) {
displayname = file + strings.Repeat(" ", round(spacing-float64(namelen)))
} else if namelen < int(spacing)*2 {
displayname = file + strings.Repeat(" ", round(spacing*2-float64(namelen)))
files = insert(files, strings.Repeat(" ", int(spacing)), i+1)
} else if namelen < int(spacing)*3 {
displayname = file + strings.Repeat(" ", round(spacing*3-float64(namelen)))
files = insert(files, strings.Repeat(" ", int(spacing)), i+1)
files = insert(files, strings.Repeat(" ", int(spacing)), i+2)
} else if namelen < int(spacing)*4 {
displayname = file + strings.Repeat(" ", round(spacing*4-float64(namelen)))
files = insert(files, strings.Repeat(" ", int(spacing)), i+1)
files = insert(files, strings.Repeat(" ", int(spacing)), i+2)
files = insert(files, strings.Repeat(" ", int(spacing)), i+3)
} else {
// if it is more than 4 collumns, it will be cut off
displayname = file[:(int(spacing)*4)-3] + "..."
files = insert(files, strings.Repeat(" ", int(spacing)), i+1)
files = insert(files, strings.Repeat(" ", int(spacing)), i+2)
files = insert(files, strings.Repeat(" ", int(spacing)), i+3)
}

if m, err := regexp.MatchString("\\s+", file); m == false {
if err != nil {
log.Fatal(err)
}
fst, err := os.Stat(rawfile)
if err != nil {
log.Fatal(err)
}
if fst.IsDir() {

printstr += red + displayname
} else {
printstr += grn + displayname
}
}
}
printstr += white
fmt.Println(printstr)
}

func iconer(dirpath string) string {
// get the file extension

var icon string
// Get the name of the file/folder without the path
path := pathlib.Base(dirpath)
f, err := os.Stat(dirpath)
// if the error is an ENOENT, skip the file
if err != nil {
log.Fatal(err)
}
if f.IsDir() == false {

ext := path[strings.LastIndex(path, ".")+1:]
switch ext {
case "py":
icon = "\ue73c"
case "go", "mod", "sum":
icon = "\ufcd1"
case "exe":
icon = "\uf085"
case "jar", "java", "class":
icon = "\ue256"
case "cpp":
icon = "\ue61d"
case "c":
icon = "\ue61e"
case "cs":
icon = "\uf81a"
case "h":
icon = "\uf471"
case "sh", "bat", "cmd":
icon = "\uf120"
case "sql", "db", "sqlite3":
icon = "\uf472"
case "js":
icon = "\ue74e"
case "ts":
icon = "\ufbe4"
case "xml", "json", "yml", "yaml", "toml":
icon = "\ufb25"
case "txt":
icon = "\ue612"
case "html", "htm":
icon = "\ue736"
case "css":
icon = "\ue749"
case "md":
icon = "\ue73e"
case "log":
icon = "\uf71d"
case "lua":
icon = "\ue620"
case "php":
icon = "\uf81e"
case "img", "png", "jpg", "jpeg", "gif", "bmp", "ico":
icon = "\uf03e"
case "woff", "woff2", "ttf", "otf", "eot":
icon = "\ufb68"
case "sass", "scss":
icon = "\ufcea"
case "mp3", "wav", "ogg", "flac", "aac", "m4a", "mp4", "m4v", "avi", "mpg", "mpeg", "mov", "webm", "wmv", "flv", "3gp", "3g2":
icon = "\uf1c8"
case "zip", "rar", "7z", "tar", "gz", "bz2", "z", "lz", "lzma", "lzo", "lz4", "lzop", "lzma2", "lz4hc", "xz", "zst", "zstd", "zpaq":
icon = "\uf1c6"
case "csv", "xls", "xlt", "xltx", "xltm", "xltb", "xlsx", "xlsm", "xlsb", "xla", "xlam", "xll", "xlw", "xlr":
icon = "\uf0ce"
case "dll", "so":
icon = "\uf471"
default:
icon = ""
}
} else {
// Get the directory name
dir := path[strings.LastIndex(path, "/")+1:]
switch dir {
case ".git":
icon = "\ue5fd"
case ".vscode":
icon = "\ue70c"
case "logs":
icon = "\uf71d"
case "backups", "backup":
icon = "\uf650"
case "env", "envs", "environment", ".env":
icon = "\ue73c"
case "json":
icon = "\ufb25"
case "config", "configs":
icon = "\uf085"
default:
icon = "\uf07c"
}
}
return icon

}

0 comments on commit 6f598bb

Please sign in to comment.