robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4local tt =require ( 'test.functional.testterm' ) 5 6local describe , it , before_each =t . describe , t . it , t . before_each 7local assert_alive =n . assert_alive 8local feed , clear =n . feed , n . clear 9local api =n . api 10local testprg , command =n . testprg , n . command 11local fn =n . fn 12local nvim_set =n . nvim_set 13local is_os =t . is_os 14local skip =t . skip 15 16describe ( ':terminal highlight' , function () 17local screen 18 19before_each ( function () 20clear () 21screen =Screen . new ( 50 , 7 , { rgb =false } ) 22screen : set_default_attr_ids ( { 23[ 1 ] ={ foreground =45 } , 24[ 2 ] ={ background =46 } , 25[ 3 ] ={ foreground =45 , background =46 } , 26[ 4 ] ={ bold =true , italic =true , underline =true , strikethrough =true } , 27[ 5 ] ={ bold =true } , 28[ 6 ] ={ foreground =12 } , 29[ 7 ] ={ bold =true , reverse =true } , 30[ 8 ] ={ background =11 } , 31[ 9 ] ={ foreground =130 } , 32[ 10 ] ={ reverse =true } , 33[ 11 ] ={ background =11 } , 34[ 12 ] ={ bold =true , underdouble =true } , 35[ 13 ] ={ italic =true , undercurl =true } , 36} ) 37command (( "enew | call jobstart(['%s'], {'term':v:true})" ): format ( testprg ( 'tty-test' ))) 38feed ( 'i' ) 39screen : expect ( [[ 40tty ready | 41^ | 42|*4 43{5:-- TERMINAL --} | 44]] ) 45end ) 46 47local function descr ( title , attr_num , set_attrs_fn ) 48local function sub ( s ) 49local str =s : gsub ( 'NUM' , attr_num ) 50return str 51end 52 53describe ( title , function () 54before_each ( function () 55set_attrs_fn () 56tt . feed_data ( 'text' ) 57tt . clear_attrs () 58tt . feed_data ( 'text' ) 59end ) 60 61local function pass_attrs () 62screen : expect ( sub ( [[ 63tty ready | 64{NUM:text}text^ | 65|*4 66{5:-- TERMINAL --} | 67]] )) 68end 69 70it ( 'will pass the corresponding attributes' , pass_attrs ) 71 72it ( 'will pass the corresponding attributes on scrollback' , function () 73pass_attrs () 74local lines ={} 75for i =1 , 8 do 76table . insert ( lines , 'line' .. tostring ( i )) 77end 78table . insert ( lines , '' ) 79tt . feed_data ( lines ) 80screen : expect ( [[ 81line4 | 82line5 | 83line6 | 84line7 | 85line8 | 86^ | 87{5:-- TERMINAL --} | 88]] ) 89feed ( '<c-\\><c-n>gg' ) 90screen : expect ( sub ( [[ 91^tty ready | 92{NUM:text}textline1 | 93line2 | 94line3 | 95line4 | 96line5 | 97| 98]] )) 99end ) 100end ) 101end 102 103descr ( 'foreground' , 1 , function () 104tt . set_fg ( 45 ) 105end ) 106descr ( 'background' , 2 , function () 107tt . set_bg ( 46 ) 108end ) 109descr ( 'foreground and background' , 3 , function () 110tt . set_fg ( 45 ) 111tt . set_bg ( 46 ) 112end ) 113descr ( 'bold, italics, underline and strikethrough' , 4 , function () 114tt . set_bold () 115tt . set_italic () 116tt . set_underline () 117tt . set_strikethrough () 118end ) 119descr ( 'bold and underdouble' , 12 , function () 120tt . set_bold () 121tt . set_underdouble () 122end ) 123descr ( 'italics and undercurl' , 13 , function () 124tt . set_italic () 125tt . set_undercurl () 126end ) 127end ) 128 129it ( ':terminal highlight has lower precedence than editor #9964' , function () 130clear () 131local screen =Screen . new ( 30 , 4 , { rgb =true } ) 132screen : set_default_attr_ids ( { 133-- "Normal" highlight emitted by the child nvim process. 134N_child ={ 135foreground =tonumber ( '0x4040ff' ), 136background =tonumber ( '0xffff40' ), 137fg_indexed =true , 138bg_indexed =true , 139} , 140-- "Search" highlight in the parent nvim process. 141S ={ background =Screen . colors . Green , italic =true , foreground =Screen . colors . Red } , 142-- "Question" highlight in the parent nvim process. 143-- note: bg is indexed as it comes from the (cterm) child, while fg isn't as it comes from (rgb) parent 144Q ={ 145background =tonumber ( '0xffff40' ), 146bold =true , 147foreground =Screen . colors . SeaGreen4 , 148bg_indexed =true , 149} , 150} ) 151-- Child nvim process in :terminal (with cterm colors). 152fn . jobstart ( { 153n . nvim_prog , 154'-n' , 155'-u' , 156'NORC' , 157'-i' , 158'NONE' , 159'--cmd' , 160nvim_set .. ' notermguicolors' , 161'+hi Normal ctermfg=Blue ctermbg=Yellow' , 162'+norm! ichild nvim' , 163'+norm! oline 2' , 164} , { 165term =true , 166env ={ 167VIMRUNTIME =os . getenv ( 'VIMRUNTIME' ), 168} , 169} ) 170screen : expect ( [[ 171{N_child:^child nvim }| 172{N_child:line 2 }| 173{N_child: }| 174| 175]] ) 176command ( 'hi Search gui=italic guifg=Red guibg=Green cterm=italic ctermfg=Red ctermbg=Green' ) 177feed ( '/nvim<cr>' ) 178screen : expect ( [[ 179{N_child:child }{S:^nvim}{N_child: }| 180{N_child:line 2 }| 181{N_child: }| 182/nvim | 183]] ) 184command ( 'syntax keyword Question line' ) 185screen : expect ( [[ 186{N_child:child }{S:^nvim}{N_child: }| 187{Q:line}{N_child: 2 }| 188{N_child: }| 189/nvim | 190]] ) 191end ) 192 193it ( 'CursorLine and CursorColumn work in :terminal buffer in Normal mode' , function () 194clear () 195local screen =Screen . new ( 50 , 7 ) 196screen : add_extra_attr_ids ( { [ 100 ] ={ background =Screen . colors . Gray90 , reverse =true } } ) 197command (( "enew | call jobstart(['%s'], {'term':v:true})" ): format ( testprg ( 'tty-test' ))) 198screen : expect ( [[ 199^tty ready | 200|*6 201]] ) 202tt . feed_data (( ' foobar' ): rep ( 30 )) 203screen : expect ( [[ 204^tty ready | 205foobar foobar foobar foobar foobar foobar foobar | 206foobar foobar foobar foobar foobar foobar foobar f| 207oobar foobar foobar foobar foobar foobar foobar fo| 208obar foobar foobar foobar foobar foobar foobar foo| 209bar foobar | 210| 211]] ) 212command ( 'set cursorline cursorcolumn' ) 213feed ( 'j10w' ) 214screen : expect ( [[ 215tty ready {21: } | 216foobar foobar{21: }foobar foobar foobar foobar foobar | 217{21:foobar foobar ^foobar foobar foobar foobar foobar f}| 218oobar foobar f{21:o}obar foobar foobar foobar foobar fo| 219obar foobar fo{21:o}bar foobar foobar foobar foobar foo| 220bar foobar {21: } | 221| 222]] ) 223-- Entering terminal mode disables 'cursorline' and 'cursorcolumn'. 224feed ( 'i' ) 225screen : expect ( [[ 226tty ready | 227foobar foobar foobar foobar foobar foobar foobar | 228foobar foobar foobar foobar foobar foobar foobar f| 229oobar foobar foobar foobar foobar foobar foobar fo| 230obar foobar foobar foobar foobar foobar foobar foo| 231bar foobar^ | 232{5:-- TERMINAL --} | 233]] ) 234-- Leaving terminal mode restores old values. 235feed ( [[<C-\><C-N>]] ) 236screen : expect ( [[ 237tty ready{21: } | 238foobar f{21:o}obar foobar foobar foobar foobar foobar | 239foobar fo{21:o}bar foobar foobar foobar foobar foobar f| 240oobar foo{21:b}ar foobar foobar foobar foobar foobar fo| 241obar foob{21:a}r foobar foobar foobar foobar foobar foo| 242{21:bar fooba^r }| 243| 244]] ) 245 246-- CursorLine and CursorColumn are combined with terminal colors. 247tt . set_reverse () 248tt . feed_data ( ' foobar' ) 249tt . clear_attrs () 250screen : expect ( [[ 251tty ready{21: } | 252foobar f{21:o}obar foobar foobar foobar foobar foobar | 253foobar fo{21:o}bar foobar foobar foobar foobar foobar f| 254oobar foo{21:b}ar foobar foobar foobar foobar foobar fo| 255obar foob{21:a}r foobar foobar foobar foobar foobar foo| 256{21:bar fooba^r}{100: foobar}{21: }| 257| 258]] ) 259feed ( '2gg15|' ) 260screen : expect ( [[ 261tty ready {21: } | 262{21: foobar foobar^ foobar foobar foobar foobar foobar }| 263foobar foobar {21:f}oobar foobar foobar foobar foobar f| 264oobar foobar f{21:o}obar foobar foobar foobar foobar fo| 265obar foobar fo{21:o}bar foobar foobar foobar foobar foo| 266bar foobar{2: foo}{100:b}{2:ar} | 267| 268]] ) 269 270-- Set bg color to red 271tt . feed_csi ( '48;2;255:0:0m' ) 272tt . feed_data ( ' foobar' ) 273tt . clear_attrs () 274feed ( '2gg20|' ) 275 276-- Terminal color has higher precedence 277screen : expect ( [[ 278tty ready {21: } | 279{21: foobar foobar foob^ar foobar foobar foobar foobar }| 280foobar foobar fooba{21:r} foobar foobar foobar foobar f| 281oobar foobar foobar{21: }foobar foobar foobar foobar fo| 282obar foobar foobar {21:f}oobar foobar foobar foobar foo| 283bar foobar{2: foobar}{30: foobar} | 284| 285]] ) 286feed ( 'G$' ) 287screen : expect ( [[ 288tty ready {21: } | 289foobar foobar foobar f{21:o}obar foobar foobar foobar | 290foobar foobar foobar fo{21:o}bar foobar foobar foobar f| 291oobar foobar foobar foo{21:b}ar foobar foobar foobar fo| 292obar foobar foobar foob{21:a}r foobar foobar foobar foo| 293{21:bar foobar}{100: foobar}{30: fooba^r}{21: }| 294| 295]] ) 296end ) 297 298describe ( ':terminal highlight forwarding' , function () 299local screen 300 301before_each ( function () 302clear () 303screen =Screen . new ( 50 , 7 ) 304screen : set_rgb_cterm ( true ) 305screen : set_default_attr_ids ( { 306[ 1 ] ={ { bold =true } , { bold =true } } , 307[ 2 ] ={ { fg_indexed =true , foreground =tonumber ( '0xe0e000' ) } , { foreground =3 } } , 308[ 3 ] ={ { foreground =tonumber ( '0xff8000' ) } , {} } , 309} ) 310command (( "enew | call jobstart(['%s'], {'term':v:true})" ): format ( testprg ( 'tty-test' ))) 311feed ( 'i' ) 312screen : expect ( [[ 313tty ready | 314^ | 315|*4 316{1:-- TERMINAL --} | 317]] ) 318end ) 319 320it ( 'will handle cterm and rgb attributes' , function () 321skip ( is_os ( 'win' )) 322tt . set_fg ( 3 ) 323tt . feed_data ( 'text' ) 324tt . feed_termcode ( '[38:2:255:128:0m' ) 325tt . feed_data ( 'color' ) 326tt . clear_attrs () 327tt . feed_data ( 'text' ) 328screen : expect ( [[ 329tty ready | 330{2:text}{3:color}text^ | 331|*4 332{1:-- TERMINAL --} | 333]] ) 334end ) 335 336it ( 'handles truecolor SGR with a colour space id' , function () 337skip ( is_os ( 'win' )) 338tt . feed_termcode ( '[38:2::255:128:0m' ) 339tt . feed_data ( 'color' ) 340tt . clear_attrs () 341tt . feed_data ( 'text' ) 342screen : expect ( [[ 343tty ready | 344{3:color}text^ | 345|*4 346{1:-- TERMINAL --} | 347]] ) 348end ) 349end ) 350 351--- @param buflocal boolean 352local function test_term_hl_custom_palette ( buflocal ) 353local screen 354 355before_each ( function () 356clear () 357screen =Screen . new ( 50 , 7 , { rgb =true } ) 358command ( 'enew' ) 359if buflocal then 360api . nvim_buf_set_var ( 0 , 'terminal_color_3' , '#123456' ) 361else 362api . nvim_set_var ( 'terminal_color_3' , '#123456' ) 363end 364screen : add_extra_attr_ids ( { [ 100 ] ={ foreground =tonumber ( '0x123456' ) } } ) 365end ) 366 367it ( 'will use the custom color with jobstart()' , function () 368command (( "call jobstart(['%s'], {'term': v:true})" ): format ( testprg ( 'tty-test' ))) 369feed ( 'i' ) 370screen : expect ( [[ 371tty ready | 372^ | 373|*4 374{5:-- TERMINAL --} | 375]] ) 376tt . set_fg ( 3 ) 377tt . feed_data ( 'text' ) 378tt . clear_attrs () 379tt . feed_data ( 'text' ) 380screen : expect ( [[ 381tty ready | 382{100:text}text^ | 383|*4 384{5:-- TERMINAL --} | 385]] ) 386end ) 387 388it ( 'will use the custom color with nvim_open_term() in non-curbuf' , function () 389local oldbuf =api . nvim_get_current_buf () 390command ( 'set laststatus=0 | vnew' ) 391local chan =api . nvim_open_term ( oldbuf , {} ) 392api . nvim_chan_send ( chan , '\027[38;5;3mtext\027[0;10mtext' ) 393screen : expect ( [[ 394^ │{100:text}text | 395{1:~ }│ |*5 396| 397]] ) 398end ) 399end 400 401describe ( ':terminal highlight with custom palette' , function () 402describe ( 'using g:termimal_color_*' , function () 403test_term_hl_custom_palette ( false ) 404end ) 405 406describe ( 'using b:termimal_color_*' , function () 407test_term_hl_custom_palette ( true ) 408end ) 409end ) 410 411describe ( ':terminal' , function () 412before_each ( clear ) 413 414it ( 'can display URLs' , function () 415local screen =Screen . new ( 50 , 7 ) 416screen : add_extra_attr_ids ( { [ 100 ] ={ url ='https://example.com' } } ) 417local chan =api . nvim_open_term ( 0 , {} ) 418api . nvim_chan_send ( 419chan , 420'This is an \027]8;;https://example.com\027\\example\027]8;;\027\\ of a link' 421) 422screen : expect ( [[ 423^This is an {100:example} of a link | 424|*6 425]] ) 426-- Also works if OSC 8 is split into multiple fragments. 427api . nvim_chan_send ( chan , '\nThis is another \027]8;;https' ) 428n . poke_eventloop () 429api . nvim_chan_send ( chan , '://' ) 430n . poke_eventloop () 431api . nvim_chan_send ( chan , 'example' ) 432n . poke_eventloop () 433api . nvim_chan_send ( chan , '.com\027\\EXAMPLE\027]8;;\027\\ of a link' ) 434screen : expect ( [[ 435^This is an {100:example} of a link | 436This is another {100:EXAMPLE} of a link | 437|*5 438]] ) 439end ) 440 441it ( 'zoomout with large horizontal output #30374' , function () 442skip ( is_os ( 'win' )) 443 444-- Start terminal smaller. 445local screen =Screen . new ( 50 , 50 , { rgb =false } ) 446feed ( [[:terminal<cr>]] ) 447 448-- Generate very wide output. 449feed ( 'ifor i in $(seq 1 10000); do echo -n $i; done\r\n' ) 450 451-- Make terminal big. 452screen : try_resize ( 5000 , 5000 ) 453command ( 'call jobresize(b:terminal_job_id, 5000, 5000)' ) 454 455assert_alive () 456end ) 457end )