@@ -56,6 +56,12 @@ function getInterface(options) {
5656 return [ rli , fi ] ;
5757}
5858
59+ function assertCursorRowsAndCols ( rli , rows , cols ) {
60+ const cursorPos = rli . getCursorPos ( ) ;
61+ assert . strictEqual ( cursorPos . rows , rows ) ;
62+ assert . strictEqual ( cursorPos . cols , cols ) ;
63+ }
64+
5965[
6066 undefined ,
6167 50 ,
@@ -338,9 +344,7 @@ function getInterface(options) {
338344 const [ rli ] = getInterface ( { terminal : true } ) ;
339345 const expectedLines = [ 'foo' ] ;
340346 rli . question ( expectedLines [ 0 ] , ( ) => rli . close ( ) ) ;
341- const cursorPos = rli . getCursorPos ( ) ;
342- assert . strictEqual ( cursorPos . rows , 0 ) ;
343- assert . strictEqual ( cursorPos . cols , expectedLines [ 0 ] . length ) ;
347+ assertCursorRowsAndCols ( rli , 0 , expectedLines [ 0 ] . length ) ;
344348 rli . close ( ) ;
345349}
346350
@@ -349,9 +353,8 @@ function getInterface(options) {
349353 const [ rli ] = getInterface ( { terminal : true } ) ;
350354 const expectedLines = [ 'foo' , 'bar' ] ;
351355 rli . question ( expectedLines . join ( '\n' ) , ( ) => rli . close ( ) ) ;
352- const cursorPos = rli . getCursorPos ( ) ;
353- assert . strictEqual ( cursorPos . rows , expectedLines . length - 1 ) ;
354- assert . strictEqual ( cursorPos . cols , expectedLines . slice ( - 1 ) [ 0 ] . length ) ;
356+ assertCursorRowsAndCols (
357+ rli , expectedLines . length - 1 , expectedLines . slice ( - 1 ) [ 0 ] . length ) ;
355358 rli . close ( ) ;
356359}
357360
@@ -360,44 +363,30 @@ function getInterface(options) {
360363 const [ rli , fi ] = getInterface ( { terminal : true , prompt : '' } ) ;
361364 fi . emit ( 'data' , 'the quick brown fox' ) ;
362365 fi . emit ( 'keypress' , '.' , { ctrl : true , name : 'a' } ) ;
363- let cursorPos = rli . getCursorPos ( ) ;
364- assert . strictEqual ( cursorPos . rows , 0 ) ;
365- assert . strictEqual ( cursorPos . cols , 0 ) ;
366+ assertCursorRowsAndCols ( rli , 0 , 0 ) ;
366367 fi . emit ( 'keypress' , '.' , { ctrl : true , name : 'e' } ) ;
367- cursorPos = rli . getCursorPos ( ) ;
368- assert . strictEqual ( cursorPos . rows , 0 ) ;
369- assert . strictEqual ( cursorPos . cols , 19 ) ;
368+ assertCursorRowsAndCols ( rli , 0 , 19 ) ;
370369 rli . close ( ) ;
371370}
372371
373372{
374373 // Back and Forward one character
375374 const [ rli , fi ] = getInterface ( { terminal : true , prompt : '' } ) ;
376375 fi . emit ( 'data' , 'the quick brown fox' ) ;
377- let cursorPos = rli . getCursorPos ( ) ;
378- assert . strictEqual ( cursorPos . rows , 0 ) ;
379- assert . strictEqual ( cursorPos . cols , 19 ) ;
376+ assertCursorRowsAndCols ( rli , 0 , 19 ) ;
380377
381378 // Back one character
382379 fi . emit ( 'keypress' , '.' , { ctrl : true , name : 'b' } ) ;
383- cursorPos = rli . getCursorPos ( ) ;
384- assert . strictEqual ( cursorPos . rows , 0 ) ;
385- assert . strictEqual ( cursorPos . cols , 18 ) ;
380+ assertCursorRowsAndCols ( rli , 0 , 18 ) ;
386381 // Back one character
387382 fi . emit ( 'keypress' , '.' , { ctrl : true , name : 'b' } ) ;
388- cursorPos = rli . getCursorPos ( ) ;
389- assert . strictEqual ( cursorPos . rows , 0 ) ;
390- assert . strictEqual ( cursorPos . cols , 17 ) ;
383+ assertCursorRowsAndCols ( rli , 0 , 17 ) ;
391384 // Forward one character
392385 fi . emit ( 'keypress' , '.' , { ctrl : true , name : 'f' } ) ;
393- cursorPos = rli . getCursorPos ( ) ;
394- assert . strictEqual ( cursorPos . rows , 0 ) ;
395- assert . strictEqual ( cursorPos . cols , 18 ) ;
386+ assertCursorRowsAndCols ( rli , 0 , 18 ) ;
396387 // Forward one character
397388 fi . emit ( 'keypress' , '.' , { ctrl : true , name : 'f' } ) ;
398- cursorPos = rli . getCursorPos ( ) ;
399- assert . strictEqual ( cursorPos . rows , 0 ) ;
400- assert . strictEqual ( cursorPos . cols , 19 ) ;
389+ assertCursorRowsAndCols ( rli , 0 , 19 ) ;
401390 rli . close ( ) ;
402391}
403392
@@ -408,15 +397,11 @@ function getInterface(options) {
408397
409398 // Move left one character/code point
410399 fi . emit ( 'keypress' , '.' , { name : 'left' } ) ;
411- let cursorPos = rli . getCursorPos ( ) ;
412- assert . strictEqual ( cursorPos . rows , 0 ) ;
413- assert . strictEqual ( cursorPos . cols , 0 ) ;
400+ assertCursorRowsAndCols ( rli , 0 , 0 ) ;
414401
415402 // Move right one character/code point
416403 fi . emit ( 'keypress' , '.' , { name : 'right' } ) ;
417- cursorPos = rli . getCursorPos ( ) ;
418- assert . strictEqual ( cursorPos . rows , 0 ) ;
419- assert . strictEqual ( cursorPos . cols , 2 ) ;
404+ assertCursorRowsAndCols ( rli , 0 , 2 ) ;
420405
421406 rli . on ( 'line' , common . mustCall ( ( line ) => {
422407 assert . strictEqual ( line , '💻' ) ;
@@ -432,14 +417,10 @@ function getInterface(options) {
432417
433418 // Move left one character/code point
434419 fi . emit ( 'keypress' , '.' , { name : 'left' } ) ;
435- let cursorPos = rli . getCursorPos ( ) ;
436- assert . strictEqual ( cursorPos . rows , 0 ) ;
437- assert . strictEqual ( cursorPos . cols , 0 ) ;
420+ assertCursorRowsAndCols ( rli , 0 , 0 ) ;
438421
439422 fi . emit ( 'data' , '🐕' ) ;
440- cursorPos = rli . getCursorPos ( ) ;
441- assert . strictEqual ( cursorPos . rows , 0 ) ;
442- assert . strictEqual ( cursorPos . cols , 2 ) ;
423+ assertCursorRowsAndCols ( rli , 0 , 2 ) ;
443424
444425 rli . on ( 'line' , common . mustCall ( ( line ) => {
445426 assert . strictEqual ( line , '🐕💻' ) ;
@@ -455,14 +436,10 @@ function getInterface(options) {
455436
456437 // Move left one character/code point
457438 fi . emit ( 'keypress' , '.' , { name : 'right' } ) ;
458- let cursorPos = rli . getCursorPos ( ) ;
459- assert . strictEqual ( cursorPos . rows , 0 ) ;
460- assert . strictEqual ( cursorPos . cols , 2 ) ;
439+ assertCursorRowsAndCols ( rli , 0 , 2 ) ;
461440
462441 fi . emit ( 'data' , '🐕' ) ;
463- cursorPos = rli . getCursorPos ( ) ;
464- assert . strictEqual ( cursorPos . rows , 0 ) ;
465- assert . strictEqual ( cursorPos . cols , 4 ) ;
442+ assertCursorRowsAndCols ( rli , 0 , 4 ) ;
466443
467444 rli . on ( 'line' , common . mustCall ( ( line ) => {
468445 assert . strictEqual ( line , '💻🐕' ) ;
@@ -476,21 +453,13 @@ function getInterface(options) {
476453 const [ rli , fi ] = getInterface ( { terminal : true , prompt : '' } ) ;
477454 fi . emit ( 'data' , 'the quick brown fox' ) ;
478455 fi . emit ( 'keypress' , '.' , { ctrl : true , name : 'left' } ) ;
479- let cursorPos = rli . getCursorPos ( ) ;
480- assert . strictEqual ( cursorPos . rows , 0 ) ;
481- assert . strictEqual ( cursorPos . cols , 16 ) ;
456+ assertCursorRowsAndCols ( rli , 0 , 16 ) ;
482457 fi . emit ( 'keypress' , '.' , { meta : true , name : 'b' } ) ;
483- cursorPos = rli . getCursorPos ( ) ;
484- assert . strictEqual ( cursorPos . rows , 0 ) ;
485- assert . strictEqual ( cursorPos . cols , 10 ) ;
458+ assertCursorRowsAndCols ( rli , 0 , 10 ) ;
486459 fi . emit ( 'keypress' , '.' , { ctrl : true , name : 'right' } ) ;
487- cursorPos = rli . getCursorPos ( ) ;
488- assert . strictEqual ( cursorPos . rows , 0 ) ;
489- assert . strictEqual ( cursorPos . cols , 16 ) ;
460+ assertCursorRowsAndCols ( rli , 0 , 16 ) ;
490461 fi . emit ( 'keypress' , '.' , { meta : true , name : 'f' } ) ;
491- cursorPos = rli . getCursorPos ( ) ;
492- assert . strictEqual ( cursorPos . rows , 0 ) ;
493- assert . strictEqual ( cursorPos . cols , 19 ) ;
462+ assertCursorRowsAndCols ( rli , 0 , 19 ) ;
494463 rli . close ( ) ;
495464}
496465
@@ -554,15 +523,11 @@ function getInterface(options) {
554523{
555524 const [ rli , fi ] = getInterface ( { terminal : true , prompt : '' } ) ;
556525 fi . emit ( 'data' , 'the quick brown fox' ) ;
557- let cursorPos = rli . getCursorPos ( ) ;
558- assert . strictEqual ( cursorPos . rows , 0 ) ;
559- assert . strictEqual ( cursorPos . cols , 19 ) ;
526+ assertCursorRowsAndCols ( rli , 0 , 19 ) ;
560527
561528 // Delete left character
562529 fi . emit ( 'keypress' , '.' , { ctrl : true , name : 'h' } ) ;
563- cursorPos = rli . getCursorPos ( ) ;
564- assert . strictEqual ( cursorPos . rows , 0 ) ;
565- assert . strictEqual ( cursorPos . cols , 18 ) ;
530+ assertCursorRowsAndCols ( rli , 0 , 18 ) ;
566531 rli . on ( 'line' , common . mustCall ( ( line ) => {
567532 assert . strictEqual ( line , 'the quick brown fo' ) ;
568533 } ) ) ;
@@ -574,14 +539,10 @@ function getInterface(options) {
574539{
575540 const [ rli , fi ] = getInterface ( { terminal : true , prompt : '' } ) ;
576541 fi . emit ( 'data' , '💻' ) ;
577- let cursorPos = rli . getCursorPos ( ) ;
578- assert . strictEqual ( cursorPos . rows , 0 ) ;
579- assert . strictEqual ( cursorPos . cols , 2 ) ;
542+ assertCursorRowsAndCols ( rli , 0 , 2 ) ;
580543 // Delete left character
581544 fi . emit ( 'keypress' , '.' , { ctrl : true , name : 'h' } ) ;
582- cursorPos = rli . getCursorPos ( ) ;
583- assert . strictEqual ( cursorPos . rows , 0 ) ;
584- assert . strictEqual ( cursorPos . cols , 0 ) ;
545+ assertCursorRowsAndCols ( rli , 0 , 0 ) ;
585546 rli . on ( 'line' , common . mustCall ( ( line ) => {
586547 assert . strictEqual ( line , '' ) ;
587548 } ) ) ;
@@ -596,15 +557,11 @@ function getInterface(options) {
596557
597558 // Go to the start of the line
598559 fi . emit ( 'keypress' , '.' , { ctrl : true , name : 'a' } ) ;
599- let cursorPos = rli . getCursorPos ( ) ;
600- assert . strictEqual ( cursorPos . rows , 0 ) ;
601- assert . strictEqual ( cursorPos . cols , 0 ) ;
560+ assertCursorRowsAndCols ( rli , 0 , 0 ) ;
602561
603562 // Delete right character
604563 fi . emit ( 'keypress' , '.' , { ctrl : true , name : 'd' } ) ;
605- cursorPos = rli . getCursorPos ( ) ;
606- assert . strictEqual ( cursorPos . rows , 0 ) ;
607- assert . strictEqual ( cursorPos . cols , 0 ) ;
564+ assertCursorRowsAndCols ( rli , 0 , 0 ) ;
608565 rli . on ( 'line' , common . mustCall ( ( line ) => {
609566 assert . strictEqual ( line , 'he quick brown fox' ) ;
610567 } ) ) ;
@@ -619,15 +576,11 @@ function getInterface(options) {
619576
620577 // Go to the start of the line
621578 fi . emit ( 'keypress' , '.' , { ctrl : true , name : 'a' } ) ;
622- let cursorPos = rli . getCursorPos ( ) ;
623- assert . strictEqual ( cursorPos . rows , 0 ) ;
624- assert . strictEqual ( cursorPos . cols , 0 ) ;
579+ assertCursorRowsAndCols ( rli , 0 , 0 ) ;
625580
626581 // Delete right character
627582 fi . emit ( 'keypress' , '.' , { ctrl : true , name : 'd' } ) ;
628- cursorPos = rli . getCursorPos ( ) ;
629- assert . strictEqual ( cursorPos . rows , 0 ) ;
630- assert . strictEqual ( cursorPos . cols , 0 ) ;
583+ assertCursorRowsAndCols ( rli , 0 , 0 ) ;
631584 rli . on ( 'line' , common . mustCall ( ( line ) => {
632585 assert . strictEqual ( line , '' ) ;
633586 } ) ) ;
@@ -639,15 +592,11 @@ function getInterface(options) {
639592{
640593 const [ rli , fi ] = getInterface ( { terminal : true , prompt : '' } ) ;
641594 fi . emit ( 'data' , 'the quick brown fox' ) ;
642- let cursorPos = rli . getCursorPos ( ) ;
643- assert . strictEqual ( cursorPos . rows , 0 ) ;
644- assert . strictEqual ( cursorPos . cols , 19 ) ;
595+ assertCursorRowsAndCols ( rli , 0 , 19 ) ;
645596
646597 // Delete from current to start of line
647598 fi . emit ( 'keypress' , '.' , { ctrl : true , shift : true , name : 'backspace' } ) ;
648- cursorPos = rli . getCursorPos ( ) ;
649- assert . strictEqual ( cursorPos . rows , 0 ) ;
650- assert . strictEqual ( cursorPos . cols , 0 ) ;
599+ assertCursorRowsAndCols ( rli , 0 , 0 ) ;
651600 rli . on ( 'line' , common . mustCall ( ( line ) => {
652601 assert . strictEqual ( line , '' ) ;
653602 } ) ) ;
@@ -662,15 +611,11 @@ function getInterface(options) {
662611
663612 // Go to the start of the line
664613 fi . emit ( 'keypress' , '.' , { ctrl : true , name : 'a' } ) ;
665- let cursorPos = rli . getCursorPos ( ) ;
666- assert . strictEqual ( cursorPos . rows , 0 ) ;
667- assert . strictEqual ( cursorPos . cols , 0 ) ;
614+ assertCursorRowsAndCols ( rli , 0 , 0 ) ;
668615
669616 // Delete from current to end of line
670617 fi . emit ( 'keypress' , '.' , { ctrl : true , shift : true , name : 'delete' } ) ;
671- cursorPos = rli . getCursorPos ( ) ;
672- assert . strictEqual ( cursorPos . rows , 0 ) ;
673- assert . strictEqual ( cursorPos . cols , 0 ) ;
618+ assertCursorRowsAndCols ( rli , 0 , 0 ) ;
674619 rli . on ( 'line' , common . mustCall ( ( line ) => {
675620 assert . strictEqual ( line , '' ) ;
676621 } ) ) ;
@@ -683,9 +628,7 @@ function getInterface(options) {
683628 const [ rli , fi ] = getInterface ( { terminal : true , prompt : '' } ) ;
684629 fi . columns = 10 ;
685630 fi . emit ( 'data' , 'multi-line text' ) ;
686- const cursorPos = rli . getCursorPos ( ) ;
687- assert . strictEqual ( cursorPos . rows , 1 ) ;
688- assert . strictEqual ( cursorPos . cols , 5 ) ;
631+ assertCursorRowsAndCols ( rli , 1 , 5 ) ;
689632 rli . close ( ) ;
690633}
691634
@@ -697,9 +640,7 @@ function getInterface(options) {
697640 } ) ;
698641 fi . columns = 10 ;
699642 fi . emit ( 'data' , 't' ) ;
700- const cursorPos = rli . getCursorPos ( ) ;
701- assert . strictEqual ( cursorPos . rows , 4 ) ;
702- assert . strictEqual ( cursorPos . cols , 3 ) ;
643+ assertCursorRowsAndCols ( rli , 4 , 3 ) ;
703644 rli . close ( ) ;
704645}
705646
@@ -709,9 +650,7 @@ function getInterface(options) {
709650 const lines = [ 'line 1' , 'line 2' , 'line 3' ] ;
710651 fi . emit ( 'data' , lines . join ( '\n' ) ) ;
711652 fi . emit ( 'keypress' , '.' , { ctrl : true , name : 'l' } ) ;
712- const cursorPos = rli . getCursorPos ( ) ;
713- assert . strictEqual ( cursorPos . rows , 0 ) ;
714- assert . strictEqual ( cursorPos . cols , 6 ) ;
653+ assertCursorRowsAndCols ( rli , 0 , 6 ) ;
715654 rli . on ( 'line' , common . mustCall ( ( line ) => {
716655 assert . strictEqual ( line , 'line 3' ) ;
717656 } ) ) ;
0 commit comments