Skip to content

Commit 798216a

Browse files
committed
Introduce list package.
1 parent 8071c3e commit 798216a

File tree

3 files changed

+75
-47
lines changed

3 files changed

+75
-47
lines changed

list/root.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package list
2+
3+
import (
4+
"os"
5+
"log"
6+
"io/ioutil"
7+
"github.com/drn/nerd-ls/node"
8+
)
9+
10+
// List - Contains all info necessary to render list of nodes
11+
type List struct {
12+
Nodes []node.Node
13+
MaxLength int
14+
MaxSize int
15+
}
16+
17+
// Fetch - Fetch List representing current directory
18+
func Fetch(options map[string]bool) List {
19+
files, err := ioutil.ReadDir(".")
20+
if err != nil { log.Fatal(err) }
21+
22+
nodes := make([]node.Node, len(files)+2)
23+
24+
index := 0
25+
if options["all"] {
26+
file, _ := os.Stat(".")
27+
nodes[0] = node.New(file)
28+
file, _ = os.Stat("..")
29+
nodes[1] = node.New(file)
30+
index += 2
31+
}
32+
33+
for i:=0; i<len(files); i++ {
34+
if !options["all"] && []rune(files[i].Name())[0] == '.' { continue }
35+
nodes[index] = node.New(files[i])
36+
index++
37+
}
38+
39+
nodes = nodes[:index]
40+
41+
return List{
42+
nodes,
43+
maxLength(nodes),
44+
maxSize(nodes),
45+
}
46+
}
47+
48+
func maxLength(nodes []node.Node) int {
49+
maxLength := 0
50+
for _, node := range nodes {
51+
length := node.Length
52+
if maxLength < length { maxLength = length }
53+
}
54+
return maxLength
55+
}
56+
57+
func maxSize(nodes []node.Node) int {
58+
maxSize := 0
59+
for _, node := range nodes {
60+
size := node.Size
61+
if maxSize < size { maxSize = size }
62+
}
63+
return maxSize
64+
}

main.go

+9-19
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"flag"
77
"strings"
8-
"github.com/drn/nerd-ls/node"
8+
"github.com/drn/nerd-ls/list"
99
"golang.org/x/crypto/ssh/terminal"
1010
)
1111

@@ -26,7 +26,7 @@ var long = flag.Bool(
2626
func main() {
2727
flag.Parse()
2828

29-
nodes := node.Fetch(
29+
nodes := list.Fetch(
3030
map[string]bool{
3131
"all": *all,
3232
"long": *long,
@@ -40,43 +40,33 @@ func main() {
4040
}
4141
}
4242

43-
func displayLong(nodes []node.Node) {
44-
for _, node := range nodes {
43+
func displayLong(list list.List) {
44+
for _, node := range list.Nodes {
4545
fmt.Printf("%s %d %s\n", node.Mode, node.Size, node.Name)
4646
}
4747
}
4848

49-
func displayCompact(nodes []node.Node) {
49+
func displayCompact(list list.List) {
5050
width := width()
5151
count := 0
52-
maxLength := maxLength(nodes)
5352

5453
padding := 0
55-
for _, node := range nodes {
54+
for _, node := range list.Nodes {
5655
if padding > 0 { fmt.Print(strings.Repeat(" ", padding)) }
5756

58-
count += maxLength
57+
count += list.MaxLength
5958
if count >= width {
6059
fmt.Println()
61-
count = maxLength
60+
count = list.MaxLength
6261
}
6362

64-
padding = maxLength - node.Length
63+
padding = list.MaxLength - node.Length
6564

6665
fmt.Print(node.Name)
6766
}
6867
fmt.Println()
6968
}
7069

71-
func maxLength(nodes []node.Node) int {
72-
maxLength := 0
73-
for _, node := range nodes {
74-
size := node.Length
75-
if maxLength < size { maxLength = size }
76-
}
77-
return maxLength
78-
}
79-
8070
func width() int {
8171
width, _, err := terminal.GetSize(int(os.Stdout.Fd()))
8272
if err != nil {

node/root.go

+2-28
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package node
22

33
import (
44
"os"
5-
"log"
65
"fmt"
7-
"io/ioutil"
86
"github.com/fatih/color"
97
"github.com/drn/nerd-ls/icon"
108
)
@@ -17,32 +15,8 @@ type Node struct {
1715
Size int
1816
}
1917

20-
// Fetch - Fetch nodes in currently directory
21-
func Fetch(options map[string]bool) []Node {
22-
files, err := ioutil.ReadDir(".")
23-
if err != nil { log.Fatal(err) }
24-
25-
nodes := make([]Node, len(files)+2)
26-
27-
index := 0
28-
if options["all"] {
29-
file, _ := os.Stat(".")
30-
nodes[0] = new(file)
31-
file, _ = os.Stat("..")
32-
nodes[1] = new(file)
33-
index += 2
34-
}
35-
36-
for i:=0; i<len(files); i++ {
37-
if !options["all"] && []rune(files[i].Name())[0] == '.' { continue }
38-
nodes[index] = new(files[i])
39-
index++
40-
}
41-
42-
return nodes[:index]
43-
}
44-
45-
func new(file os.FileInfo) Node {
18+
// New - Initializes Node with os.FileInfo
19+
func New(file os.FileInfo) Node {
4620
name := rawName(file)
4721
length := len([]rune(name))
4822
name = colorize(file, name)

0 commit comments

Comments
 (0)