github.com/goki/ki@v1.1.11/indent/indent.go (about) 1 // Copyright (c) 2018, The GoKi Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 /* 6 Package indent provides trivial indentation generation methods: Tabs, 7 Spaces, and Indent with a selector. Also bytes versions. 8 Just for clarity, simplicity. 9 */ 10 package indent 11 12 import ( 13 "bytes" 14 "strings" 15 ) 16 17 // Char is the type of indentation character to use 18 type Char int 19 20 const ( 21 // Tab uses tab for indentation 22 Tab Char = iota 23 24 // Space uses spaces for indentation 25 Space 26 ) 27 28 // Tabs returns a string of n tabs 29 func Tabs(n int) string { 30 return strings.Repeat("\t", n) 31 } 32 33 // TabBytes returns []byte of n tabs 34 func TabBytes(n int) []byte { 35 return bytes.Repeat([]byte("\t"), n) 36 } 37 38 // Spaces returns a string of n*width spaces 39 func Spaces(n, width int) string { 40 return strings.Repeat(" ", n*width) 41 } 42 43 // SpaceBytes returns a []byte of n*width spaces 44 func SpaceBytes(n, width int) []byte { 45 return bytes.Repeat([]byte(" "), n*width) 46 } 47 48 // String returns a string of n tabs or n*width spaces depending on the indent char 49 func String(ich Char, n, width int) string { 50 if ich == Tab { 51 return Tabs(n) 52 } 53 return Spaces(n, width) 54 } 55 56 // Bytes returns []byte of n tabs or n*width spaces depending on the indent char 57 func Bytes(ich Char, n, width int) []byte { 58 if ich == Tab { 59 return TabBytes(n) 60 } 61 return SpaceBytes(n, width) 62 } 63 64 // Len returns the length of the indent string given indent char and indent level 65 func Len(ich Char, n, width int) int { 66 if ich == Tab { 67 return n 68 } 69 return n * width 70 }