@@ -43,3 +43,107 @@ const assert = require('assert');
4343
4444 input . write ( 'abc\n' ) ;
4545}
46+
47+ {
48+ const input = new PassThrough ( ) ;
49+ const rl = readline . createInterface ( {
50+ terminal : true ,
51+ input : input
52+ } ) ;
53+
54+ rl . write ( 'foo' ) ;
55+ assert . strictEqual ( rl . cursor , 3 ) ;
56+
57+ const key = {
58+ xterm : {
59+ home : [ '\x1b[H' , { ctrl : true , name : 'a' } ] ,
60+ end : [ '\x1b[F' , { ctrl : true , name : 'e' } ] ,
61+ } ,
62+ gnome : {
63+ home : [ '\x1bOH' , { ctrl : true , name : 'a' } ] ,
64+ end : [ '\x1bOF' , { ctrl : true , name : 'e' } ]
65+ } ,
66+ rxvt : {
67+ home : [ '\x1b[7' , { ctrl : true , name : 'a' } ] ,
68+ end : [ '\x1b[8' , { ctrl : true , name : 'e' } ]
69+ } ,
70+ putty : {
71+ home : [ '\x1b[1~' , { ctrl : true , name : 'a' } ] ,
72+ end : [ '\x1b[>~' , { ctrl : true , name : 'e' } ]
73+ }
74+ } ;
75+
76+ [ key . xterm , key . gnome , key . rxvt , key . putty ] . forEach ( function ( key ) {
77+ rl . write . apply ( rl , key . home ) ;
78+ assert . strictEqual ( rl . cursor , 0 ) ;
79+ rl . write . apply ( rl , key . end ) ;
80+ assert . strictEqual ( rl . cursor , 3 ) ;
81+ } ) ;
82+
83+ }
84+
85+ {
86+ const input = new PassThrough ( ) ;
87+ const rl = readline . createInterface ( {
88+ terminal : true ,
89+ input : input
90+ } ) ;
91+
92+ const key = {
93+ xterm : {
94+ home : [ '\x1b[H' , { ctrl : true , name : 'a' } ] ,
95+ metab : [ '\x1bb' , { meta : true , name : 'b' } ] ,
96+ metaf : [ '\x1bf' , { meta : true , name : 'f' } ] ,
97+ }
98+ } ;
99+
100+ rl . write ( 'foo bar.hop/zoo' ) ;
101+ rl . write . apply ( rl , key . xterm . home ) ;
102+ [
103+ { cursor : 4 , key : key . xterm . metaf } ,
104+ { cursor : 7 , key : key . xterm . metaf } ,
105+ { cursor : 8 , key : key . xterm . metaf } ,
106+ { cursor : 11 , key : key . xterm . metaf } ,
107+ { cursor : 12 , key : key . xterm . metaf } ,
108+ { cursor : 15 , key : key . xterm . metaf } ,
109+ { cursor : 12 , key : key . xterm . metab } ,
110+ { cursor : 11 , key : key . xterm . metab } ,
111+ { cursor : 8 , key : key . xterm . metab } ,
112+ { cursor : 7 , key : key . xterm . metab } ,
113+ { cursor : 4 , key : key . xterm . metab } ,
114+ { cursor : 0 , key : key . xterm . metab } ,
115+ ] . forEach ( function ( action ) {
116+ rl . write . apply ( rl , action . key ) ;
117+ assert . strictEqual ( rl . cursor , action . cursor ) ;
118+ } ) ;
119+ }
120+
121+ {
122+ const input = new PassThrough ( ) ;
123+ const rl = readline . createInterface ( {
124+ terminal : true ,
125+ input : input
126+ } ) ;
127+
128+ const key = {
129+ xterm : {
130+ home : [ '\x1b[H' , { ctrl : true , name : 'a' } ] ,
131+ metad : [ '\x1bd' , { meta : true , name : 'd' } ]
132+ }
133+ } ;
134+
135+ rl . write ( 'foo bar.hop/zoo' ) ;
136+ rl . write . apply ( rl , key . xterm . home ) ;
137+ [
138+ 'bar.hop/zoo' ,
139+ '.hop/zoo' ,
140+ 'hop/zoo' ,
141+ '/zoo' ,
142+ 'zoo' ,
143+ ''
144+ ] . forEach ( function ( expectedLine ) {
145+ rl . write . apply ( rl , key . xterm . metad ) ;
146+ assert . strictEqual ( 0 , rl . cursor ) ;
147+ assert . strictEqual ( expectedLine , rl . line ) ;
148+ } ) ;
149+ }
0 commit comments