@@ -2,6 +2,7 @@ package scanview
22
33import (
44 "net"
5+ "sort"
56 "time"
67
78 "charm.land/bubbles/v2/stopwatch"
@@ -19,7 +20,24 @@ type ProgressMsg struct {
1920type CompleteMsg struct {}
2021type QuitMsg struct {}
2122
22- // appendIfNew appends host to hosts only if no existing entry has the same IP.
23+ func sortHosts (hosts []shared.HostResult ) {
24+ sort .Slice (hosts , func (i , j int ) bool {
25+ a := net .ParseIP (hosts [i ].IP ).To4 ()
26+ b := net .ParseIP (hosts [j ].IP ).To4 ()
27+ if a == nil || b == nil {
28+ return hosts [i ].IP < hosts [j ].IP
29+ }
30+ for k := range a {
31+ if a [k ] != b [k ] {
32+ return a [k ] < b [k ]
33+ }
34+ }
35+ return false
36+ })
37+ }
38+
39+ // appendIfNew appends host to hosts only if no existing entry has the same IP,
40+ // keeping the slice sorted by IP.
2341func appendIfNew (hosts []shared.HostResult , host * shared.HostResult ) []shared.HostResult {
2442 if host == nil {
2543 return hosts
@@ -29,7 +47,9 @@ func appendIfNew(hosts []shared.HostResult, host *shared.HostResult) []shared.Ho
2947 return hosts
3048 }
3149 }
32- return append (hosts , * host )
50+ hosts = append (hosts , * host )
51+ sortHosts (hosts )
52+ return hosts
3353}
3454
3555func ListenForProgress (progressChan <- chan shared.ProgressUpdate ) tea.Cmd {
@@ -85,6 +105,7 @@ func prepareForExit(m Model, shouldPrint bool) Model {
85105 if len (m .FinalHosts ) == 0 && len (m .FoundHosts ) > 0 {
86106 m .FinalHosts = make ([]shared.HostResult , len (m .FoundHosts ))
87107 copy (m .FinalHosts , m .FoundHosts )
108+ sortHosts (m .FinalHosts )
88109 }
89110 m .FoundHosts = nil
90111 m .Results .SetContent ("" )
0 commit comments