okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
22a3d7068
main
1/** 2* A chat title cut from the first prompt without cutting a character in half (#4533). 3* 4* `prompt.slice(0, 47)` counts UTF-16 units, so an emoji (two units), a flag (four), a family or a skin-tone sequence (up to eleven) that 5* straddles the cut leaves half a character: a lone surrogate that draws as a replacement box, or a ZWJ with nothing after it. The title is 6* stored and restored as it is, so the broken tail comes back on every start and goes through the font fallback that WebKitGTK crashes in. 7* This cuts at a user-perceived character (grapheme) when the engine has Intl.Segmenter, and at a code point when it has not. 8* 9* The three `prompt.length > 48 ? prompt.slice(0, 47).trimEnd() + '…' : prompt` places in lib/store.ts should call `shortTitle(prompt)`. 10*/ 11const WIDTH = 48 ; 12 13export function shortTitle ( text :string , width = WIDTH ) :string { 14if ( text . length <= width ) 15return text ; 16const room = width - 1 ; // the ellipsis takes one unit 17let end = 0 ; 18const Segmenter = ( Intl as unknown as { Segmenter ?:new ( locale ?:string , options ?:{ granularity :string }) => { segment ( input :string ) :Iterable <{ segment :string ; index :number }> } }). Segmenter ; 19if ( Segmenter ) { 20for ( const part of new Segmenter ( undefined , { granularity :'grapheme' }). segment ( text )) { 21if ( part . index + part . segment . length > room ) 22break ; 23end = part . index + part . segment . length ; 24} 25} 26else { 27for ( const ch of text ) { // iterates by code point 28if ( end + ch . length > room ) 29break ; 30end += ch . length ; 31} 32} 33return text . slice ( 0 , end ). trimEnd () + '…' ; 34}