Skip to content

Commit 9da1269

Browse files
committed
Basic ls.
0 parents  commit 9da1269

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

main.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"fmt"
6+
"log"
7+
"bytes"
8+
"strings"
9+
"io/ioutil"
10+
"golang.org/x/crypto/ssh/terminal"
11+
)
12+
13+
func main() {
14+
width, _, err := terminal.GetSize(int(os.Stdout.Fd()))
15+
if err != nil {
16+
fmt.Printf("error getting terminal dimensions\n")
17+
fmt.Printf("%v\n", err)
18+
os.Exit(1)
19+
}
20+
21+
// fmt.Println(width)
22+
23+
var buffer bytes.Buffer
24+
25+
files, err := ioutil.ReadDir("./")
26+
if err != nil { log.Fatal(err) }
27+
28+
maxSize := 0
29+
30+
for _, f := range files {
31+
// if this is a .dotfile and '-a' is not specified, skip it
32+
if []rune(f.Name())[0] == rune('.') {
33+
continue
34+
}
35+
name := f.Name()
36+
size := len(name)
37+
if maxSize < size { maxSize = size }
38+
}
39+
40+
count := 0
41+
42+
for _, f := range files {
43+
// if this is a .dotfile and '-a' is not specified, skip it
44+
if []rune(f.Name())[0] == rune('.') {
45+
continue
46+
}
47+
48+
name := f.Name()
49+
size := len(name)
50+
51+
difference := maxSize - size
52+
53+
54+
if count + maxSize + 1 > width {
55+
buffer.WriteString("\n")
56+
count = 0
57+
}
58+
59+
count += maxSize + 1
60+
61+
buffer.WriteString(name)
62+
buffer.WriteString(strings.Repeat(" ", difference))
63+
buffer.WriteRune(' ')
64+
}
65+
66+
// fmt.Println(maxSize)
67+
68+
fmt.Println(buffer.String())
69+
70+
}

0 commit comments

Comments
 (0)