mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 13:09:49 -07:00
new: variables are now accessible from every command with {env.varname} (closes #276)
This commit is contained in:
parent
c326400257
commit
a536fb4c55
1 changed files with 39 additions and 48 deletions
|
@ -2,7 +2,6 @@ package session
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/bettercap/bettercap/core"
|
"github.com/bettercap/bettercap/core"
|
||||||
|
@ -15,7 +14,26 @@ const (
|
||||||
DefaultPrompt = "{by}{fw}{cidr} {fb}> {env.iface.ipv4} {reset} {bold}» {reset}"
|
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 {
|
"{cidr}": func(s *Session) string {
|
||||||
return s.Interface.CIDR()
|
return s.Interface.CIDR()
|
||||||
},
|
},
|
||||||
|
@ -38,8 +56,7 @@ var PromptCallbacks = map[string]func(s *Session) string{
|
||||||
return fmt.Sprintf("%d", s.Queue.Stats.Errors)
|
return fmt.Sprintf("%d", s.Queue.Stats.Errors)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
)
|
||||||
var envRe = regexp.MustCompile(`{env\.([^}]+)}`)
|
|
||||||
|
|
||||||
type Prompt struct {
|
type Prompt struct {
|
||||||
}
|
}
|
||||||
|
@ -54,25 +71,6 @@ func (p Prompt) Render(s *Session) string {
|
||||||
prompt = DefaultPrompt
|
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 {
|
for tok, effect := range effects {
|
||||||
prompt = strings.Replace(prompt, tok, effect, -1)
|
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)
|
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
|
// make sure an user error does not screw all terminal
|
||||||
if !strings.HasPrefix(prompt, core.RESET) {
|
if !strings.HasPrefix(prompt, core.RESET) {
|
||||||
prompt += core.RESET
|
prompt += core.RESET
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue