Debian Codegraph @

sprintf.go

raw
1 //
2 // Copyright (c) 2016-2022 The Aurora Authors. All rights reserved.
3 // This program is free software. It comes without any warranty,
4 // to the extent permitted by applicable law. You can redistribute
5 // it and/or modify it under the terms of the Unlicense. See LICENSE
6 // file for more details or see below.
7 //
8
9 //
10 // This is free and unencumbered software released into the public domain.
11 //
12 // Anyone is free to copy, modify, publish, use, compile, sell, or
13 // distribute this software, either in source code form or as a compiled
14 // binary, for any purpose, commercial or non-commercial, and by any
15 // means.
16 //
17 // In jurisdictions that recognize copyright laws, the author or authors
18 // of this software dedicate any and all copyright interest in the
19 // software to the public domain. We make this dedication for the benefit
20 // of the public at large and to the detriment of our heirs and
21 // successors. We intend this dedication to be an overt act of
22 // relinquishment in perpetuity of all present and future rights to this
23 // software under copyright law.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28 // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 // OTHER DEALINGS IN THE SOFTWARE.
32 //
33 // For more information, please refer to <http://unlicense.org/>
34 //
35
36 package aurora
37
38 import (
39 "fmt"
40 "strconv"
41 "unicode/utf8"
42 )
43
44 type tailedValue struct {
45 Value
46 tail Color
47 }
48
49 func (v *tailedValue) Format(s fmt.State, verb rune) {
50
51 // it's enough for many cases (%-+020.10f)
52 // % - 1
53 // availFlags - 3 (5)
54 // width - 2
55 // prec - 3 (.23)
56 // verb - 1
57 // --------------
58 // 10
59 // +
60 // \033[ 5
61 // 0;1;3;4;5;7;8;9;20;21;51;52;53 30
62 // 38;5;216 8
63 // 48;5;216 8
64 // m 1
65 // +
66 // \033[0m 7
67 //
68 // x2 (possible tail color)
69 //
70 // 10 + 59 * 2 = 128
71
72 var (
73 format = make([]byte, 0, 128)
74 color = v.Color()
75 )
76 if color != 0 {
77 format = append(format, esc...)
78 format = color.appendNos(format, v.tail != 0)
79 format = append(format, 'm')
80 }
81 format = append(format, '%')
82 var f byte
83 for i := 0; i < len(availFlags); i++ {
84 if f = availFlags[i]; s.Flag(int(f)) {
85 format = append(format, f)
86 }
87 }
88 var width, prec int
89 var ok bool
90 if width, ok = s.Width(); ok {
91 format = strconv.AppendInt(format, int64(width), 10)
92 }
93 if prec, ok = s.Precision(); ok {
94 format = append(format, '.')
95 format = strconv.AppendInt(format, int64(prec), 10)
96 }
97 if verb > utf8.RuneSelf {
98 format = append(format, string(verb)...)
99 } else {
100 format = append(format, byte(verb))
101 }
102 if color != 0 {
103 if v.tail != 0 {
104 // set next (previous) format clearing current one
105 format = append(format, esc...)
106 format = v.tail.appendNos(format, true)
107 format = append(format, 'm')
108 } else {
109 format = append(format, clear...) // just clear
110 }
111 }
112 fmt.Fprintf(s, string(format), v.Value.Value())
113 }
114
115 func sprintf(format interface{}, args ...interface{}) string {
116 switch ft := format.(type) {
117 case string:
118 return fmt.Sprintf(ft, args...)
119 case Value:
120 for i, v := range args {
121 if val, ok := v.(Value); ok {
122 args[i] = &tailedValue{Value: val, tail: ft.Color()}
123 continue
124 }
125 }
126 return fmt.Sprintf(ft.String(), args...)
127 }
128 // unknown type of format (we hope it's a string)
129 return fmt.Sprintf(fmt.Sprint(format), args...)
130 }