-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcat.go
More file actions
59 lines (52 loc) · 1.15 KB
/
cat.go
File metadata and controls
59 lines (52 loc) · 1.15 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"syscall"
)
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
proc = kernel32.NewProc("SetConsoleTextAttribute")
)
func main() {
flag.Parse()
if flag.NArg() == 0 {
fmt.Println("Usage: ", os.Args[0], " file[file..]")
os.Exit(-1)
}
dirPth, _ := os.Getwd()
for i, v := range flag.Args() {
if i != 0 {
fmt.Println("------------------------------------------------------------------")
}
if strings.HasPrefix(v, "/") || strings.Index(v, ":") == 1 {
print(v)
continue
}
f := filepath.Join(dirPth, v)
print(f)
}
}
func print(file string) {
ColorPrintln(file+":", file+":")
data, err := ioutil.ReadFile(file)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(data))
}
func ColorPrintln(v, a string) {
idx := strings.Index(v, a)
fmt.Printf("%s", v[:idx])
handle, _, _ := proc.Call(uintptr(syscall.Stdout), uintptr(12)) //12 Red light
fmt.Printf(a)
handle, _, _ = proc.Call(uintptr(syscall.Stdout), uintptr(7)) //White dark
CloseHandle := kernel32.NewProc("CloseHandle")
CloseHandle.Call(handle)
fmt.Printf("%s\n", v[idx+len(a):])
}