-
Notifications
You must be signed in to change notification settings - Fork 0
/
writroll.go
102 lines (91 loc) · 2.07 KB
/
writroll.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
/*
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
char getch(){
char ch = 0;
struct termios old = {0};
fflush(stdout);
if( tcgetattr(0, &old) < 0 ) perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if( tcsetattr(0, TCSANOW, &old) < 0 ) perror("tcsetattr ICANON");
if( read(0, &ch,1) < 0 ) perror("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if(tcsetattr(0, TCSADRAIN, &old) < 0) perror("tcsetattr ~ICANON");
return ch;
}
*/
import "C"
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
const (
defaultFileTypes = "h,c,cpp,hpp,js,ts,py,go"
)
func readDir(dir string, fileTypes map[string]struct{}) []string {
files, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
list := []string{}
for _, f := range files {
fp := filepath.Join(dir, f.Name())
if f.IsDir() {
list = append(list, readDir(fp, fileTypes)...)
} else if _, found := fileTypes[filepath.Ext(fp)]; found {
list = append(list, fp)
}
}
return list
}
func main() {
// Read args
count := flag.Int("count", 5, "count of characters on single key hit")
dir := flag.String("dir", "", "directory to get source files from")
fileTypesRaw := flag.String("filetypes", defaultFileTypes, "filetypes to use")
flag.Parse()
// Put current working directory if dir is empty
if *dir == "" {
pwd, err := os.Getwd()
if err != nil {
fmt.Println(err)
return
}
*dir = pwd
}
// Prepare set of file types
fileTypes := map[string]struct{}{}
for _, fileType := range strings.Split(*fileTypesRaw, ",") {
fileTypes["."+fileType] = struct{}{}
}
// Get all possible files & read them one by one
for _, file := range readDir(*dir, fileTypes) {
f, err := os.Open(file)
if err != nil {
return
}
// Read wanted amount of characters until we reach the EOF
var b = make([]byte, *count)
for {
C.getch()
s, err := f.Read(b)
if err != nil {
break
}
fmt.Print(string(b[:s]))
}
fmt.Println()
f.Close()
}
}