forked from bettercap/bettercap
/
prompt.go
95 lines (80 loc) · 2.16 KB
/
prompt.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
package session
import (
"fmt"
"regexp"
"strings"
"github.com/bettercap/bettercap/core"
"github.com/dustin/go-humanize"
)
const (
PromptVariable = "$"
DefaultPrompt = "{by}{fw}{cidr} {fb}> {env.iface.ipv4} {reset} {bold}» {reset}"
)
var PromptEffects = map[string]string{
"{bold}": core.BOLD,
"{dim}": core.DIM,
"{r}": core.RED,
"{g}": core.GREEN,
"{b}": core.BLUE,
"{y}": core.YELLOW,
"{fb}": core.FG_BLACK,
"{fw}": core.FG_WHITE,
"{bdg}": core.BG_DGRAY,
"{br}": core.BG_RED,
"{bg}": core.BG_GREEN,
"{by}": core.BG_YELLOW,
"{blb}": core.BG_LBLUE, // Ziggy this is for you <3
"{reset}": core.RESET,
}
var PromptCallbacks = map[string]func(s *Session) string{
"{cidr}": func(s *Session) string {
return s.Interface.CIDR()
},
"{net.sent}": func(s *Session) string {
return fmt.Sprintf("%d", s.Queue.Stats.Sent)
},
"{net.sent.human}": func(s *Session) string {
return humanize.Bytes(s.Queue.Stats.Sent)
},
"{net.received}": func(s *Session) string {
return fmt.Sprintf("%d", s.Queue.Stats.Received)
},
"{net.received.human}": func(s *Session) string {
return humanize.Bytes(s.Queue.Stats.Received)
},
"{net.packets}": func(s *Session) string {
return fmt.Sprintf("%d", s.Queue.Stats.PktReceived)
},
"{net.errors}": func(s *Session) string {
return fmt.Sprintf("%d", s.Queue.Stats.Errors)
},
}
var envRe = regexp.MustCompile("{env\\.([^}]+)}")
type Prompt struct {
}
func NewPrompt() Prompt {
return Prompt{}
}
func (p Prompt) Render(s *Session) string {
found, prompt := s.Env.Get(PromptVariable)
if found == false {
prompt = DefaultPrompt
}
for tok, effect := range PromptEffects {
prompt = strings.Replace(prompt, tok, effect, -1)
}
for tok, cb := range PromptCallbacks {
prompt = strings.Replace(prompt, tok, cb(s), -1)
}
m := envRe.FindAllString(prompt, -1)
for _, match := range m {
name := strings.Trim(strings.Replace(match, "env.", "", -1), "{}")
_, value := s.Env.Get(name)
prompt = strings.Replace(prompt, match, value, -1)
}
// make sure an user error does not screw all terminal
if strings.HasPrefix(prompt, core.RESET) == false {
prompt += core.RESET
}
return prompt
}