new: variables are now accessible from every command with {env.varname} (closes #276)

This commit is contained in:
evilsocket 2018-05-14 10:24:53 +02:00
commit a536fb4c55
No known key found for this signature in database
GPG key ID: 1564D7F30393A456

View file

@ -2,7 +2,6 @@ package session
import (
"fmt"
"regexp"
"strings"
"github.com/bettercap/bettercap/core"
@ -15,7 +14,26 @@ const (
DefaultPrompt = "{by}{fw}{cidr} {fb}> {env.iface.ipv4} {reset} {bold}» {reset}"
)
var PromptCallbacks = map[string]func(s *Session) string{
var (
// these are here because if colors are disabled,
// we need the updated core.* variables
effects = 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,
}
PromptCallbacks = map[string]func(s *Session) string{
"{cidr}": func(s *Session) string {
return s.Interface.CIDR()
},
@ -38,8 +56,7 @@ var PromptCallbacks = map[string]func(s *Session) string{
return fmt.Sprintf("%d", s.Queue.Stats.Errors)
},
}
var envRe = regexp.MustCompile(`{env\.([^}]+)}`)
)
type Prompt struct {
}
@ -54,25 +71,6 @@ func (p Prompt) Render(s *Session) string {
prompt = DefaultPrompt
}
// these are here because if colors are disabled,
// we need the updated core.* variables
var effects = 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,
}
for tok, effect := range effects {
prompt = strings.Replace(prompt, tok, effect, -1)
}
@ -81,13 +79,6 @@ func (p Prompt) Render(s *Session) string {
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) {
prompt += core.RESET