diff --git a/daze.go b/daze.go index 3b8c3e5..5cda4c8 100644 --- a/daze.go +++ b/daze.go @@ -29,6 +29,7 @@ import ( "github.com/mohanson/daze/lib/doa" "github.com/mohanson/daze/lib/lru" + "github.com/mohanson/daze/lib/pretty" ) // ============================================================================ @@ -1052,6 +1053,24 @@ func (r *RandomReader) Read(p []byte) (int, error) { return len(p), nil } +// The PrettyReader struct represents a custom reader that keeps track of read bytes and prints progress. +type PrettyReader struct { + E uint64 // Total number of bytes read so far + F uint64 // Total capacity of the input stream + R io.Reader // The underlying reader that this object wraps around +} + +// The Read method reads data from the wrapped reader and prints progress updates. +func (r *PrettyReader) Read(p []byte) (int, error) { + if r.E == 0 { + pretty.PrintProgress(0) + } + n, err := r.R.Read(p) + r.E += uint64(n) + pretty.PrintProgress(float64(r.E) / float64(r.F)) + return n, err +} + // Salt converts the stupid password passed in by the user to 32-sized byte array. func Salt(s string) []byte { h := sha256.Sum256([]byte(s)) @@ -1075,8 +1094,14 @@ func Salt(s string) []byte { // LoadApnic loads remote resource. APNIC is the Regional Internet Registry administering IP addresses for the Asia // Pacific. func LoadApnic() map[string][]*net.IPNet { - log.Println("main: load apnic data from http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest") - f := doa.Try(OpenFile("http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest")) + url := "http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest" + log.Println("main: load apnic data from", url) + rep := doa.Try(http.Get(url)) + f := &ReadWriteCloser{ + Reader: &PrettyReader{0, uint64(rep.ContentLength), rep.Body}, + Writer: nil, + Closer: rep.Body, + } defer f.Close() r := map[string][]*net.IPNet{} s := bufio.NewScanner(f) diff --git a/lib/pretty/README.md b/lib/pretty/README.md new file mode 100644 index 0000000..19d621c --- /dev/null +++ b/lib/pretty/README.md @@ -0,0 +1,29 @@ +# Pretty + +Utilities to prettify console output. + +**Progress** + +``` +$ go run cmd/progress/main.go + +2025/03/12 09:53:42 pretty: [=========================> ] 59% +``` + + + +**Table** + +``` +$ go run cmd/table/main.go + +2025/03/12 09:51:57 pretty: City name Area Population Annual Rainfall +2025/03/12 09:51:57 pretty: ----------------------------------------- +2025/03/12 09:51:57 pretty: Adelaide 1295 1158259 600.5 +2025/03/12 09:51:57 pretty: Brisbane 5905 1857594 1146.4 +2025/03/12 09:51:57 pretty: Darwin 112 120900 1714.7 +2025/03/12 09:51:57 pretty: Hobart 1357 205556 619.5 +2025/03/12 09:51:57 pretty: Melbourne 1566 3806092 646.9 +2025/03/12 09:51:57 pretty: Perth 5386 1554769 869.4 +2025/03/12 09:51:57 pretty: Sydney 2058 4336374 1214.8 +``` diff --git a/lib/pretty/cmd/progress/main.go b/lib/pretty/cmd/progress/main.go new file mode 100644 index 0000000..28e929c --- /dev/null +++ b/lib/pretty/cmd/progress/main.go @@ -0,0 +1,16 @@ +package main + +import ( + "time" + + "github.com/mohanson/daze/lib/pretty" +) + +func main() { + pretty.PrintProgress(0) + for i := range 100 { + time.Sleep(time.Millisecond * 16) + pretty.PrintProgress(float64(i+1) / 100) + } + pretty.PrintProgress(1) +} diff --git a/lib/pretty/cmd/table/main.go b/lib/pretty/cmd/table/main.go new file mode 100644 index 0000000..f14b0f5 --- /dev/null +++ b/lib/pretty/cmd/table/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "github.com/mohanson/daze/lib/pretty" +) + +func main() { + pretty.PrintTable([][]string{ + {"City name", "Area", "Population", "Annual Rainfall"}, + {"Adelaide", "1295", "1158259", "600.5"}, + {"Brisbane", "5905", "1857594", "1146.4"}, + {"Darwin", "112", "120900", "1714.7"}, + {"Hobart", "1357", "205556", "619.5"}, + {"Melbourne", "1566", "3806092", "646.9"}, + {"Perth", "5386", "1554769", "869.4"}, + {"Sydney", "2058", "4336374", "1214.8"}, + }) +} diff --git a/lib/pretty/pretty.go b/lib/pretty/pretty.go new file mode 100644 index 0000000..0f3d43b --- /dev/null +++ b/lib/pretty/pretty.go @@ -0,0 +1,64 @@ +package pretty + +import ( + "fmt" + "log" + "os" + "strings" + + "github.com/mohanson/daze/lib/doa" +) + +// PrintProgress draw a progress bar in the terminal. The percent takes values ​​from 0 to 1. +func PrintProgress(percent float64) { + doa.Doa(0 <= percent && percent <= 1) + out, _ := os.Stdout.Stat() + // Identify if we are displaying to a terminal or through a pipe or redirect. + if out.Mode()&os.ModeCharDevice == os.ModeCharDevice { + // Save or restore cursor position. + if percent == 0 { + log.Writer().Write([]byte{0x1b, 0x37}) + } + if percent != 0 { + log.Writer().Write([]byte{0x1b, 0x38}) + } + } + cap := int(percent * 44) + buf := []byte("[ ] 000%") + for i := 1; i < cap+1; i++ { + buf[i] = '=' + } + buf[1+cap] = '>' + num := fmt.Sprintf("%3d", int(percent*100)) + buf[48] = num[0] + buf[49] = num[1] + buf[50] = num[2] + log.Println("pretty:", string(buf)) +} + +// PrintTable easily draw tables in terminal/console applications from a list of lists of strings. +func PrintTable(data [][]string) { + size := make([]int, len(data[0])) + for _, r := range data { + for j, c := range r { + size[j] = max(size[j], len(c)) + } + } + line := make([]string, len(data[0])) + for j, c := range data[0] { + l := size[j] + line[j] = strings.Repeat(" ", l-len(c)) + c + } + log.Println("pretty:", strings.Join(line, " ")) + for i, c := range size { + line[i] = strings.Repeat("-", c) + } + log.Println("pretty:", strings.Join(line, "-")) + for _, r := range data[1:] { + for j, c := range r { + l := size[j] + line[j] = strings.Repeat(" ", l-len(c)) + c + } + log.Println("pretty:", strings.Join(line, " ")) + } +} diff --git a/res/rule.cidr b/res/rule.cidr index 9fe88b1..5c24af1 100644 --- a/res/rule.cidr +++ b/res/rule.cidr @@ -456,7 +456,6 @@ L 43.230.20.0/22 L 43.230.32.0/22 L 43.230.68.0/22 L 43.230.72.0/22 -L 43.230.84.0/22 L 43.230.124.0/22 L 43.230.136.0/22 L 43.230.220.0/22 @@ -905,7 +904,6 @@ L 43.248.228.0/22 L 43.248.232.0/22 L 43.248.244.0/22 L 43.249.4.0/22 -L 43.249.8.0/22 L 43.249.120.0/22 L 43.249.132.0/22 L 43.249.136.0/22 @@ -1008,6 +1006,7 @@ L 43.255.72.0/22 L 43.255.76.0/22 L 43.255.84.0/22 L 43.255.96.0/22 +L 43.255.144.0/22 L 43.255.176.0/22 L 43.255.184.0/22 L 43.255.192.0/22 @@ -2507,7 +2506,6 @@ L 103.40.36.0/22 L 103.40.40.0/22 L 103.40.44.0/22 L 103.40.88.0/22 -L 103.40.100.0/22 L 103.40.158.0/23 L 103.40.174.0/23 L 103.40.192.0/22 @@ -2710,7 +2708,6 @@ L 103.49.72.0/22 L 103.49.76.0/22 L 103.49.96.0/22 L 103.49.108.0/22 -L 103.49.128.0/22 L 103.49.176.0/22 L 103.49.180.0/22 L 103.49.196.0/22 @@ -3302,7 +3299,9 @@ L 103.89.216.0/22 L 103.89.220.0/22 L 103.89.224.0/22 L 103.89.228.0/22 +L 103.90.51.0/24 L 103.90.52.0/22 +L 103.90.56.0/23 L 103.90.92.0/22 L 103.90.100.0/22 L 103.90.104.0/22 @@ -3499,7 +3498,6 @@ L 103.100.236.0/22 L 103.100.240.0/22 L 103.100.248.0/22 L 103.100.252.0/22 -L 103.101.4.0/22 L 103.101.8.0/22 L 103.101.12.0/22 L 103.101.60.0/22 @@ -3747,9 +3745,7 @@ L 103.126.124.0/22 L 103.126.128.0/22 L 103.126.132.0/22 L 103.126.208.0/22 -L 103.129.52.0/22 L 103.130.132.0/22 -L 103.130.152.0/24 L 103.130.160.0/22 L 103.130.228.0/22 L 103.131.20.0/22 @@ -3763,15 +3759,10 @@ L 103.131.228.0/22 L 103.131.240.0/22 L 103.132.22.0/23 L 103.132.60.0/22 -L 103.132.64.0/22 -L 103.132.68.0/22 -L 103.132.72.0/22 -L 103.132.76.0/22 +L 103.132.64.0/20 L 103.132.80.0/22 -L 103.132.104.0/22 -L 103.132.108.0/22 -L 103.132.112.0/22 -L 103.132.116.0/22 +L 103.132.104.0/21 +L 103.132.112.0/21 L 103.132.120.0/22 L 103.132.160.0/22 L 103.132.164.0/22 @@ -3785,7 +3776,6 @@ L 103.133.128.0/22 L 103.133.136.0/22 L 103.133.176.0/22 L 103.133.232.0/22 -L 103.134.12.0/24 L 103.134.196.0/22 L 103.134.232.0/23 L 103.135.80.0/22 @@ -3827,7 +3817,6 @@ L 103.139.204.0/23 L 103.139.212.0/23 L 103.140.8.0/23 L 103.140.14.0/23 -L 103.140.70.0/23 L 103.140.126.0/23 L 103.140.140.0/23 L 103.140.144.0/23 @@ -3860,9 +3849,7 @@ L 103.142.238.0/23 L 103.142.248.0/23 L 103.143.16.0/23 L 103.143.18.0/23 -L 103.143.31.0/24 L 103.143.74.0/23 -L 103.143.120.0/23 L 103.143.124.0/23 L 103.143.132.0/23 L 103.143.134.0/23 @@ -3873,17 +3860,13 @@ L 103.144.52.0/23 L 103.144.66.0/23 L 103.144.70.0/23 L 103.144.72.0/23 -L 103.144.108.0/23 L 103.144.136.0/23 -L 103.144.148.0/23 L 103.144.158.0/23 L 103.144.240.0/23 L 103.145.38.0/23 -L 103.145.40.0/23 L 103.145.42.0/23 L 103.145.60.0/23 L 103.145.72.0/23 -L 103.145.80.0/23 L 103.145.86.0/23 L 103.145.92.0/23 L 103.145.94.0/23 @@ -3943,7 +3926,6 @@ L 103.151.142.0/23 L 103.151.148.0/23 L 103.151.150.0/23 L 103.151.158.0/23 -L 103.151.178.0/23 L 103.151.206.0/23 L 103.151.216.0/23 L 103.151.228.0/23 @@ -5002,6 +4984,7 @@ L 103.239.0.0/22 L 103.239.44.0/22 L 103.239.68.0/22 L 103.239.152.0/22 +L 103.239.156.0/22 L 103.239.180.0/22 L 103.239.184.0/22 L 103.239.192.0/22 @@ -5237,8 +5220,6 @@ L 111.67.192.0/20 L 111.68.64.0/19 L 111.72.0.0/13 L 111.85.0.0/16 -L 111.91.192.0/19 -L 111.92.240.0/22 L 111.92.248.0/22 L 111.92.252.0/22 L 111.112.0.0/15 @@ -5371,7 +5352,6 @@ L 114.119.204.0/22 L 114.119.208.0/20 L 114.119.224.0/19 L 114.132.0.0/16 -L 114.134.184.0/22 L 114.134.188.0/23 L 114.135.0.0/16 L 114.138.0.0/15 @@ -5403,7 +5383,6 @@ L 115.47.0.0/16 L 115.48.0.0/12 L 115.69.64.0/20 L 115.84.0.0/18 -L 115.84.192.0/19 L 115.85.192.0/18 L 115.100.0.0/14 L 115.104.0.0/14 @@ -6257,6 +6236,8 @@ L 160.187.223.0/24 L 160.187.252.0/23 L 160.187.254.0/23 L 160.191.0.0/23 +L 160.191.104.0/23 +L 160.191.110.0/23 L 160.202.60.0/22 L 160.202.148.0/22 L 160.202.152.0/22 @@ -6272,9 +6253,30 @@ L 160.202.240.0/22 L 160.202.244.0/22 L 160.202.248.0/22 L 160.202.252.0/22 +L 160.250.14.0/23 +L 160.250.16.0/23 +L 160.250.18.0/23 +L 160.250.24.0/23 +L 160.250.84.0/23 +L 160.250.90.0/23 +L 160.250.102.0/23 +L 160.250.104.0/23 +L 160.250.140.0/24 +L 160.250.160.0/23 +L 160.250.170.0/23 +L 160.250.214.0/23 +L 160.250.252.0/23 L 161.120.0.0/16 L 161.189.0.0/16 L 161.207.0.0/16 +L 161.248.20.0/23 +L 161.248.42.0/23 +L 161.248.84.0/23 +L 161.248.92.0/23 +L 161.248.108.0/23 +L 161.248.110.0/23 +L 161.248.112.0/23 +L 161.248.136.0/23 L 162.14.0.0/16 L 162.105.0.0/16 L 163.0.0.0/16 @@ -6310,11 +6312,19 @@ L 163.53.168.0/22 L 163.53.172.0/22 L 163.53.188.0/22 L 163.53.240.0/22 +L 163.61.62.0/23 +L 163.61.113.0/24 +L 163.61.178.0/23 +L 163.61.202.0/23 +L 163.61.214.0/23 L 163.125.0.0/16 L 163.142.0.0/16 L 163.177.0.0/16 L 163.179.0.0/16 L 163.204.0.0/16 +L 163.223.28.0/23 +L 163.223.32.0/23 +L 163.223.68.0/23 L 163.228.0.0/16 L 164.52.0.0/17 L 166.111.0.0/16 @@ -6499,6 +6509,7 @@ L 192.140.212.0/22 L 192.144.128.0/17 L 192.197.113.0/24 L 193.112.0.0/16 +L 193.119.0.0/19 L 198.175.100.0/22 L 199.212.57.0/24 L 202.0.100.0/23 @@ -7039,6 +7050,7 @@ L 202.127.5.0/24 L 202.127.6.0/23 L 202.127.12.0/22 L 202.127.16.0/20 +L 202.127.40.0/21 L 202.127.48.0/20 L 202.127.112.0/20 L 202.127.128.0/20 @@ -8692,7 +8704,6 @@ L 2001:df0:59c0::/48 L 2001:df0:85c0::/48 L 2001:df0:9d40::/48 L 2001:df0:ac40::/48 -L 2001:df0:b180::/48 L 2001:df0:bf80::/48 L 2001:df0:d880::/48 L 2001:df0:f8c0::/48 @@ -8704,23 +8715,18 @@ L 2001:df1:5b80::/48 L 2001:df1:5fc0::/48 L 2001:df1:6180::/48 L 2001:df1:61c0::/48 -L 2001:df1:6b80::/48 L 2001:df1:a100::/48 -L 2001:df1:bd80::/48 L 2001:df1:c900::/48 L 2001:df1:d180::/48 L 2001:df1:da00::/48 L 2001:df1:f480::/48 -L 2001:df1:f580::/48 L 2001:df1:fd80::/48 L 2001:df2:80::/48 -L 2001:df2:180::/48 L 2001:df2:5780::/48 L 2001:df2:8bc0::/48 L 2001:df2:a580::/48 L 2001:df2:c240::/48 L 2001:df2:d4c0::/48 -L 2001:df3:1480::/48 L 2001:df3:15c0::/48 L 2001:df3:2a80::/48 L 2001:df3:3a80::/48 @@ -8743,25 +8749,26 @@ L 2001:df4:2e80::/48 L 2001:df4:3d80::/48 L 2001:df4:4b80::/48 L 2001:df4:4d80::/48 +L 2001:df4:a1c0::/48 L 2001:df4:a680::/48 L 2001:df4:a980::/48 L 2001:df4:c180::/48 L 2001:df4:c580::/48 L 2001:df4:c780::/48 -L 2001:df4:de80::/48 +L 2001:df4:e140::/48 +L 2001:df5:1440::/48 L 2001:df5:2080::/48 +L 2001:df5:2fc0::/48 L 2001:df5:5f80::/48 L 2001:df5:7800::/48 L 2001:df6:100::/48 L 2001:df6:3d00::/48 L 2001:df6:5d00::/48 L 2001:df6:6800::/48 -L 2001:df6:df00::/48 L 2001:df6:f400::/48 L 2001:df7:1480::/48 L 2001:df7:2b80::/48 L 2001:df7:6600::/48 -L 2001:df7:ab00::/48 L 2001:df7:e580::/48 L 2001:e08::/32 L 2001:e18::/32 @@ -8923,6 +8930,7 @@ L 2400:7f80::/32 L 2400:7fc0::/32 L 2400:8080::/32 L 2400:8200::/32 +L 2400:8201::/32 L 2400:82c0::/32 L 2400:8580::/32 L 2400:8600::/32 @@ -8964,7 +8972,6 @@ L 2400:a420::/32 L 2400:a480::/32 L 2400:a5a0::/32 L 2400:a6a0::/32 -L 2400:a6e0::/32 L 2400:a780::/32 L 2400:a860::/32 L 2400:a8a0::/32 @@ -9148,6 +9155,7 @@ L 2401:4a80::/32 L 2401:4b00::/32 L 2401:4f80::/32 L 2401:5180::/32 +L 2401:5680::/32 L 2401:58a0::/32 L 2401:5960::/32 L 2401:59c0::/32 @@ -9296,32 +9304,52 @@ L 2401:cc60::/32 L 2401:ce00::/32 L 2401:cf40::/32 L 2401:cfc0::/32 +L 2401:cfe0::/32 +L 2401:d060::/32 L 2401:d0c0::/32 +L 2401:d0e0::/32 L 2401:d140::/32 L 2401:d180::/32 L 2401:d2c0::/32 L 2401:d340::/32 +L 2401:d420::/32 L 2401:d780::/32 +L 2401:d7e0::/32 +L 2401:d8e0::/32 +L 2401:d920::/28 L 2401:da00::/32 +L 2401:dbe0::/32 +L 2401:dd20::/32 +L 2401:dd60::/32 +L 2401:dde0::/32 L 2401:de00::/32 +L 2401:dfe0::/32 +L 2401:e020::/32 L 2401:e080::/32 L 2401:e0c0::/32 L 2401:e140::/32 L 2401:e240::/32 L 2401:e2c0::/32 L 2401:e340::/32 +L 2401:e360::/32 +L 2401:e620::/32 L 2401:e840::/32 L 2401:e8c0::/32 L 2401:e940::/32 L 2401:e9c0::/32 L 2401:ec00::/32 L 2401:ec40::/32 +L 2401:f0a0::/32 +L 2401:f0e0::/32 +L 2401:f220::/32 L 2401:f300::/32 +L 2401:f320::/32 +L 2401:f3e0::/32 +L 2401:f4a0::/32 L 2401:f7c0::/32 L 2401:fa80::/32 L 2401:fb80::/32 L 2401:fc80::/32 -L 2401:fe80::/32 L 2401:ffc0::/32 L 2402:440::/32 L 2402:5c0::/32 @@ -9335,11 +9363,9 @@ L 2402:1600::/32 L 2402:1740::/32 L 2402:19c0::/32 L 2402:1ec0::/32 -L 2402:1f40::/32 L 2402:1f80::/32 L 2402:2000::/32 L 2402:2280::/32 -L 2402:22c0::/32 L 2402:2440::/32 L 2402:24c0::/32 L 2402:2540::/32 @@ -9583,7 +9609,6 @@ L 2403:24c0::/32 L 2403:2580::/32 L 2403:25c0::/32 L 2403:2680::/32 -L 2403:26c0::/32 L 2403:2740::/32 L 2403:2780::/32 L 2403:28c0::/32 @@ -10074,7 +10099,6 @@ L 2405:4880::/32 L 2405:4980::/32 L 2405:4a80::/32 L 2405:4b80::/32 -L 2405:4d40::/32 L 2405:4e80::/32 L 2405:4f80::/32 L 2405:5080::/32