-
Notifications
You must be signed in to change notification settings - Fork 0
/
purge.go
73 lines (59 loc) · 1.71 KB
/
purge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"os"
"runtime"
"fmt"
"github.com/jmacdonald/purge/filesystem/directory/navigator"
"github.com/jmacdonald/purge/input"
"github.com/jmacdonald/purge/view"
)
func main() {
// Use all available "logical CPUs", as reported by the machine.
runtime.GOMAXPROCS(runtime.NumCPU())
// Determine in which directory to start,
// validating the path if passed by the user.
var startingPath string
if len(os.Args) > 1 {
startingPath = os.Args[1]
// Check that the specified directory exists.
path, error := os.Stat(startingPath)
if !(error == nil && path.IsDir()) {
fmt.Println("Can't open the specified directory.")
return
}
} else {
// Fall back to the current directory.
var err error
startingPath, err = os.Getwd()
if err != nil {
panic(err)
}
}
// Initialize (and schedule cleanup for) the view.
view.Initialize()
defer view.Close()
// Create a command channel that we'll use
// to communicate with the navigator.
nav := make(chan string)
// Create a buffer channel that the navigator will
// use to push updates to the view after state changes.
buffers := make(chan *view.Buffer)
// Start the view in a goroutine.
go view.New(buffers)
// Start the navigator in the starting directory.
go navigator.NewNavigator(startingPath, nav, buffers)
// Listen for user input, relaying the
// appropriate commands to the navigator.
for {
// Read a character from STDIN.
character := input.Read(os.Stdin)
// Map the character to its corresponding command.
command := input.Map[character]
// Don't pass the quit command along, just exit the application loop.
if command == "Quit" {
break
}
// Send the command along to the navigator.
nav <- command
}
}