| 1 | package session |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | |
| 7 | "github.com/evilsocket/islazy/tui" |
| 8 | |
| 9 | "github.com/dustin/go-humanize" |
| 10 | ) |
| 11 | |
| 12 | const ( |
| 13 | PromptVariable = "$" |
| 14 | DefaultPrompt = "{by}{fw}{cidr} {fb}> {env.iface.ipv4} {reset} {bold}» {reset}" |
| 15 | DefaultPromptMonitor = "{by}{fb} {env.iface.name} {reset} {bold}» {reset}" |
| 16 | ) |
| 17 | |
| 18 | var ( |
| 19 | effects = map[string]string{} |
| 20 | PromptCallbacks = map[string]func(s *Session) string{ |
| 21 | "{cidr}": func(s *Session) string { |
| 22 | return s.Interface.CIDR() |
| 23 | }, |
| 24 | "{net.sent}": func(s *Session) string { |
| 25 | return fmt.Sprintf("%d", s.Queue.Stats.Sent) |
| 26 | }, |
| 27 | "{net.sent.human}": func(s *Session) string { |
| 28 | return humanize.Bytes(s.Queue.Stats.Sent) |
| 29 | }, |
| 30 | "{net.received}": func(s *Session) string { |
| 31 | return fmt.Sprintf("%d", s.Queue.Stats.Received) |
| 32 | }, |
| 33 | "{net.received.human}": func(s *Session) string { |
| 34 | return humanize.Bytes(s.Queue.Stats.Received) |
| 35 | }, |
| 36 | "{net.packets}": func(s *Session) string { |
| 37 | return fmt.Sprintf("%d", s.Queue.Stats.PktReceived) |
| 38 | }, |
| 39 | "{net.errors}": func(s *Session) string { |
| 40 | return fmt.Sprintf("%d", s.Queue.Stats.Errors) |
| 41 | }, |
| 42 | } |
| 43 | ) |
| 44 | |
| 45 | type Prompt struct { |
| 46 | } |
| 47 | |
| 48 | func NewPrompt() Prompt { |
| 49 | // these are here because if colors are disabled, |
| 50 | // we need the updated tui.* variables |
| 51 | effects = map[string]string{ |
| 52 | "{bold}": tui.BOLD, |
| 53 | "{dim}": tui.DIM, |
| 54 | "{r}": tui.RED, |
| 55 | "{g}": tui.GREEN, |
| 56 | "{b}": tui.BLUE, |
| 57 | "{y}": tui.YELLOW, |
| 58 | "{fb}": tui.FOREBLACK, |
| 59 | "{fw}": tui.FOREWHITE, |
| 60 | "{bdg}": tui.BACKDARKGRAY, |
| 61 | "{br}": tui.BACKRED, |
| 62 | "{bg}": tui.BACKGREEN, |
| 63 | "{by}": tui.BACKYELLOW, |
| 64 | "{blb}": tui.BACKLIGHTBLUE, // Ziggy this is for you <3 |
| 65 | "{reset}": tui.RESET, |
| 66 | } |
| 67 | return Prompt{} |
| 68 | } |
| 69 | |
| 70 | func (p Prompt) Render(s *Session) string { |
| 71 | found, prompt := s.Env.Get(PromptVariable) |
| 72 | if !found { |
| 73 | prompt = DefaultPrompt |
| 74 | } |
| 75 | |
| 76 | for tok, effect := range effects { |
| 77 | prompt = strings.Replace(prompt, tok, effect, -1) |
| 78 | } |
| 79 | |
| 80 | for tok, cb := range PromptCallbacks { |
| 81 | prompt = strings.Replace(prompt, tok, cb(s), -1) |
| 82 | } |
| 83 | |
| 84 | // make sure an user error does not screw all terminal |
| 85 | if !strings.HasPrefix(prompt, tui.RESET) { |
| 86 | prompt += tui.RESET |
| 87 | } |
| 88 | |
| 89 | return prompt |
| 90 | } |