Best K6 code snippet using cmd.printBar
console.go
Source:console.go
...197 return198 }199 c.lastCenter = txt200 if cvars.ConsoleLogCenterPrint.Bool() {201 c.printBar()202 c.centerPrint(txt) // '\n' is not needed as centerPrint adds it203 c.printBar()204 // clear the notify times to make sure the txt is not shown205 // twice: in the center and in the notification line at the206 // top of the screen.207 c.ClearNotify()208 }209}210const (211 centerPrintWhitespace = " " // 20x 'x20'212)213func (c *qconsole) centerPrint(txt string) {214 w := 40215 if w > c.lineWidth {216 w = c.lineWidth217 }218 parts := strings.Split(txt, "\\n")219 // Split removes the '\n' so we can not forget to add it again.220 // Its probably ok to use Split and create new strings afterwards221 // as we add whitespace in most cases. The special case where we222 // could avoid a new string should be rare.223 for _, p := range parts {224 l := len(p)225 if l < w {226 wl := (w - l) / 2227 c.Print(centerPrintWhitespace[:wl] + p + "\n")228 } else {229 c.Print(txt + "\n")230 }231 }232}233func (c *qconsole) Print(txt string) {234 if !c.initialized {235 return236 }237 if cls.state == ca_dedicated {238 // no graphics mode239 return240 }241 c.print(txt)242 if cls.signon != 4 && !screen.disabled {243 if !printRecursionProtection {244 printRecursionProtection = true245 screen.Update()246 printRecursionProtection = false247 }248 }249}250func (c *qconsole) DrawNotify() {251 qCanvas.Set(CANVAS_CONSOLE)252 y := c.height253 lines := 0254 delta := float64(cvars.ConsoleNotifyTime.Value()) // #sec to display255 for _, t := range c.times {256 diff := time.Since(t).Seconds()257 if diff < delta {258 lines++259 }260 }261 l := len(c.origText)262 for lines > 0 {263 DrawStringWhite(8, y, c.origText[l-lines])264 y += 8265 lines--266 screen.tileClearUpdates = 0267 }268 // TODO(therjak): add missing chat functionality again269}270func (c *qconsole) Draw(lines int) {271 // TODO(therjak): add line break functionality and respect '\n'272 // i.a. do not draw origText but a derived version273 if lines <= 0 {274 return275 }276 c.visibleLines = lines * c.height / screen.Height277 qCanvas.Set(CANVAS_CONSOLE)278 DrawConsoleBackground()279 rows := (c.visibleLines + 7) / 8280 y := c.height - rows*8281 rows -= 2 // for input and version line282 sb := 0283 if c.backScroll != 0 {284 sb = 2285 }286 //for i := c.current - rows + 1; i <= c.current-sb; i++ {287 for i := len(c.origText) - rows; i < len(c.origText)-sb; i++ {288 j := i - c.backScroll289 if j < 0 {290 y += 8291 continue292 //j = 0293 }294 // draw the actual text295 DrawStringWhite(8, y, c.origText[j])296 y += 8297 }298 if c.backScroll != 0 {299 y += 8 // blank line300 nx := 8301 for i := 0; i < c.lineWidth; i++ {302 DrawCharacterWhite(nx, y, int('^'))303 nx += 8304 }305 }306 c.DrawInput()307 version := fmt.Sprintf("QuakeSpasm %1.2f.%d", QUAKESPASM_VERSION, QUAKESPASM_VER_PATCH)308 y += 8309 x := (c.lineWidth - len(version) + 2) * 8310 DrawStringWhite(x, y, version)311}312func (c *qconsole) DrawInput() {313 if keyDestination != keys.Console && !c.forceDuplication {314 return315 }316 // TODO(therjak): some kind of scrolling in case of len(keyInput.text > lineWidth317 DrawStringWhite(8, c.height-16, keyInput.String())318 // TODO(therjak): cursor blinking319 // depending on con_cursorspeed and key_blinktime320 DrawPicture(8+keyInput.cursorXPos*8, c.height-16, keyInput.Cursor())321}322// Con_Print323func (q *qconsole) print(txt string) {324 if len(txt) < 1 {325 return326 }327 switch txt[0] {328 case 1:329 localSound("misc/talk.wav")330 fallthrough331 case 2:332 // make the string copper color333 b := []byte(txt[1:])334 for i := 0; i < len(b); i++ {335 if b[i] != '\n' {336 b[i] = b[i] | 128337 }338 }339 txt = string(b)340 }341 var a []string342 for {343 // TODO(therjak): why do we need to check for \r?344 // if yes, change to IndexAny(txt, "\n\r")345 m := strings.Index(txt, "\n")346 if m < 0 {347 break348 }349 a = append(a, txt[:m+1]) // Dropping the '\n' would break the code below350 txt = txt[m+1:]351 }352 if len(txt) > 0 {353 a = append(a, txt)354 }355 times := 0356 // FIXME(therjak): We are only allowed to make a line break if we find357 // a '\n'. Otherwise we break text output originating from the vm.358 // This also means we need to verify we did not remove a '\n' in359 // conlog.Print360 ol := len(q.origText)361 if ol == 0 || strings.HasSuffix(q.origText[ol-1], "\n") {362 q.origText = append(q.origText, a...)363 times = len(a)364 } else {365 q.origText[ol-1] = q.origText[ol-1] + a[0]366 q.origText = append(q.origText, a[1:]...)367 times = len(a[1:]) // only add a time if actually a new line was added368 }369 t := time.Now()370 newTimes := q.times[:]371 for i := 0; i < times; i++ {372 newTimes = append(newTimes, t)373 }374 copy(q.times[:], newTimes[len(newTimes)-4:])375}376//do not use. use conlog.Printf377func conPrintf(format string, v ...interface{}) {378 s := fmt.Sprintf(format, v...)379 log.Print(s)380 console.Print(s)381}382//do not use. use conlog.SafePrintf383func conSafePrintf(format string, v ...interface{}) {384 tmp := screen.disabled385 screen.disabled = true386 s := fmt.Sprintf(format, v...)387 log.Print(s)388 console.Print(s)389 screen.disabled = tmp390}391func init() {392 conlog.SetPrintf(conPrintf)393 conlog.SetSafePrintf(conSafePrintf)394}395const (396 // 40 chars, starts with 1d, ends with 1f, 1e between397 quakeBar = "\x1d\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1f\n"398)399func (c *qconsole) printBar() {400 // TODO(therjak): we need a conlog.PrintBar401 if c.lineWidth >= len(quakeBar) {402 conlog.Printf(quakeBar)403 } else {404 var b strings.Builder405 b.WriteByte('\x1d')406 for i := 2; i < console.lineWidth; i++ {407 b.WriteByte('\x1e')408 }409 b.WriteByte('\x1f')410 b.WriteByte('\n')411 conlog.Printf(b.String())412 }413}...
outputport.go
Source:outputport.go
1package uiadapter2import (3 "errors"4 "regexp"5 "strings"6 "unicode/utf8"7 attr "github.com/mzki/erago/attribute"8 "github.com/mzki/erago/width"9)10// outputport implemnts a part of flow.GameController.11// It modifies and parses a text to output.12type outputPort struct {13 syncer *lineSyncer14 UI15}16func newOutputPort(ui UI, ls *lineSyncer) *outputPort {17 return &outputPort{18 syncer: ls,19 UI: ui,20 }21}22const patternString = `\[\s*(\-?[0-9]+)\s*\][\sã]?[^\[\n]+`23var buttonPattern = regexp.MustCompile(patternString)24// Print text s or print button if text s represents button pattern.25// Given text s must end "\n" or contain no "\n".26func (p outputPort) parsePrint(s string) error {27 for len(s) > 0 {28 loc := buttonPattern.FindStringSubmatchIndex(s)29 if loc == nil {30 return p.UI.Print(s)31 }32 i, j := loc[0], loc[1]33 cmd := s[loc[2]:loc[3]]34 if i > 0 {35 if err := p.UI.Print(s[:i]); err != nil {36 return err37 }38 }39 if err := p.UI.PrintButton(s[i:j], cmd); err != nil {40 return err41 }42 s = s[j:]43 }44 return nil45}46// =======================47// --- API for flow.Printer ---48// =======================49func (p outputPort) Print(s string) error {50 return p.printInternal(true, s)51}52func (p outputPort) printInternal(parseButton bool, s string) error {53 for len(s) > 0 {54 var lineSyncRequest = true55 // extract text for each line.56 i := 1 + strings.Index(s, "\n")57 if i == 0 {58 // "\n" is not found59 i = len(s)60 lineSyncRequest = false61 }62 // print63 var err error = nil64 if parseButton {65 err = p.parsePrint(s[:i])66 } else {67 err = p.UI.Print(s[:i])68 }69 if err != nil {70 return err71 }72 // skip processed text73 s = s[i:]74 // synchronize output result either "\n" is appeared or not75 if lineSyncRequest {76 err = p.syncer.SyncLine()77 } else {78 err = p.syncer.SyncText()79 }80 if err != nil {81 return err82 }83 }84 return nil85}86func (p outputPort) withView(vname string, fn func()) error {87 currentName := p.UI.GetCurrentViewName()88 if err := p.UI.SetCurrentView(vname); err != nil {89 return err90 }91 fn()92 p.UI.SetCurrentView(currentName)93 return nil94}95// print string and parse button automatically96func (p outputPort) VPrint(vname, s string) error {97 err := p.withView(vname, func() {98 p.Print(s)99 })100 return err101}102func (p outputPort) PrintL(s string) error {103 return p.Print(s + "\n")104}105// print string + "\n" and parse button automatically106func (p outputPort) VPrintL(vname, s string) error {107 err := p.withView(vname, func() {108 p.PrintL(s)109 })110 return err111}112// print text with padding space to fill at least having the width.113// e.g. text "AAA" with width 5 is "AAA ".114// But width of multibyte character is counted as 2, while that of single byte character is 1.115// If the text expresses button pattern, the entire text is teasted as Button.116// The text after "\n" is ignored.117func (p outputPort) PrintC(s string, w int) error {118 i := strings.Index(s, "\n")119 if i < 0 {120 i = len(s)121 }122 s = s[:i]123 // padding space to fill width w.124 if space := w - width.StringWidth(s); space > 0 {125 s += strings.Repeat(" ", space)126 }127 var err error = nil128 if loc := buttonPattern.FindStringSubmatchIndex(s); loc != nil {129 err = p.UI.PrintButton(s, s[loc[2]:loc[3]])130 } else {131 err = p.UI.PrintLabel(s)132 }133 if err != nil {134 return err135 }136 return p.syncer.SyncText()137}138// print string.139// it also parses button automatically.140func (p outputPort) VPrintC(vname, s string, width int) error {141 err := p.withView(vname, func() {142 p.PrintC(s, width)143 })144 return err145}146// print text without parsing button pattern.147func (p outputPort) PrintPlain(s string) error {148 return p.printInternal(false, s)149}150// print plain text. no parse button151func (p outputPort) VPrintPlain(vname, s string) error {152 err := p.withView(vname, func() {153 p.PrintPlain(s)154 })155 return err156}157func (p outputPort) VPrintButton(vname, caption, cmd string) error {158 err := p.withView(vname, func() {159 p.PrintButton(caption, cmd)160 })161 return err162}163func buildTextBar(now, max int64, w int, fg, bg string) string {164 w -= 2 // remove frame width, ASCII characters "[" and "]".165 if w <= 0 {166 return "[]"167 }168 // check fg and bg is valid utf8 symbol.169 fg_r, _ := utf8.DecodeRuneInString(fg)170 if fg_r == utf8.RuneError {171 panic("buildTextBar: invalid utf8 string for fg")172 }173 bg_r, _ := utf8.DecodeRuneInString(bg)174 if bg_r == utf8.RuneError {175 panic("buildTextBar: invalid utf8 string for bg")176 }177 now_w := int(float64(w) * float64(now) / float64(max))178 fg_w := width.RuneWidth(fg_r)179 if fg_w == 0 {180 panic("TextBar: zero width fg character")181 }182 result := "[" + strings.Repeat(string(fg_r), now_w/fg_w)183 rest_w := w - now_w184 if rest_w == 0 {185 return result + "]"186 }187 bg_w := width.RuneWidth(bg_r)188 if bg_w == 0 {189 panic("TextBar: zero width bg character")190 }191 result += strings.Repeat(string(bg_r), rest_w/bg_w) + "]"192 // TODO: if rest_w is odd and bg is a multibyte character,193 // returned bar's width is w-1. it should be handled?194 return result195}196// print text bar with current value now, maximum value max, bar's width w,197// bar's symbol fg, and background symbol bg.198// For example, now=3, max=10, w=5, fg='#', and bg='.' then199// prints "[#..]".200func (p outputPort) PrintBar(now, max int64, w int, fg, bg string) error {201 return p.PrintLabel(buildTextBar(now, max, w, fg, bg))202}203func (p outputPort) VPrintBar(vname string, now, max int64, width int, fg, bg string) error {204 err := p.withView(vname, func() {205 p.PrintBar(now, max, width, fg, bg)206 })207 return err208}209// return text represented bar as string.210// it is same as printed text by PrintBar().211func (p outputPort) TextBar(now, max int64, w int, fg, bg string) (string, error) {212 return buildTextBar(now, max, w, fg, bg), nil213}214func (p outputPort) VPrintLine(vname string, sym string) error {215 err := p.withView(vname, func() {216 p.PrintLine(sym)217 })218 return err219}220func (p outputPort) VClearLine(vname string, nline int) error {221 err := p.withView(vname, func() {222 p.ClearLine(nline)223 })224 return err225}226func (p outputPort) VClearLineAll(vname string) error {227 err := p.withView(vname, func() {228 p.ClearLineAll()229 })230 return err231}232func (p outputPort) VNewPage(vname string) error {233 err := p.withView(vname, func() {234 p.NewPage()235 })236 return err237}238var (239 errorEmptyNameNotAllowed = errors.New("empty view name is not allowed")240 errorInvalidBorderRate = errors.New("border rate must be in [0:1)")241)242func (p outputPort) SetSingleLayout(vname string) error {243 if vname == "" {244 return errorEmptyNameNotAllowed245 }246 return p.UI.SetLayout(attr.NewSingleText(vname))247}248func (p outputPort) SetVerticalLayout(vname1, vname2 string, rate float64) error {249 text1, text2, err := weightedTexts(vname1, vname2, rate)250 if err != nil {251 return err252 }253 return p.UI.SetLayout(attr.NewFlowVertical(text1, text2))254}255func (p outputPort) SetHorizontalLayout(vname1, vname2 string, rate float64) error {256 text1, text2, err := weightedTexts(vname1, vname2, rate)257 if err != nil {258 return err259 }260 return p.UI.SetLayout(attr.NewFlowHorizontal(text1, text2))261}262func weightedTexts(vname1, vname2 string, rate float64) (text1, text2 *attr.LayoutData, err error) {263 if vname1 == "" || vname2 == "" {264 return nil, nil, errorEmptyNameNotAllowed265 }266 if rate <= 0.0 || rate >= 1.0 {267 return nil, nil, errorInvalidBorderRate268 }269 weight1 := int(10.0 * rate)270 weight2 := 10 - weight1271 text1 = attr.WithParentValue(attr.NewSingleText(vname1), weight1)272 text2 = attr.WithParentValue(attr.NewSingleText(vname2), weight2)273 return274}...
scene_iocontroller.go
Source:scene_iocontroller.go
1package stub2import (3 "context"4 "fmt"5 "time"6)7// implements scene.IOController8type sceneIOController struct {9 *gameUIStub10}11// return implements scene.IOController12func NewFlowGameController() *sceneIOController {13 return &sceneIOController{NewGameUIStub()}14}15func (ui sceneIOController) RawInput() (string, error) {16 return "", nil17}18func (ui sceneIOController) RawInputWithTimeout(context.Context, time.Duration) (string, error) {19 return "", nil20}21func (ui sceneIOController) Command() (string, error) {22 return "", nil23}24func (ui sceneIOController) CommandWithTimeout(context.Context, time.Duration) (string, error) {25 return "", nil26}27func (ui sceneIOController) CommandNumber() (int, error) {28 return 0, nil29}30func (ui sceneIOController) CommandNumberWithTimeout(context.Context, time.Duration) (int, error) {31 return 0, nil32}33func (ui sceneIOController) CommandNumberRange(ctx context.Context, min, max int) (int, error) {34 return 0, nil35}36func (ui sceneIOController) CommandNumberSelect(ctx context.Context, ns ...int) (int, error) {37 return 0, nil38}39func (ui sceneIOController) Wait() error {40 return nil41}42func (ui sceneIOController) WaitWithTimeout(context.Context, time.Duration) error {43 return nil44}45func (ui sceneIOController) PrintPlain(s string) error {46 ui.Print(s)47 return nil48}49func (ui sceneIOController) PrintL(s string) error {50 _, err := fmt.Println(s)51 return err52}53func (ui sceneIOController) PrintC(s string, width int) error {54 ui.Print(s)55 return nil56}57func (ui sceneIOController) PrintW(s string) error {58 ui.Print(s)59 return nil60}61func (ui sceneIOController) PrintBar(int64, int64, int, string, string) error {62 ui.Print("[#..]")63 return nil64}65func (ui sceneIOController) TextBar(int64, int64, int, string, string) (string, error) {66 return "[#..]", nil67}68func (ui sceneIOController) VPrint(vname, s string) error {69 ui.Print(s)70 return nil71}72func (ui sceneIOController) VPrintL(vname, s string) error {73 ui.PrintL(s)74 return nil75}76func (ui sceneIOController) VPrintC(vname, s string, width int) error {77 ui.PrintC(s, width)78 return nil79}80func (ui sceneIOController) VPrintPlain(vname, s string) error {81 ui.PrintPlain(s)82 return nil83}84func (ui sceneIOController) VPrintW(vname, s string) error {85 ui.PrintW(s)86 return nil87}88func (ui sceneIOController) VPrintButton(vname, caption, cmd string) error {89 ui.PrintButton(caption, cmd)90 return nil91}92func (ui sceneIOController) VPrintBar(vname string, now, max int64, width int, fg, bg string) error {93 ui.PrintBar(now, max, width, fg, bg)94 return nil95}96func (ui sceneIOController) VPrintLine(vname, sym string) error {97 ui.PrintLine(sym)98 return nil99}100func (ui sceneIOController) VClearLine(vname string, nline int) error {101 ui.ClearLine(nline)102 return nil103}104func (ui sceneIOController) VClearLineAll(vname string) error {105 ui.ClearLineAll()106 return nil107}108func (ui sceneIOController) VNewPage(vname string) error {109 ui.NewPage()110 return nil111}112func (ui sceneIOController) SetSingleLayout(string) error { return nil }113func (ui sceneIOController) SetHorizontalLayout(string, string, float64) error { return nil }114func (ui sceneIOController) SetVerticalLayout(string, string, float64) error { return nil }...
printBar
Using AI Code Generation
1import "cmd"2func main() {3 cmd.PrintBar()4}5import "cmd"6func main() {7 cmd.PrintFoo()8}9import "cmd"10func main() {11 cmd.PrintFoo()12}13import "cmd"14func main() {15 cmd.PrintBar()16}17import "cmd"18func main() {19 cmd.PrintBar()20}21import "cmd"22func main() {23 cmd.PrintFoo()24}25import "cmd"26func main() {27 cmd.PrintBar()28}29import "cmd"30func main() {31 cmd.PrintFoo()32}33import "cmd"34func main() {35 cmd.PrintBar()36}37import "cmd"38func main() {39 cmd.PrintFoo()40}41import "cmd"42func main() {43 cmd.PrintBar()44}45import "cmd"46func main() {47 cmd.PrintBar()48}49import "cmd"50func main() {51 cmd.PrintFoo()52}53import "cmd"54func main() {55 cmd.PrintBar()56}57import "cmd"
printBar
Using AI Code Generation
1import "cmd"2func main() {3cmd.printBar()4}5import "cmd"6func main() {7cmd.printFoo()8}9import "cmd"10func main() {11cmd.printBar()12}13import "cmd"14func main() {15cmd.printFoo()16}17import "cmd"18func main() {19cmd.printBar()20}21import "cmd"22func main() {23cmd.printFoo()24}25import "cmd"26func main() {27cmd.printBar()28}29import "cmd"30func main() {31cmd.printFoo()32}33import "cmd"34func main() {35cmd.printBar()36}37import "cmd"38func main() {39cmd.printFoo()40}41import "cmd"42func main() {43cmd.printBar()44}45import "cmd"46func main() {47cmd.printFoo()48}49import "cmd"50func main() {51cmd.printBar()52}53import "cmd"54func main() {55cmd.printFoo()56}57import "cmd"58func main() {59cmd.printBar()60}
printBar
Using AI Code Generation
1import "fmt"2func main() {3 cmd := Cmd{}4 cmd.printBar()5}6import "fmt"7func main() {8 cmd := Cmd{}9 cmd.printBar()10}
printBar
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println("Hello, playground")4 cmd := cmd{}5 cmd.printBar()6}7import "fmt"8func main() {9 fmt.Println("Hello, playground")10 cmd := cmd{}11 cmd.printFoo()12}13import "fmt"14func main() {15 fmt.Println("Hello, playground")16 cmd := cmd{}17 cmd.printBar()18}19import "fmt"20func main() {21 fmt.Println("Hello, playground")22 cmd := cmd{}23 cmd.printFoo()24}25import "fmt"26func main() {27 fmt.Println("Hello, playground")28 cmd := cmd{}29 cmd.printBar()30}31import "fmt"32func main() {33 fmt.Println("Hello, playground")34 cmd := cmd{}35 cmd.printFoo()36}37import "fmt"38func main() {39 fmt.Println("Hello, playground")40 cmd := cmd{}41 cmd.printBar()42}43import "fmt"44func main() {45 fmt.Println("Hello, playground")46 cmd := cmd{}47 cmd.printFoo()48}49import "fmt"50func main() {51 fmt.Println("Hello, playground")52 cmd := cmd{}53 cmd.printBar()54}55import "fmt"56func main() {57 fmt.Println("Hello, playground")58 cmd := cmd{}59 cmd.printFoo()60}61import "fmt"62func main() {63 fmt.Println("Hello, playground
printBar
Using AI Code Generation
1import (2func main() {3 cmd.PrintBar()4}5import (6func PrintBar() {7 fmt.Println("bar")8}9import "testing"10func TestPrintBar(t *testing.T) {11 PrintBar()12}
printBar
Using AI Code Generation
1import "github.com/username/bar"2func main() {3 bar.printBar()4}5import "github.com/username/bar"6func main() {7 bar.printBar()8}
printBar
Using AI Code Generation
1import "fmt"2import "1"3func main(){4cmd.printBar()5}6func printBar(){7fmt.Println("bar")8}9func printFoo(){10fmt.Println("foo")11}12import "fmt"13import "1"14func main(){15cmd.printFoo()16}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!