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,48 +14,10 @@ const (
DefaultPrompt = "{by}{fw}{cidr} {fb}> {env.iface.ipv4} {reset} {bold}» {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 {
prompt = DefaultPrompt
}
var (
// these are here because if colors are disabled,
// we need the updated core.* variables
var effects = map[string]string{
effects = map[string]string{
"{bold}": core.BOLD,
"{dim}": core.DIM,
"{r}": core.RED,
@ -72,6 +33,43 @@ func (p Prompt) Render(s *Session) string {
"{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()
},
"{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)
},
}
)
type Prompt struct {
}
func NewPrompt() Prompt {
return Prompt{}
}
func (p Prompt) Render(s *Session) string {
found, prompt := s.Env.Get(PromptVariable)
if !found {
prompt = DefaultPrompt
}
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