예제 #1
0
파일: console.go 프로젝트: bbuck/dragon-mud
// Println prints the text followed by a trailing newline processing color codes.
func (c *Console) Println(text interface{}) {
	var str string
	switch value := text.(type) {
	case string:
		str = value
	case fmt.Stringer:
		str = value.String()
	case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
		str = fmt.Sprintf("%d", text)
	case float32, float64:
		str = fmt.Sprintf("%f", text)
	default:
		str = fmt.Sprintf("%v", text)
	}

	fmt.Fprintf(c.writer, "%s\n", color.Colorize(str))
}
예제 #2
0
파일: console.go 프로젝트: bbuck/dragon-mud
// Printf will print the string with a format to the console processing
// color codes.
func (c *Console) Printf(format string, params ...interface{}) {
	text := fmt.Sprintf(format, params...)
	fmt.Fprintf(c.writer, "%s", color.Colorize(text))
}
예제 #3
0
파일: console.go 프로젝트: bbuck/dragon-mud
// Write makes Console conform to io.Writer and can therefore be used as a
// logger target.
func (c *Console) Write(p []byte) (n int, err error) {
	str := color.Colorize(string(p))
	_, err = c.writer.Write([]byte(str))

	return len(p), err
}
예제 #4
0
var _ = Describe("Console", func() {
	var (
		console *Console
		buffer  *bytes.Buffer
		str     = "this is text"
	)

	BeforeEach(func() {
		buffer = new(bytes.Buffer)
		console = NewConsole(buffer)
	})

	Context("colored text", func() {
		var (
			coloredStr = "{red}this is {green}colored{reset} text"
			result     = color.Colorize(coloredStr)
		)

		Describe("Println", func() {
			It("processes colors and then prints", func() {
				console.Println(coloredStr)
				Ω(buffer.String()).Should(Equal(result + "\n"))
			})

			It("accepts an fmt.Stringer", func() {
				stringer := testStringer(coloredStr)
				console.Println(stringer)
				Ω(buffer.String()).Should(Equal(result + "\n"))
			})

			Context("arbitrary values", func() {