...

Source file src/github.com/logrusorgru/aurora/v3/sprintf.go

Documentation: github.com/logrusorgru/aurora/v3

     1  //
     2  // Copyright (c) 2016-2020 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  )
    41  
    42  // Sprintf allows to use Value as format. For example
    43  //
    44  //    v := Sprintf(Red("total: +3.5f points"), Blue(3.14))
    45  //
    46  // In this case "total:" and "points" will be red, but
    47  // 3.14 will be blue. But, in another example
    48  //
    49  //    v := Sprintf(Red("total: +3.5f points"), 3.14)
    50  //
    51  // full string will be red. And no way to clear 3.14 to
    52  // default format and color
    53  func Sprintf(format interface{}, args ...interface{}) string {
    54  	switch ft := format.(type) {
    55  	case string:
    56  		return fmt.Sprintf(ft, args...)
    57  	case Value:
    58  		for i, v := range args {
    59  			if val, ok := v.(Value); ok {
    60  				args[i] = val.setTail(ft.Color())
    61  				continue
    62  			}
    63  		}
    64  		return fmt.Sprintf(ft.String(), args...)
    65  	}
    66  	// unknown type of format (we hope it's a string)
    67  	return fmt.Sprintf(fmt.Sprint(format), args...)
    68  }
    69  

View as plain text