/
terminal.go
55 lines (51 loc) · 1.38 KB
/
terminal.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
package util
import (
"github.com/desotech-it/whoami/app"
"fmt"
"io"
"path/filepath"
"strings"
"github.com/olekukonko/tablewriter"
"github.com/qeesung/image2ascii/convert"
)
func WriteWhoamiInfoAsText(w io.Writer, info app.WhoamiInfo, request string, clientInfo map[string]string) {
{
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"Hostname"})
table.Append([]string{info.Hostname})
table.Render()
}
{
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"IP", "Interface"})
for iface, addrs := range info.Addresses {
for _, addr := range addrs {
table.Append([]string{addr, iface})
}
}
table.Render()
}
{
requestNoCarriageReturn := strings.ReplaceAll(request, "\r\n", "\n")
table := tablewriter.NewWriter(w)
table.SetAutoWrapText(false)
table.SetHeader([]string{"Request"})
table.Append([]string{requestNoCarriageReturn})
table.Render()
}
{
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"Client Info"})
for k, v := range(clientInfo) {
table.Append([]string{fmt.Sprintf("%s=%s", k, v)})
}
table.Render()
}
}
func WriteImageAsText(w io.Writer, imageFilename string) {
options := convert.DefaultOptions
options.Ratio = 0.25
converter := convert.NewImageConverter()
imageLocation := filepath.Join("static", "images", imageFilename)
fmt.Fprint(w, converter.ImageFile2ASCIIString(imageLocation, &options))
}