-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
79 lines (61 loc) · 1.58 KB
/
utils.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
package main
import (
"github.com/fumiyas/go-tty"
"github.com/kardianos/osext"
"github.com/mattn/go-colorable"
"github.com/qpliu/qrencode-go/qrencode"
"github.com/russross/blackfriday"
"io/ioutil"
"math/rand"
"net"
"os"
"path"
"time"
)
func printQrCode(src string, inverse bool) {
grid, errQr := qrencode.Encode(src, qrencode.ECLevelL)
if errQr != nil {
panic(errQr)
}
da1, errQrText := tty.GetDeviceAttributes1(os.Stdout)
if errQrText == nil && da1[tty.DA1_SIXEL] {
printSixelQr(os.Stdout, grid, inverse)
} else {
stdout := colorable.NewColorableStdout()
printAnsiArtQr(stdout, grid, inverse)
}
}
func generateRandomKey(length int) string {
letters := []rune("abcdefghijklmnopqrstuvwxyz")
sequence := make([]rune, length)
rand.Seed(time.Now().UnixNano())
for i := range sequence {
sequence[i] = letters[rand.Intn(len(letters))]
}
return string(sequence)
}
func readMdFileToHtml(filename string) []byte {
normalizedRelativePath := normalizeRelativePath(filename)
if bytes, err := ioutil.ReadFile(normalizedRelativePath); err == nil {
return blackfriday.MarkdownBasic(bytes)
}
return []byte{}
}
func normalizeRelativePath(relativePath string) string {
filename, _ := osext.Executable()
return path.Join(path.Dir(filename), relativePath)
}
func findLocalIpAddress() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
panic(err)
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
panic("Could not find any suitable IP address")
}