keruch

Go

Sep 18th, 2021 (edited)
598
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. type Color int
  6. type CharStyle int
  7. type SandGlassMod func(size int, char rune, str string) (int, rune, string)
  8.  
  9. const (
  10.     Black Color = iota
  11.     Red
  12.     Green
  13.     Brown
  14.     Blue
  15.     Purple
  16.     Cyan
  17.     Gray
  18. )
  19.  
  20. const (
  21.     Normal CharStyle = iota
  22.     Bold
  23.     Underlined
  24.     Blinking
  25.     Reverse
  26. )
  27.  
  28. func printSandGlass(mods ...SandGlassMod) {
  29.     // default values for size and char
  30.     size, char := 8, '#'
  31.  
  32.     // apply functions to size and char
  33.     for _, mod := range mods {
  34.         size, char, _ = mod(size, char, "")
  35.     }
  36.  
  37.     for lineNumber := 0; lineNumber < size; lineNumber++ {
  38.         for columnNumber := 0; columnNumber < size; columnNumber++ {
  39.             var symbol rune
  40.             if (lineNumber == 0 || lineNumber == size-1) ||
  41.                 (columnNumber == lineNumber || columnNumber == size-lineNumber-1) {
  42.                 symbol = char
  43.             } else {
  44.                 symbol = ' '
  45.             }
  46.             formatString := string(symbol)
  47.  
  48.             // apply functions to format string
  49.             for _, mod := range mods {
  50.                 _, _, formatString = mod(0, 0, formatString)
  51.             }
  52.             fmt.Print(formatString)
  53.         }
  54.         fmt.Println()
  55.     }
  56. }
  57.  
  58. func main() {
  59.     printSandGlass(setSandGlassChar('H'), setSandGlassSize(5),
  60.         setCharStyle(Reverse), setBackgroundColor(Red), setForegroundColor(Blue))
  61. }
  62.  
  63. func setForegroundColor(color Color) SandGlassMod {
  64.     color += 30
  65.     return func(size int, char rune, s string) (int, rune, string) {
  66.         return size, char, fmt.Sprintf("\033[%dm%s\033[0m", color, s)
  67.     }
  68. }
  69.  
  70. func setBackgroundColor(color Color) SandGlassMod {
  71.     color += 40
  72.     return func(size int, char rune, s string) (int, rune, string) {
  73.         return size, char, fmt.Sprintf("\033[%dm%s\033[0m", color, s)
  74.     }
  75. }
  76.  
  77. func setCharStyle(style CharStyle) SandGlassMod {
  78.     return func(size int, char rune, s string) (int, rune, string) {
  79.         return size, char, fmt.Sprintf("\033[%dm%s\033[0m", style, s)
  80.     }
  81. }
  82.  
  83. func setSandGlassSize(inputSize int) SandGlassMod {
  84.     return func(size int, char rune, s string) (int, rune, string) {
  85.         return inputSize, char, s
  86.     }
  87. }
  88.  
  89. func setSandGlassChar(inputChar rune) SandGlassMod {
  90.     return func(size int, char rune, s string) (int, rune, string) {
  91.         return size, inputChar, s
  92.     }
  93. }
RAW Paste Data