Esempio n. 1
0
func (v value) Format(f fmt.State, c rune) {
	if debug || f.Flag('#') {
		fmt.Fprintf(f, "%s["+v.format+"]", typeName(v.v), v.v)
	} else {
		fmt.Fprintf(f, v.format, v.v)
	}
}
Esempio n. 2
0
// Format satisfies the fmt.Formatter interface.
func (f formatter) Format(fs fmt.State, c rune) {
	if c == 'v' && fs.Flag('#') {
		fmt.Fprintf(fs, "%#v", f.matrix)
		return
	}
	format(f.matrix, f.prefix, f.margin, f.dot, f.squeeze, fs, c)
}
Esempio n. 3
0
// Format is a support routine for fmt.Formatter. It accepts
// the formats 'b' (binary), 'o' (octal), 'd' (decimal), 'x'
// (lowercase hexadecimal), and 'X' (uppercase hexadecimal).
//
func (x *Int) Format(s fmt.State, ch int) {
	cs := charset(ch)

	// special cases
	switch {
	case cs == "":
		// unknown format
		fmt.Fprintf(s, "%%!%c(big.Int=%s)", ch, x.String())
		return
	case x == nil:
		fmt.Fprint(s, "<nil>")
		return
	}

	// determine format
	format := "%s"
	if s.Flag('#') {
		switch ch {
		case 'o':
			format = "0%s"
		case 'x':
			format = "0x%s"
		case 'X':
			format = "0X%s"
		}
	}
	if x.neg {
		format = "-" + format
	}

	fmt.Fprintf(s, format, x.abs.string(cs))
}
Esempio n. 4
0
func (fo formatter) Format(f fmt.State, c rune) {
	if c == 'v' && f.Flag('#') && f.Flag(' ') {
		fo.format(f)
		return
	}
	fo.passThrough(f, c)
}
Esempio n. 5
0
// Format formats the frame according to the fmt.Formatter interface.
//
//    %s    source file
//    %d    source line
//    %n    function name
//    %v    equivalent to %s:%d
//
// Format accepts flags that alter the printing of some verbs, as follows:
//
//    %+s   path of source file relative to the compile time GOPATH
//    %+v   equivalent to %+s:%d
func (f Frame) Format(s fmt.State, verb rune) {
	switch verb {
	case 's':
		switch {
		case s.Flag('+'):
			pc := f.pc()
			fn := runtime.FuncForPC(pc)
			if fn == nil {
				io.WriteString(s, "unknown")
			} else {
				file, _ := fn.FileLine(pc)
				fmt.Fprintf(s, "%s\n\t%s", fn.Name(), file)
			}
		default:
			io.WriteString(s, path.Base(f.file()))
		}
	case 'd':
		fmt.Fprintf(s, "%d", f.line())
	case 'n':
		name := runtime.FuncForPC(f.pc()).Name()
		io.WriteString(s, funcname(name))
	case 'v':
		f.Format(s, 's')
		io.WriteString(s, ":")
		f.Format(s, 'd')
	}
}
Esempio n. 6
0
func (m fm) Format(fs fmt.State, c rune) {
	if c == 'v' && fs.Flag('#') {
		fmt.Fprintf(fs, "%#v", m.Matrix)
		return
	}
	Format(m.Matrix, m.margin, '.', fs, c)
}
Esempio n. 7
0
// Format implements fmt.Formatter. It accepts format.State for
// language-specific rendering.
func (v Value) Format(s fmt.State, verb rune) {
	var lang int
	if state, ok := s.(format.State); ok {
		lang, _ = language.CompactIndex(state.Language())
	}

	// Get the options. Use DefaultFormat if not present.
	opt := v.format
	if opt == nil {
		opt = defaultFormat
	}
	cur := v.currency
	if cur.index == 0 {
		cur = opt.currency
	}

	// TODO: use pattern.
	io.WriteString(s, opt.symbol(lang, cur))
	if v.amount != nil {
		s.Write(space)

		// TODO: apply currency-specific rounding
		scale, _ := opt.kind.Rounding(cur)
		if _, ok := s.Precision(); !ok {
			fmt.Fprintf(s, "%.*f", scale, v.amount)
		} else {
			fmt.Fprint(s, v.amount)
		}
	}
}
Esempio n. 8
0
// 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)
}
Esempio n. 9
0
File: int.go Progetto: Sunmonds/gcc
// write count copies of text to s
func writeMultiple(s fmt.State, text string, count int) {
	if len(text) > 0 {
		b := []byte(text)
		for ; count > 0; count-- {
			s.Write(b)
		}
	}
}
Esempio n. 10
0
File: gff.go Progetto: gordon/biogo
func (a Attributes) Format(fs fmt.State, c rune) {
	for i, tv := range a {
		fmt.Fprintf(fs, "%s %s", tv.Tag, tv.Value)
		if i < len(a)-1 {
			fs.Write([]byte("; "))
		}
	}
}
Esempio n. 11
0
File: bed.go Progetto: gordon/biogo
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)
	}
}
Esempio n. 12
0
// Format implements the fmt.Formatter interface.
func (v val) Format(s fmt.State, c rune) {
	if c == 'v' || c == 's' {
		fprint(s, reflect.ValueOf(v.v), state{
			defaults: s.Flag('+') || s.Flag('#'),
		})
	} else {
		fmt.Fprintf(s, "%%!%c(pretty.Val)", c)
	}
}
Esempio n. 13
0
func (fo formatter) Format(f fmt.State, c rune) {
	if fo.force || c == 'v' && f.Flag('#') && f.Flag(' ') {
		w := tabwriter.NewWriter(f, 4, 4, 1, ' ', 0)
		p := &printer{tw: w, Writer: w, visited: make(map[visit]int)}
		p.printValue(reflect.ValueOf(fo.x), true, fo.quote)
		w.Flush()
		return
	}
	fo.passThrough(f, c)
}
Esempio n. 14
0
func (e _error) Format(s fmt.State, verb rune) {
	switch verb {
	case 'v':
		if s.Flag('+') {
			fmt.Fprintf(s, "%+v: ", e.Stacktrace()[0])
		}
		fallthrough
	case 's':
		io.WriteString(s, e.msg)
	}
}
Esempio n. 15
0
func (s *stack) Format(st fmt.State, verb rune) {
	switch verb {
	case 'v':
		switch {
		case st.Flag('+'):
			for _, pc := range *s {
				f := Frame(pc)
				fmt.Fprintf(st, "\n%+v", f)
			}
		}
	}
}
Esempio n. 16
0
func (f *fundamental) Format(s fmt.State, verb rune) {
	switch verb {
	case 'v':
		if s.Flag('+') {
			io.WriteString(s, f.msg)
			f.stack.Format(s, verb)
			return
		}
		fallthrough
	case 's', 'q':
		io.WriteString(s, f.msg)
	}
}
Esempio n. 17
0
func (w wrapper) Format(s fmt.State, verb rune) {
	switch verb {
	case 'v':
		if s.Flag('+') {
			fmt.Fprintf(s, "%+v\n", w.Cause())
			fmt.Fprintf(s, "%+v: %s", w.Stacktrace()[0], w.msg)
			return
		}
		fallthrough
	case 's':
		io.WriteString(s, w.Error())
	}
}
Esempio n. 18
0
func (w *withMessage) Format(s fmt.State, verb rune) {
	switch verb {
	case 'v':
		if s.Flag('+') {
			fmt.Fprintf(s, "%+v\n", w.Cause())
			io.WriteString(s, w.msg)
			return
		}
		fallthrough
	case 's', 'q':
		io.WriteString(s, w.Error())
	}
}
Esempio n. 19
0
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")
}
Esempio n. 20
0
func (w *withStack) Format(s fmt.State, verb rune) {
	switch verb {
	case 'v':
		if s.Flag('+') {
			fmt.Fprintf(s, "%+v", w.Cause())
			w.stack.Format(s, verb)
			return
		}
		fallthrough
	case 's':
		io.WriteString(s, w.Error())
	case 'q':
		fmt.Fprintf(s, "%q", w.Error())
	}
}
Esempio n. 21
0
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{' '})
		}
	}
}
Esempio n. 22
0
func (f *formatter) formatStruct(s fmt.State, c rune, val reflect.Value) {
	f.depth++
	if f.verbose {
		writeType(s, val)
		writeLeftcurly(s)
		if f.pretty {
			writeNewline(s)
			writeFullIndent(s, f.depth)
		}
	} else {
		writeLeftcurly(s)
	}
	typ := val.Type()
	for i, n := 0, val.NumField(); i < n; i++ {
		field := typ.Field(i)
		if i > 0 {
			f.sep(s)
		}
		if f.verbose || f.extra {
			s.Write([]byte(field.Name))
			writeColon(s)
		}
		if field.PkgPath == "" {
			f.format(s, c, val.Field(i))
		} else {
			field := typ.Field(i)
			var valptr reflect.Value
			if val.CanAddr() {
				valptr = val.Addr()
			} else {
				valptr = reflect.New(typ)
				reflect.Indirect(valptr).Set(val)
			}
			fieldp := valptr.Pointer() + field.Offset
			fieldptr := reflect.NewAt(field.Type, unsafe.Pointer(fieldp))
			f.format(s, c, reflect.Indirect(fieldptr))
		}
	}
	f.depth--
	if f.verbose && f.pretty {
		writeComma(s)
		writeNewline(s)
		writeFullIndent(s, f.depth)
	}
	writeRightcurly(s)
}
Esempio n. 23
0
func (st StackTrace) Format(s fmt.State, verb rune) {
	switch verb {
	case 'v':
		switch {
		case s.Flag('+'):
			for _, f := range st {
				fmt.Fprintf(s, "\n%+v", f)
			}
		case s.Flag('#'):
			fmt.Fprintf(s, "%#v", []Frame(st))
		default:
			fmt.Fprintf(s, "%v", []Frame(st))
		}
	case 's':
		fmt.Fprintf(s, "%s", []Frame(st))
	}
}
Esempio n. 24
0
func (f *formatter) Format(s fmt.State, c rune) {
	if c != 'v' || (!f.deep && !f.pretty && f.ifaceok && !f.debug) {
		// default printf
		newfmt := reconstructFlags(s, c)
		fmt.Fprintf(s, newfmt, f.v)
		return
	}
	if f.v == nil {
		writeNilangle(s)
		return
	}

	f.verbose = s.Flag('#')
	f.extra = s.Flag('+')
	f.space = s.Flag(' ')
	f.format(s, c, reflect.ValueOf(f.v))
}
Esempio n. 25
0
// Format implements fmt.Formatter with support for the following verbs.
//
//    %s    source file
//    %d    line number
//    %n    function name
//    %v    equivalent to %s:%d
//
// It accepts the '+' and '#' flags for most of the verbs as follows.
//
//    %+s   path of source file relative to the compile time GOPATH
//    %#s   full path of source file
//    %+n   import path qualified function name
//    %+v   equivalent to %+s:%d
//    %#v   equivalent to %#s:%d
func (c Call) Format(s fmt.State, verb rune) {
	if c.fn == nil {
		fmt.Fprintf(s, "%%!%c(NOFUNC)", verb)
		return
	}

	switch verb {
	case 's', 'v':
		file, line := c.fn.FileLine(c.pc)
		switch {
		case s.Flag('#'):
			// done
		case s.Flag('+'):
			file = file[pkgIndex(file, c.fn.Name()):]
		default:
			const sep = "/"
			if i := strings.LastIndex(file, sep); i != -1 {
				file = file[i+len(sep):]
			}
		}
		io.WriteString(s, file)
		if verb == 'v' {
			buf := [7]byte{':'}
			s.Write(strconv.AppendInt(buf[:1], int64(line), 10))
		}

	case 'd':
		_, line := c.fn.FileLine(c.pc)
		buf := [6]byte{}
		s.Write(strconv.AppendInt(buf[:0], int64(line), 10))

	case 'n':
		name := c.fn.Name()
		if !s.Flag('+') {
			const pathSep = "/"
			if i := strings.LastIndex(name, pathSep); i != -1 {
				name = name[i+len(pathSep):]
			}
			const pkgSep = "."
			if i := strings.Index(name, pkgSep); i != -1 {
				name = name[i+len(pkgSep):]
			}
		}
		io.WriteString(s, name)
	}
}
Esempio n. 26
0
// Format outputs a pretty-printed Bytes.
func (t Bytes) Format(out fmt.State, verb rune) {
	if out.Flag('#') {
		// use alternate format: base64w
		s := base64.URLEncoding.EncodeToString([]byte(t))
		if verb == 's' && len(string(t)) > ElisionCutoff {
			fmt.Fprintf(out, "{%s...}", s[:ElisionLength])
		} else {
			fmt.Fprintf(out, "{%s}", s)
		}
	} else {
		// use default format: hex
		if verb == 's' && len(string(t)) > ElisionCutoff {
			fmt.Fprintf(out, "[%02x...]", []byte(t)[:ElisionLength])
		} else {
			fmt.Fprintf(out, "[%02x]", []byte(t))
		}
	}
}
Esempio n. 27
0
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
	}
}
Esempio n. 28
0
// Format is a support routine for fmt.Formatter. It accepts the formats 'v' and 's'
// (string), 'a' (fasta) and 'q' (fastq). String, fasta and fastq formats support
// truncated output via the verb's precision. Fasta format supports sequence line
// specification via the verb's width field. Fastq format supports optional inclusion
// of the '+' line descriptor line with the '+' flag. The 'v' verb supports the '#'
// flag for Go syntax output. The 's' and 'v' formats support the '-' flag for
// omission of the sequence name and in conjunction with this, the ' ' flag for
// alignment justification.
func (m *Multi) Format(fs fmt.State, c rune) {
	if m == nil {
		fmt.Fprint(fs, "<nil>")
		return
	}

	var align bool
	switch c {
	case 'v':
		if fs.Flag('#') {
			fmt.Fprintf(fs, "&%#v", *m)
			return
		}
		fallthrough
	case 's':
		align = fs.Flag(' ') && fs.Flag('-')
		fallthrough
	case 'a', 'q':
		format := formatString(fs, c)
		for i, r := range m.Seq {
			if align {
				fmt.Fprintf(fs, "%s", strings.Repeat(" ", r.Start()-m.Start()))
			}
			fmt.Fprintf(fs, format, r)
			if i < m.Rows()-1 {
				fmt.Fprintln(fs)
			}
		}
	default:
		fmt.Fprintf(fs, "%%!%c(*multi.Multi=%.10s)", c, m)
	}
}
Esempio n. 29
0
// 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)
}
Esempio n. 30
0
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))
}