github.com/swaros/contxt/module/taskrun@v0.0.0-20240305083542-3dbd4436ac40/labelprint.go (about) 1 // MIT License 2 // 3 // Copyright (c) 2020 Thomas Ziegler <thomas.zglr@googlemail.com>. All rights reserved. 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the Software), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 23 // AINC-NOTE-0815 24 25 package taskrun 26 27 import ( 28 "fmt" 29 30 "github.com/swaros/contxt/module/systools" 31 "github.com/swaros/manout" 32 ) 33 34 var PreHook func(msg ...interface{}) bool = nil 35 36 type CtxOutCtrl struct { 37 IgnoreCase bool 38 } 39 40 type CtxOutLabel struct { 41 Message interface{} 42 FColor string 43 } 44 45 type CtxTargetOut struct { 46 ForeCol string 47 BackCol string 48 SplitLabel string 49 Target string 50 Alternative string 51 PanelSize int 52 } 53 54 func CtxOut(msg ...interface{}) { 55 if PreHook != nil { // if the prehook is defined AND it returns true, we just stop doing anything 56 if abort := PreHook(msg...); abort { 57 return 58 } 59 } 60 var newMsh []interface{} 61 for _, chk := range msg { 62 switch ctrl := chk.(type) { 63 case CtxOutCtrl: 64 if chk.(CtxOutCtrl).IgnoreCase { // if we have found this flag set to true, it means ignore the message 65 return 66 } 67 case CtxTargetOut: 68 labelStr := "" 69 if ctrl.Alternative != "" { 70 labelStr = systools.LabelPrint(ctrl.Alternative, 1) 71 } else { 72 labelStr = systools.LabelPrintWithArg(systools.PadStringToR(ctrl.Target, ctrl.PanelSize), ctrl.ForeCol, ctrl.BackCol, 1) 73 } 74 newMsh = append(newMsh, labelStr) 75 case CtxOutLabel: 76 colmsg := manout.Message(ctrl.FColor, ctrl.Message) + " " 77 newMsh = append(newMsh, colmsg) 78 default: 79 newMsh = append(newMsh, chk) 80 } 81 82 } 83 msg = newMsh 84 fmt.Println(manout.MessageCln(msg...)) 85 } 86 87 func ValF(val interface{}) CtxOutLabel { 88 return CtxOutLabel{Message: val, FColor: manout.ForeLightBlue} 89 } 90 91 func InfoF(val interface{}) CtxOutLabel { 92 return CtxOutLabel{Message: val, FColor: manout.ForeCyan} 93 } 94 95 func InfoRed(val interface{}) CtxOutLabel { 96 return CtxOutLabel{Message: val, FColor: manout.ForeLightRed} 97 } 98 99 func InfoMinor(val interface{}) CtxOutLabel { 100 return CtxOutLabel{Message: val, FColor: manout.ForeDarkGrey} 101 } 102 103 func defaultLabel(val interface{}) string { 104 return fmt.Sprintf(" %10s", val) 105 } 106 107 func LabelFY(val interface{}) CtxOutLabel { 108 ctxl := CtxOutLabel{Message: val, FColor: manout.ForeYellow} 109 ctxl.Message = manout.Message("[", manout.ForeYellow, defaultLabel(val), manout.ForeLightYellow, "] ") 110 return ctxl 111 } 112 113 func LabelOkF(val interface{}) CtxOutLabel { 114 ctxl := CtxOutLabel{Message: val, FColor: manout.ForeLightGreen} 115 ctxl.Message = manout.Message("[", manout.ForeYellow, defaultLabel(val), manout.ForeLightYellow, "] ") 116 return ctxl 117 } 118 119 func LabelErrF(val interface{}) CtxOutLabel { 120 ctxl := CtxOutLabel{Message: val, FColor: manout.ForeRed} 121 ctxl.Message = manout.Message("[", manout.ForeYellow, defaultLabel(val), manout.ForeLightYellow, "] ") 122 return ctxl 123 }