pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/fmtutil/example_test.go (about) 1 package fmtutil 2 3 // ////////////////////////////////////////////////////////////////////////////////// // 4 // // 5 // Copyright (c) 2022 ESSENTIAL KAOS // 6 // Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // 7 // // 8 // ////////////////////////////////////////////////////////////////////////////////// // 9 10 import ( 11 "fmt" 12 ) 13 14 // ////////////////////////////////////////////////////////////////////////////////// // 15 16 func ExampleSeparator() { 17 // You can change color of separator symbols and title using fmtc color tags 18 19 SeparatorColorTag = "{r}" // Set color to red 20 SeparatorTitleColorTag = "{r*}" // Set color to red with bold weight 21 22 // Or you can remove colors 23 SeparatorColorTag = "" 24 SeparatorTitleColorTag = "" 25 26 // This is tiny separator (just 1 line) 27 Separator(true) 28 29 // This is wide separator with newlines before and after separator 30 Separator(false) 31 32 // You can set title of separator 33 Separator(true, "MY SEPARATOR") 34 } 35 36 func ExamplePrettyNum() { 37 var ( 38 n1 int = 10 39 n2 uint = 5000 40 n3 float64 = 6128750.26 41 ) 42 43 fmt.Printf("%d → %s\n", n1, PrettyNum(n1)) 44 fmt.Printf("%d → %s\n", n2, PrettyNum(n2)) 45 fmt.Printf("%.2f → %s\n", n3, PrettyNum(n3)) 46 47 // Set default order separator 48 OrderSeparator = " " 49 50 fmt.Printf("%.2f → %s\n", n3, PrettyNum(n3)) 51 52 // Use custom order separator 53 fmt.Printf("%.2f → %s\n", n3, PrettyNum(n3, "|")) 54 55 // Output: 56 // 10 → 10 57 // 5000 → 5,000 58 // 6128750.26 → 6,128,750.26 59 // 6128750.26 → 6 128 750.26 60 // 6128750.26 → 6|128|750.26 61 } 62 63 func ExamplePrettyBool() { 64 fmt.Printf("%t → %s\n", true, PrettyBool(true)) 65 fmt.Printf("%t → %s\n", false, PrettyBool(false)) 66 fmt.Printf("%t → %s\n", true, PrettyBool(true, "Yep", "Nope")) 67 fmt.Printf("%t → %s\n", false, PrettyBool(false, "Yep", "Nope")) 68 69 // Output: 70 // true → Y 71 // false → N 72 // true → Yep 73 // false → Nope 74 } 75 76 func ExamplePrettyPerc() { 77 var ( 78 n1 float64 = 0.123 79 n2 float64 = 10.24 80 n3 float64 = 1294.193 81 ) 82 83 OrderSeparator = "," 84 85 fmt.Printf("%f → %s\n", n1, PrettyPerc(n1)) 86 fmt.Printf("%f → %s\n", n2, PrettyPerc(n2)) 87 fmt.Printf("%f → %s\n", n3, PrettyPerc(n3)) 88 89 // Output: 90 // 0.123000 → 0.12% 91 // 10.240000 → 10.2% 92 // 1294.193000 → 1,294.2% 93 94 } 95 96 func ExamplePrettySize() { 97 s1 := 193 98 s2 := 184713 99 s3 := int64(46361936461) 100 101 fmt.Printf("%d → %s\n", s1, PrettySize(s1)) 102 fmt.Printf("%d → %s\n", s2, PrettySize(s2)) 103 fmt.Printf("%d → %s\n", s3, PrettySize(s3)) 104 105 // Set size separator 106 SizeSeparator = " " 107 108 fmt.Printf("%d → %s\n", s3, PrettySize(s3)) 109 110 // Use custom order separator 111 fmt.Printf("%d → %s\n", s3, PrettySize(s3, "|")) 112 113 // Output: 114 // 193 → 193B 115 // 184713 → 180.4KB 116 // 46361936461 → 43.2GB 117 // 46361936461 → 43.2 GB 118 // 46361936461 → 43.2|GB 119 } 120 121 func ExampleParseSize() { 122 s1 := "160" 123 s2 := "34Mb" 124 s3 := "2.2 GB" 125 126 fmt.Printf("%s → %d\n", s1, ParseSize(s1)) 127 fmt.Printf("%s → %d\n", s2, ParseSize(s2)) 128 fmt.Printf("%s → %d\n", s3, ParseSize(s3)) 129 130 // Output: 131 // 160 → 160 132 // 34Mb → 35651584 133 // 2.2 GB → 2362232012 134 } 135 136 func ExampleFloat() { 137 f1 := 0.3145 138 f2 := 3.452 139 f3 := 135.5215 140 141 fmt.Printf("%f → %g\n", f1, Float(f1)) 142 fmt.Printf("%f → %g\n", f2, Float(f2)) 143 fmt.Printf("%f → %g\n", f3, Float(f3)) 144 145 // Output: 146 // 0.314500 → 0.31 147 // 3.452000 → 3.45 148 // 135.521500 → 135.5 149 } 150 151 func ExampleWrap() { 152 text := "Aenean tincidunt metus a tortor varius, ut bibendum magna fringilla." 153 154 fmt.Println( 155 Wrap(text, "", 36), 156 ) 157 } 158 159 func ExampleColorizePassword() { 160 password := ">+XY!b3Rog" 161 162 fmt.Println(ColorizePassword(password, "{r}", "{y}", "{c}")) 163 164 // Output: 165 // {c}>+{r}XY{c}!{r}b{y}3{r}Rog{!} 166 }