1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-22 21:29:02 +08:00
nali/pkg/entity/entity.go

76 lines
1.3 KiB
Go
Raw Normal View History

2021-07-30 21:46:06 +08:00
package entity
import (
"strings"
2021-08-02 12:01:25 +08:00
2021-08-03 07:54:35 +08:00
"github.com/fatih/color"
2021-08-02 12:01:25 +08:00
"github.com/zu1k/nali/pkg/dbif"
2021-07-30 21:46:06 +08:00
)
type EntityType uint
const (
2021-08-02 12:01:25 +08:00
TypeIPv4 = dbif.TypeIPv4
TypeIPv6 = dbif.TypeIPv6
TypeDomain = dbif.TypeDomain
TypePlain = 100
2021-07-30 21:46:06 +08:00
)
type Entity struct {
2023-03-02 16:53:50 +08:00
Loc [2]int // s[Loc[0]:Loc[1]]
2021-07-30 21:46:06 +08:00
Type EntityType
Text string
Info string
}
func (e Entity) ParseInfo() error {
return nil
}
type Entities []*Entity
func (es Entities) Len() int {
return len(es)
}
func (es Entities) Less(i, j int) bool {
2021-07-30 22:30:27 +08:00
return es[i].Loc[0] < es[j].Loc[0]
2021-07-30 21:46:06 +08:00
}
func (es Entities) Swap(i, j int) {
2021-07-30 22:30:27 +08:00
es[i], es[j] = es[j], es[i]
2021-07-30 21:46:06 +08:00
}
func (es Entities) String() string {
var result strings.Builder
for _, entity := range es {
result.WriteString(entity.Text)
2021-07-30 22:30:27 +08:00
if entity.Type != TypePlain && len(entity.Info) > 0 {
2021-07-30 21:46:06 +08:00
result.WriteString("[" + entity.Info + "] ")
}
}
return result.String()
2021-07-30 22:30:27 +08:00
}
2021-08-03 07:54:35 +08:00
func (es Entities) ColorString() string {
var line strings.Builder
for _, e := range es {
s := e.Text
switch e.Type {
case TypeIPv4:
s = color.GreenString(e.Text)
case TypeIPv6:
s = color.BlueString(e.Text)
case TypeDomain:
s = color.YellowString(e.Text)
}
if e.Type != TypePlain && len(e.Info) > 0 {
s += " [" + color.RedString(e.Info) + "] "
}
line.WriteString(s)
}
return line.String()
}