-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlovi.go
More file actions
87 lines (73 loc) · 1.32 KB
/
lovi.go
File metadata and controls
87 lines (73 loc) · 1.32 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"github.com/mattn/go-tty"
"os"
"path/filepath"
)
type state struct {
conf *config
currentFilePath string
}
func main() {
configFileName := "lovi.config"
args := os.Args
amountArgs := len(args)
if amountArgs < 2 {
fmt.Println("usage: lovi <Name from config file>")
os.Exit(1)
}
var Config config
err := setConfigFromFile(&Config, configFileName)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
dirpath, err := Config.getPath(args[1])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fileName, err := getLatestFile(dirpath)
if err != nil {
fmt.Print(err)
os.Exit(1)
}
State := state{
conf: &Config,
currentFilePath: filepath.Join(dirpath, fileName),
}
go handleUserKeyInput(&State)
fileChan := make(chan string)
go loopPrintFile(&State, fileChan)
for {
content := <-fileChan
fmt.Print(content)
}
}
func handleUserKeyInput(State *state) {
tty, err := tty.Open()
if err != nil {
}
defer tty.Close()
for {
r, err := tty.ReadRune()
if err != nil {
continue
}
switch r {
case 'q':
os.Exit(0)
default:
path, err := State.conf.getPathFromHotkey(r)
if err != nil {
continue
}
file, err := getLatestFile(path)
if err != nil {
continue
}
State.currentFilePath = filepath.Join(path, file)
}
}
}