make staticFormat at Init
Vladimir Bauer
7 years ago
| 104 | 104 | // W represents width and C represents bit set of width related config. |
| 105 | 105 | // A decorator should embed WC, to enable width synchronization. |
| 106 | 106 | type WC struct { |
| 107 | W int | |
| 108 | C int | |
| 109 | format string | |
| 110 | wsync chan int | |
| 107 | W int | |
| 108 | C int | |
| 109 | dynFormat string | |
| 110 | staticFormat string | |
| 111 | wsync chan int | |
| 111 | 112 | } |
| 112 | 113 | |
| 113 | 114 | // FormatMsg formats final message according to WC.W and WC.C. |
| 119 | 120 | if (wc.C & DextraSpace) != 0 { |
| 120 | 121 | max++ |
| 121 | 122 | } |
| 122 | return fmt.Sprintf(fmt.Sprintf(wc.format, max), msg) | |
| 123 | return fmt.Sprintf(fmt.Sprintf(wc.dynFormat, max), msg) | |
| 123 | 124 | } |
| 124 | return fmt.Sprintf(fmt.Sprintf(wc.format, wc.W), msg) | |
| 125 | return fmt.Sprintf(wc.staticFormat, msg) | |
| 125 | 126 | } |
| 126 | 127 | |
| 127 | 128 | // Init initializes width related config. |
| 128 | 129 | func (wc *WC) Init() { |
| 129 | wc.format = "%%" | |
| 130 | wc.dynFormat = "%%" | |
| 130 | 131 | if (wc.C & DidentRight) != 0 { |
| 131 | wc.format += "-" | |
| 132 | wc.dynFormat += "-" | |
| 132 | 133 | } |
| 133 | wc.format += "%ds" | |
| 134 | wc.dynFormat += "%ds" | |
| 135 | wc.staticFormat = fmt.Sprintf(wc.dynFormat, wc.W) | |
| 134 | 136 | if (wc.C & DSyncWidth) != 0 { |
| 135 | 137 | wc.wsync = make(chan int) |
| 136 | 138 | } |
| 141 | 143 | return wc.wsync, (wc.C & DSyncWidth) != 0 |
| 142 | 144 | } |
| 143 | 145 | |
| 146 | // SetConfig sets new conf and return old conf. | |
| 144 | 147 | func (wc *WC) SetConfig(conf WC) (old WC) { |
| 145 | 148 | old = *wc |
| 146 | 149 | *wc = conf |