bitbucket.org/ai69/amoy@v0.2.3/style.go (about)

     1  package amoy
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/1set/gut/ystring"
     7  	"github.com/muesli/termenv"
     8  )
     9  
    10  var (
    11  	term            = termenv.ColorProfile()
    12  	lineBreakLength = 100
    13  
    14  	// Simple style functions
    15  	StyleIndex     = makeFlexFgStyle("238", "250")
    16  	StyleMessage   = makeFgStyle("200")
    17  	StyleName      = makeFgStyle("207")
    18  	StyleDate      = makeFgStyle("82")
    19  	StyleHighlight = makeFgStyle("227")
    20  	StyleLabel     = makeFgStyle("51")
    21  	StyleAmount    = makeFgStyle("207")
    22  	StyleDot       = colorFg("•", "236")
    23  	StyleDash      = colorFg("-", "236")
    24  	StyleLineBreak = colorFg(strings.Repeat("█", lineBreakLength), "246")
    25  )
    26  
    27  var (
    28  	styleUserName = makeFgStyle("160")
    29  	styleAt       = colorFg("@", "123")
    30  	styleDomain   = makeFgStyle("199")
    31  )
    32  
    33  // StyleLabeledLineBreak renders a line break with a label in the middle.
    34  func StyleLabeledLineBreak(label string) string {
    35  	maxLen := lineBreakLength - 3*2 // for label with max length like "=[ LABEL ]="
    36  	if len(label) > maxLen {
    37  		label = TruncateStr(label, maxLen)
    38  	}
    39  	labelLen := ystring.Length(label)
    40  	leftPadLen := (lineBreakLength - labelLen - 4) / 2
    41  	rightPadLen := lineBreakLength - labelLen - 4 - leftPadLen
    42  	return strings.Repeat("=", leftPadLen) + "[ " + label + " ]" + strings.Repeat("=", rightPadLen)
    43  }
    44  
    45  // StyleEmail renders a string of email with terminal colors.
    46  func StyleEmail(email string) string {
    47  	if parts := strings.Split(email, "@"); len(parts) >= 2 {
    48  		return styleUserName(parts[0]) + styleAt + styleDomain(parts[1])
    49  	}
    50  	return styleUserName(email)
    51  }
    52  
    53  // StyleBold renders a string in bold.
    54  func StyleBold(val string) string {
    55  	return termenv.String(val).Bold().String()
    56  }
    57  
    58  // StyleCrossOut renders a string with cross-out.
    59  func StyleCrossOut(val string) string {
    60  	return termenv.String(val).CrossOut().String()
    61  }
    62  
    63  // Color a string's foreground with the given value.
    64  func colorFg(val, color string) string {
    65  	return termenv.String(val).Foreground(term.Color(color)).String()
    66  }
    67  
    68  // Return a function that will colorize the foreground of a given string.
    69  func makeFgStyle(color string) func(string) string {
    70  	return termenv.Style{}.Foreground(term.Color(color)).Styled
    71  }
    72  
    73  // Return a function that will colorize the foreground of a given string.
    74  func makeBgStyle(color string) func(string) string {
    75  	return termenv.Style{}.Background(term.Color(color)).Styled
    76  }
    77  
    78  // Return a function that will colorize the foreground of a given string.
    79  func makeFlexFgStyle(colorL, colorD string) func(string) string {
    80  	var color string
    81  	if termenv.HasDarkBackground() {
    82  		color = colorD
    83  	} else {
    84  		color = colorL
    85  	}
    86  	return termenv.Style{}.Foreground(term.Color(color)).Styled
    87  }
    88  
    89  // Color a string's foreground and background with the given value.
    90  func makeFgBgStyle(fg, bg string) func(string) string {
    91  	return termenv.Style{}.
    92  		Foreground(term.Color(fg)).
    93  		Background(term.Color(bg)).
    94  		Styled
    95  }
    96  
    97  func makeFlexFgBgStyle(fg, bgL, bgD string) func(string) string {
    98  	var bg string
    99  	if termenv.HasDarkBackground() {
   100  		bg = bgD
   101  	} else {
   102  		bg = bgL
   103  	}
   104  	return termenv.Style{}.
   105  		Foreground(term.Color(fg)).
   106  		Background(term.Color(bg)).
   107  		Styled
   108  }