robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each =t . describe , t . it , t . before_each 5local eq =t . eq 6 7local clear =n . clear 8local exec_lua =n . exec_lua 9 10---4x4 PNG image bytes. 11---@type string 12-- stylua: ignore 13local PNG_IMG_BYTES =string . char ( unpack ( { 14137 , 80 , 78 , 71 , 13 , 10 , 26 , 10 , 0 , 0 , 0 , 13 , 73 , 72 , 68 , 82 , 0 , 0 , 0 , 4 , 0 , 150 , 0 , 4 , 8 , 6 , 0 , 0 , 0 , 169 , 241 , 158 , 126 , 0 , 0 , 0 , 1 , 115 , 82 , 71 , 66 , 0 , 16174 , 206 , 28 , 233 , 0 , 0 , 0 , 39 , 73 , 68 , 65 , 84 , 8 , 153 , 99 , 252 , 207 , 192 , 17240 , 159 , 129 , 129 , 129 , 193 , 226 , 63 , 3 , 3 , 3 , 3 , 3 , 3 , 19 , 3 , 26 , 96 , 97 , 18156 , 1 , 145 , 250 , 207 , 184 , 12 , 187 , 10 , 0 , 36 , 189 , 6 , 125 , 75 , 9 , 40 , 46 , 190 , 0 , 0 , 0 , 73 , 69 , 78 , 68 , 174 , 66 , 96 , 130 , 20} )) 21 22---@param s string 23---@return string 24local function escape_ansi ( s ) 25return ( 26string . gsub ( s , '.' , function ( c ) 27local byte =string . byte ( c ) 28if byte < 32 or byte == 127 then 29return string . format ( '\\%03d' , byte ) 30else 31return c 32end 33end ) 34) 35end 36 37---@param s string 38---@return string 39local function base64_encode ( s ) 40return exec_lua ( function () 41return vim . base64 . encode ( s ) 42end ) 43end 44 45---Mock nvim_ui_send to capture escape sequence output. 46local function setup_img_api () 47exec_lua ( function () 48_G . data ={} 49local original_ui_send =vim . api . nvim_ui_send 50vim . api . nvim_ui_send =function ( d ) 51table . insert ( _G . data , d ) 52end 53_G . _original_ui_send =original_ui_send 54end ) 55end 56 57---@param esc string 58---@param opts? {strict?:boolean} 59---@return {i:integer, j:integer, control:table<string, string>, data:string|nil} 60local function parse_kitty_seq ( esc , opts ) 61opts =opts or {} 62local i , j , c , d =string . find ( esc , '\027_G([^;\027]+)([^\027]*)\027\\' ) 63assert ( c , 'invalid kitty escape sequence: ' .. escape_ansi ( esc )) 64 65if opts . strict then 66assert ( i == 1 , 'not starting with kitty graphics sequence: ' .. escape_ansi ( esc )) 67end 68 69---@type table<string, string> 70local control ={} 71local idx =0 72while true do 73local k , v , _ 74idx , _ , k , v =string . find ( c , '(%a+)=([^,]+),?' , idx + 1 ) 75if idx == nil then 76break 77end 78if k and v then 79control [ k ] =v 80end 81end 82 83---@type string|nil 84local payload 85if d and d ~= '' then 86payload =string . sub ( d , 2 ) 87end 88 89return { i =i , j =j , control =control , data =payload } 90end 91 92describe ( 'vim.ui.img' , function () 93before_each ( function () 94clear () 95setup_img_api () 96end ) 97 98it ( 'can set an image' , function () 99local esc_codes =exec_lua ( function () 100_G . data ={} 101vim . ui . img . set ( PNG_IMG_BYTES , { 102col =1 , 103row =2 , 104width =3 , 105height =4 , 106zindex =123 , 107} ) 108return table . concat ( _G . data ) 109end ) 110 111-- Transmit image bytes 112local seq =parse_kitty_seq ( esc_codes , { strict =true } ) 113local image_id =seq . control . i 114eq ( { 115f ='100' , 116a ='t' , 117t ='d' , 118i =image_id , 119q ='2' , 120m ='0' , 121} , seq . control , 'transmit image control data' ) 122eq ( base64_encode ( PNG_IMG_BYTES ), seq . data ) 123esc_codes =string . sub ( esc_codes , seq . j + 1 ) 124 125-- Cursor save 126eq ( escape_ansi ( '\0277' ), escape_ansi ( string . sub ( esc_codes , 1 , 2 )), 'cursor save' ) 127esc_codes =string . sub ( esc_codes , 3 ) 128 129-- Cursor hide 130eq ( escape_ansi ( '\027[?25l' ), escape_ansi ( string . sub ( esc_codes , 1 , 6 )), 'cursor hide' ) 131esc_codes =string . sub ( esc_codes , 7 ) 132 133-- Cursor move 134eq ( escape_ansi ( '\027[2;1H' ), escape_ansi ( string . sub ( esc_codes , 1 , 6 )), 'cursor movement' ) 135esc_codes =string . sub ( esc_codes , 7 ) 136 137-- Place image 138seq =parse_kitty_seq ( esc_codes , { strict =true } ) 139eq ( { 140a ='p' , 141i =image_id , 142p =seq . control . p , 143C ='1' , 144q ='2' , 145c ='3' , 146r ='4' , 147z ='123' , 148} , seq . control , 'display image control data' ) 149esc_codes =string . sub ( esc_codes , seq . j + 1 ) 150 151-- Cursor restore 152eq ( escape_ansi ( '\0278' ), escape_ansi ( string . sub ( esc_codes , 1 , 2 )), 'cursor restore' ) 153esc_codes =string . sub ( esc_codes , 3 ) 154 155-- Cursor show 156eq ( escape_ansi ( '\027[?25h' ), escape_ansi ( string . sub ( esc_codes , 1 , 6 )), 'cursor show' ) 157end ) 158 159it ( 'can get image info' , function () 160local result =exec_lua ( function () 161local id =vim . ui . img . set ( PNG_IMG_BYTES , { 162row =5 , 163col =10 , 164width =20 , 165height =15 , 166zindex =42 , 167} ) 168 169return { 170info =vim . ui . img . get ( id ), 171missing =vim . ui . img . get ( 999999 ), 172} 173end ) 174 175eq ( { row =5 , col =10 , width =20 , height =15 , zindex =42 } , result . info ) 176eq ( nil , result . missing ) 177end ) 178 179it ( 'can update an image' , function () 180local result =exec_lua ( function () 181local id =vim . ui . img . set ( PNG_IMG_BYTES , { 182row =1 , 183col =1 , 184width =10 , 185height =20 , 186zindex =99 , 187} ) 188 189_G . data ={} 190vim . ui . img . set ( id , { 191col =5 , 192row =6 , 193width =7 , 194height =8 , 195zindex =9 , 196} ) 197local esc_codes =table . concat ( _G . data ) 198 199-- Partial update: only change row, other fields preserved 200vim . ui . img . set ( id , { row =50 } ) 201local info =vim . ui . img . get ( id ) 202 203return { esc_codes =esc_codes , info =info } 204end ) 205 206-- Verify partial update merged opts 207eq ( { row =50 , col =5 , width =7 , height =8 , zindex =9 } , result . info ) 208 209local esc_codes =result . esc_codes 210 211-- Cursor save 212eq ( escape_ansi ( '\0277' ), escape_ansi ( string . sub ( esc_codes , 1 , 2 )), 'cursor save' ) 213esc_codes =string . sub ( esc_codes , 3 ) 214 215-- Cursor hide 216eq ( escape_ansi ( '\027[?25l' ), escape_ansi ( string . sub ( esc_codes , 1 , 6 )), 'cursor hide' ) 217esc_codes =string . sub ( esc_codes , 7 ) 218 219-- Cursor move to new position 220eq ( escape_ansi ( '\027[6;5H' ), escape_ansi ( string . sub ( esc_codes , 1 , 6 )), 'cursor movement' ) 221esc_codes =string . sub ( esc_codes , 7 ) 222 223-- Place command reuses same placement ID (flicker-free update) 224local seq =parse_kitty_seq ( esc_codes , { strict =true } ) 225eq ( { 226a ='p' , 227i =seq . control . i , 228p =seq . control . p , 229C ='1' , 230q ='2' , 231c ='7' , 232r ='8' , 233z ='9' , 234} , seq . control , 'update image control data' ) 235esc_codes =string . sub ( esc_codes , seq . j + 1 ) 236 237-- Cursor restore 238eq ( escape_ansi ( '\0278' ), escape_ansi ( string . sub ( esc_codes , 1 , 2 )), 'cursor restore' ) 239esc_codes =string . sub ( esc_codes , 3 ) 240 241-- Cursor show 242eq ( escape_ansi ( '\027[?25h' ), escape_ansi ( string . sub ( esc_codes , 1 , 6 )), 'cursor show' ) 243end ) 244 245it ( 'can delete all images' , function () 246local result =exec_lua ( function () 247local id1 =vim . ui . img . set ( PNG_IMG_BYTES , { row =1 , col =1 } ) 248local id2 =vim . ui . img . set ( PNG_IMG_BYTES , { row =2 , col =2 } ) 249 250_G . data ={} 251local deleted =vim . ui . img . del ( math . huge ) 252return { 253esc_codes =table . concat ( _G . data ), 254deleted =deleted , 255after_id1 =vim . ui . img . get ( id1 ), 256after_id2 =vim . ui . img . get ( id2 ), 257not_deleted =vim . ui . img . del ( math . huge ), -- nothing to delete 258} 259end ) 260 261local seq =parse_kitty_seq ( result . esc_codes , { strict =true } ) 262eq ( { a ='d' , d ='A' , q ='2' } , seq . control , 'delete all control data' ) 263 264eq ( true , result . deleted ) 265eq ( nil , result . after_id1 ) 266eq ( nil , result . after_id2 ) 267eq ( false , result . not_deleted ) 268end ) 269 270it ( 'can delete an image' , function () 271local result =exec_lua ( function () 272local id =vim . ui . img . set ( PNG_IMG_BYTES , { row =1 , col =1 } ) 273 274_G . data ={} 275local found =vim . ui . img . del ( id ) 276local after =vim . ui . img . get ( id ) 277local not_found =vim . ui . img . del ( id ) 278 279return { 280esc_codes =table . concat ( _G . data ), 281found =found , 282after =after , 283not_found =not_found , 284} 285end ) 286 287local seq =parse_kitty_seq ( result . esc_codes , { strict =true } ) 288eq ( { 289a ='d' , 290d ='i' , 291i =seq . control . i , 292q ='2' , 293} , seq . control , 'delete image' ) 294 295eq ( true , result . found ) 296eq ( nil , result . after ) 297eq ( false , result . not_found ) 298end ) 299end )