code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/printer/interactive.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package printer 17 18 import ( 19 "fmt" 20 "io" 21 22 "github.com/muesli/termenv" 23 ) 24 25 type InteractivePrinter struct { 26 writer io.Writer 27 profile termenv.Profile 28 29 checkMark string 30 crossMark string 31 questionMark string 32 arrow termenv.Style 33 bangMark termenv.Style 34 } 35 36 func (p *InteractivePrinter) String() *FormattedString { 37 return &FormattedString{ 38 profile: p.profile, 39 checkMark: p.checkMark, 40 crossMark: p.crossMark, 41 questionMark: p.questionMark, 42 arrow: p.arrow, 43 bangMark: p.bangMark, 44 } 45 } 46 47 func (p *InteractivePrinter) Print(s *FormattedString) { 48 if _, err := fmt.Fprint(p.writer, s.str); err != nil { 49 panic(fmt.Sprintf("couldn't write to %v: %v", p.writer, err)) 50 } 51 } 52 53 type FormattedString struct { 54 profile termenv.Profile 55 56 checkMark string 57 crossMark string 58 questionMark string 59 arrow termenv.Style 60 bangMark termenv.Style 61 62 str string 63 } 64 65 func (s *FormattedString) GreenArrow() *FormattedString { 66 s.str += s.arrow.Foreground(s.profile.Color("2")).String() 67 return s 68 } 69 70 func (s *FormattedString) RedArrow() *FormattedString { 71 s.str += s.arrow.Foreground(s.profile.Color("1")).String() 72 return s 73 } 74 75 func (s *FormattedString) BlueArrow() *FormattedString { 76 s.str += s.arrow.Foreground(s.profile.Color("6")).String() 77 return s 78 } 79 80 func (s *FormattedString) CheckMark() *FormattedString { 81 s.str += s.checkMark 82 return s 83 } 84 85 func (s *FormattedString) ListItem() *FormattedString { 86 s.str += " " 87 return s 88 } 89 90 // Pad adds a padding that compensate the status characters. 91 // It's useful to display information on multiple lines. 92 func (s *FormattedString) Pad() *FormattedString { 93 s.str += " " 94 return s 95 } 96 97 func (s *FormattedString) WarningBangMark() *FormattedString { 98 s.str += s.bangMark.Foreground(s.profile.Color("3")).String() 99 return s 100 } 101 102 func (s *FormattedString) DangerBangMark() *FormattedString { 103 s.str += s.bangMark.Foreground(s.profile.Color("1")).String() 104 return s 105 } 106 107 func (s *FormattedString) QuestionMark() *FormattedString { 108 s.str += s.questionMark 109 return s 110 } 111 112 func (s *FormattedString) CrossMark() *FormattedString { 113 s.str += s.crossMark 114 return s 115 } 116 117 func (s *FormattedString) SuccessText(t string) *FormattedString { 118 s.str += termenv.String(t).Foreground(s.profile.Color("2")).String() 119 return s 120 } 121 122 func (s *FormattedString) InfoText(t string) *FormattedString { 123 s.str += termenv.String(t).Foreground(s.profile.Color("6")).String() 124 return s 125 } 126 127 func (s *FormattedString) WarningText(t string) *FormattedString { 128 s.str += termenv.String(t).Foreground(s.profile.Color("3")).String() 129 return s 130 } 131 132 func (s *FormattedString) DangerText(t string) *FormattedString { 133 s.str += termenv.String(t).Foreground(s.profile.Color("1")).String() 134 return s 135 } 136 137 func (s *FormattedString) NextLine() *FormattedString { 138 s.str += "\n" 139 return s 140 } 141 142 func (s *FormattedString) NextSection() *FormattedString { 143 s.str += "\n\n" 144 return s 145 } 146 147 func (s *FormattedString) Text(str string) *FormattedString { 148 s.str += str 149 return s 150 } 151 152 func (s *FormattedString) Code(str string) *FormattedString { 153 s.str += fmt.Sprintf(" $ %s", str) 154 return s 155 } 156 157 func (s *FormattedString) Bold(str string) *FormattedString { 158 s.str += termenv.String(str).Bold().String() 159 return s 160 } 161 162 func (s *FormattedString) DangerBold(str string) *FormattedString { 163 s.str += termenv.String(str).Bold().Foreground(s.profile.Color("1")).String() 164 return s 165 } 166 167 func (s *FormattedString) SuccessBold(str string) *FormattedString { 168 s.str += termenv.String(str).Bold().Foreground(s.profile.Color("2")).String() 169 return s 170 } 171 172 func (s *FormattedString) Underline(str string) *FormattedString { 173 s.str += termenv.String(str).Underline().String() 174 return s 175 }