本文整理匯總了Golang中fmt.State.Width方法的典型用法代碼示例。如果您正苦於以下問題:Golang State.Width方法的具體用法?Golang State.Width怎麽用?Golang State.Width使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類fmt.State
的用法示例。
在下文中一共展示了State.Width方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Format
// Format makes Unit satisfy the fmt.Formatter interface. The unit is formatted
// with dimensions appended. If the power if the dimension is not zero or one,
// symbol^power is appended, if the power is one, just the symbol is appended
// and if the power is zero, nothing is appended. Dimensions are appended
// in order by symbol name with positive powers ahead of negative powers.
func (u *Unit) Format(fs fmt.State, c rune) {
if u == nil {
fmt.Fprint(fs, "<nil>")
}
switch c {
case 'v':
if fs.Flag('#') {
fmt.Fprintf(fs, "&%#v", *u)
return
}
fallthrough
case 'e', 'E', 'f', 'F', 'g', 'G':
p, pOk := fs.Precision()
w, wOk := fs.Width()
switch {
case pOk && wOk:
fmt.Fprintf(fs, "%*.*"+string(c), w, p, u.value)
case pOk:
fmt.Fprintf(fs, "%.*"+string(c), p, u.value)
case wOk:
fmt.Fprintf(fs, "%*"+string(c), w, u.value)
default:
fmt.Fprintf(fs, "%"+string(c), u.value)
}
default:
fmt.Fprintf(fs, "%%!%c(*Unit=%g)", c, u)
return
}
if u.formatted == "" && len(u.dimensions) > 0 {
u.formatted = u.dimensions.String()
}
fmt.Fprintf(fs, " %s", u.formatted)
}
示例2: Format
// Format may call Sprint(f) or Fprint(f) etc. to generate its output.
func (e *goof) Format(f fmt.State, c rune) {
s := e.getMessage(e.includeFieldsInFormat)
fs := &bytes.Buffer{}
fs.WriteRune('%')
if f.Flag('+') {
fs.WriteRune('+')
}
if f.Flag('-') {
fs.WriteRune('-')
}
if f.Flag('#') {
fs.WriteRune('#')
}
if f.Flag(' ') {
fs.WriteRune(' ')
}
if f.Flag('0') {
fs.WriteRune('0')
}
if w, ok := f.Width(); ok {
fs.WriteString(fmt.Sprintf("%d", w))
}
if p, ok := f.Precision(); ok {
fs.WriteString(fmt.Sprintf("%d", p))
}
fs.WriteRune(c)
fmt.Fprintf(f, fs.String(), s)
}
示例3: Format
func (m Mass) Format(fs fmt.State, c rune) {
switch c {
case 'v':
if fs.Flag('#') {
fmt.Fprintf(fs, "%T(%v)", m, float64(m))
return
}
fallthrough
case 'e', 'E', 'f', 'F', 'g', 'G':
p, pOk := fs.Precision()
w, wOk := fs.Width()
switch {
case pOk && wOk:
fmt.Fprintf(fs, "%*.*"+string(c), w, p, float64(m))
case pOk:
fmt.Fprintf(fs, "%.*"+string(c), p, float64(m))
case wOk:
fmt.Fprintf(fs, "%*"+string(c), w, float64(m))
default:
fmt.Fprintf(fs, "%"+string(c), float64(m))
}
fmt.Fprint(fs, " kg")
default:
fmt.Fprintf(fs, "%%!%c(%T=%g kg)", c, m, float64(m))
return
}
}
示例4: Format
func (st *stacktrace) Format(f fmt.State, c rune) {
var text string
if f.Flag('+') && !f.Flag('#') && c == 's' { // "%+s"
text = formatFull(st)
} else if f.Flag('#') && !f.Flag('+') && c == 's' { // "%#s"
text = formatBrief(st)
} else {
text = map[Format]func(*stacktrace) string{
FormatFull: formatFull,
FormatBrief: formatBrief,
}[DefaultFormat](st)
}
formatString := "%"
// keep the flags recognized by fmt package
for _, flag := range "-+# 0" {
if f.Flag(int(flag)) {
formatString += string(flag)
}
}
if width, has := f.Width(); has {
formatString += fmt.Sprint(width)
}
if precision, has := f.Precision(); has {
formatString += "."
formatString += fmt.Sprint(precision)
}
formatString += string(c)
fmt.Fprintf(f, formatString, text)
}
示例5: Format
func (af *ansiFormatter) Format(f fmt.State, c rune) {
// reconstruct the format string in bf
bf := new(bytes.Buffer)
bf.WriteByte('%')
for _, x := range []byte{'-', '+', '#', ' ', '0'} {
if f.Flag(int(x)) {
bf.WriteByte(x)
}
}
if w, ok := f.Width(); ok {
fmt.Fprint(bf, w)
}
if p, ok := f.Precision(); ok {
fmt.Fprintf(bf, ".%d", p)
}
bf.WriteRune(c)
format := bf.String()
if len(af.codes) == 0 {
fmt.Fprintf(f, format, af.value)
return
}
fmt.Fprintf(f, "\x1b[%d", af.codes[0])
for _, code := range af.codes[1:] {
fmt.Fprintf(f, ";%d", code)
}
f.Write([]byte{'m'})
fmt.Fprintf(f, format, af.value)
fmt.Fprint(f, "\x1b[0m")
}
示例6: Format
func (e escapable) Format(f fmt.State, c rune) {
s := "%"
for i := 0; i < 128; i++ {
if f.Flag(i) {
s += string(i)
}
}
if w, ok := f.Width(); ok {
s += fmt.Sprintf("%d", w)
}
if p, ok := f.Precision(); ok {
s += fmt.Sprintf(".%d", p)
}
// If we have an uppercase format char and a slice, format each slice
// element
if unicode.IsUpper(c) && reflect.TypeOf(e.x).Kind() == reflect.Slice {
s += strings.ToLower(string(c))
v := reflect.ValueOf(e.x)
for i := 0; i < v.Len(); i++ {
formatted := fmt.Sprintf(s, v.Index(i))
io.WriteString(f, ReadableEscapeArg(formatted))
if i+1 != v.Len() {
io.WriteString(f, " ")
}
}
return
}
s += string(c)
formatted := fmt.Sprintf(s, e.x)
io.WriteString(f, ReadableEscapeArg(formatted))
}
示例7: Format
// TODO: Implement left/right align
func (b ByteSize) Format(f fmt.State, c rune) {
var decimal bool
switch c {
case 'd':
decimal = true
case 'b':
decimal = false
case 'v':
fmt.Fprintf(f, "%s", b.String())
return
default:
fmt.Fprintf(f, "%%!%c(ByteSize=%s)", c, b.String())
return
}
unit, divisor := b.UnitDivisor(decimal)
fmtstring := "%"
if w, ok := f.Width(); ok {
fmtstring += fmt.Sprintf("%d", w-len(unit)-1)
}
if p, ok := f.Precision(); ok {
fmtstring += fmt.Sprintf(".%d", p)
}
fmtstring += "f %s"
fmt.Fprintf(f, fmtstring, float64(b)/float64(divisor), unit)
}
示例8: format
func format(b Bed, fs fmt.State, c rune) {
bv := reflect.ValueOf(b)
if bv.IsNil() {
fmt.Fprint(fs, "<nil>")
return
}
bv = bv.Elem()
switch c {
case 'v':
if fs.Flag('#') {
fmt.Fprintf(fs, "&%#v", bv.Interface())
return
}
fallthrough
case 's':
width, _ := fs.Width()
if !b.canBed(width) {
fmt.Fprintf(fs, "%%!(BADWIDTH)%T", b)
return
}
if width == 0 {
width = bv.NumField()
}
for i := 0; i < width; i++ {
f := bv.Field(i).Interface()
if i >= rgbField {
switch i {
case rgbField:
rv := reflect.ValueOf(f)
if reflect.DeepEqual(rv.Interface(), color.RGBA{}) {
fs.Write([]byte{'0'})
} else {
fmt.Fprintf(fs, "%d,%d,%d",
rv.Field(0).Interface(), rv.Field(1).Interface(), rv.Field(2).Interface())
}
case blockCountField:
fmt.Fprint(fs, f)
case blockSizesField, blockStartsField:
av := reflect.ValueOf(f)
l := av.Len()
for j := 0; j < l; j++ {
fmt.Fprint(fs, av.Index(j).Interface())
if j < l-1 {
fs.Write([]byte{','})
}
}
}
} else {
fmt.Fprint(fs, f)
}
if i < width-1 {
fs.Write([]byte{'\t'})
}
}
default:
fmt.Fprintf(fs, "%%!%c(%T=%3s)", c, b, b)
}
}
示例9: Format
// We implement fmt.Formatter interface just so we can left-align the cards.
func (s Suit) Format(f fmt.State, c int) {
x := stringTable[s]
f.Write([]byte(x))
if w, ok := f.Width(); ok {
for i := 0; i < w-len(x); i++ {
f.Write([]byte{' '})
}
}
}
示例10: passThrough
func (fo formatter) passThrough(f fmt.State, c rune) {
s := "%"
for i := 0; i < 128; i++ {
if f.Flag(i) {
s += string(i)
}
}
if w, ok := f.Width(); ok {
s += fmt.Sprintf("%d", w)
}
if p, ok := f.Precision(); ok {
s += fmt.Sprintf(".%d", p)
}
s += string(c)
fmt.Fprintf(f, s, fo.x)
}
示例11: Format
func (r Range) Format(f fmt.State, c int) {
i := byte(0)
for ; i < r.Min; i++ {
f.Write([]byte{'X'})
}
for ; float64(i)+0.5 < r.Mean; i++ {
f.Write([]byte{'x'})
}
for ; i < r.Max; i++ {
f.Write([]byte{'.'})
}
if w, ok := f.Width(); ok {
for ; i < byte(w); i++ {
f.Write([]byte{' '})
}
}
}
示例12: Format
func (e escapable) Format(f fmt.State, c rune) {
s := "%"
for i := 0; i < 128; i++ {
if f.Flag(i) {
s += string(i)
}
}
if w, ok := f.Width(); ok {
s += fmt.Sprintf("%d", w)
}
if p, ok := f.Precision(); ok {
s += fmt.Sprintf(".%d", p)
}
s += string(c)
formatted := fmt.Sprintf(s, e.x)
io.WriteString(f, ReadableEscapeArg(formatted))
}
示例13: defaultFormat
func defaultFormat(v interface{}, f fmt.State, c rune) {
buf := make([]string, 0, 10)
buf = append(buf, "%")
for i := 0; i < 128; i++ {
if f.Flag(i) {
buf = append(buf, string(i))
}
}
if w, ok := f.Width(); ok {
buf = append(buf, strconv.Itoa(w))
}
if p, ok := f.Precision(); ok {
buf = append(buf, "."+strconv.Itoa(p))
}
buf = append(buf, string(c))
format := strings.Join(buf, "")
fmt.Fprintf(f, format, v)
}
示例14: formatString
func formatString(fs fmt.State, c rune) string {
w, wOk := fs.Width()
p, pOk := fs.Precision()
var b bytes.Buffer
b.WriteByte('%')
for _, f := range "+-# 0" {
if fs.Flag(int(f)) {
b.WriteRune(f)
}
}
if wOk {
fmt.Fprint(&b, w)
}
if pOk {
b.WriteByte('.')
fmt.Fprint(&b, p)
}
b.WriteRune(c)
return b.String()
}
示例15: Format
func (x *M_Cmd) Format(f fmt.State, c int) {
if c == 'v' && f.Flag('#') && x != nil {
fmt.Fprintf(f, "M_%s", M_Cmd_name[int32(*x)])
return
}
s := "%"
for i := 0; i < 128; i++ {
if f.Flag(i) {
s += string(i)
}
}
if w, ok := f.Width(); ok {
s += fmt.Sprintf("%d", w)
}
if p, ok := f.Precision(); ok {
s += fmt.Sprintf(".%d", p)
}
s += string(c)
fmt.Fprintf(f, s, (*int32)(x))
}