@@ -554,7 +554,6 @@ export class GhosttyTerminalSurface {
554554 private mountHeight = 0 ;
555555 private selectionEnd : { x : number ; y : number } | null = null ;
556556 private selectionAnchorScreen : { x : number ; y : number } | null = null ;
557- private selectionEndScreen : { x : number ; y : number } | null = null ;
558557 private selectionMode : "cell" | "word" | "line" = "cell" ;
559558 private selectionBase : {
560559 start : { x : number ; y : number } ;
@@ -955,11 +954,16 @@ export class GhosttyTerminalSurface {
955954 return this . core . selectionText ( ) ;
956955 }
957956
957+ /** An upper bound on the selection's size, read without formatting its text. */
958+ getSelectionCellCount ( ) : number {
959+ const range = this . core . selectionRange ( ) ;
960+ return range ? ( Math . abs ( range . end . y - range . start . y ) + 1 ) * this . cols : 0 ;
961+ }
962+
958963 selectAll ( ) : void {
959964 this . clearPrimedCopy ( ) ;
960965 const range = this . core . selectAll ( ) ;
961966 this . selectionAnchorScreen = range ?. start ?? null ;
962- this . selectionEndScreen = range ?. end ?? null ;
963967 this . selectionEnd = range ? this . core . screenPointToViewport ( range . end . x , range . end . y ) : null ;
964968 this . selectionMode = "cell" ;
965969 this . selectionBase = null ;
@@ -970,15 +974,13 @@ export class GhosttyTerminalSurface {
970974 }
971975
972976 getSelectionPosition ( ) : GhosttySelectionPosition | null {
973- if ( ! this . selectionAnchorScreen || ! this . selectionEndScreen || ! this . hasSelection ( ) )
974- return null ;
975- const before =
976- this . selectionAnchorScreen . y < this . selectionEndScreen . y ||
977- ( this . selectionAnchorScreen . y === this . selectionEndScreen . y &&
978- this . selectionAnchorScreen . x <= this . selectionEndScreen . x ) ;
979- return before
980- ? { start : this . selectionAnchorScreen , end : this . selectionEndScreen }
981- : { start : this . selectionEndScreen , end : this . selectionAnchorScreen } ;
977+ // Ask Ghostty rather than the gesture's coordinates, which go stale once
978+ // output scrolls the selected rows (alternate screen, scrollback pruning).
979+ const range = this . hasSelection ( ) ? this . core . selectionRange ( ) : null ;
980+ if ( ! range ) return null ;
981+ const { start, end } = range ;
982+ const before = start . y < end . y || ( start . y === end . y && start . x <= end . x ) ;
983+ return before ? { start, end } : { start : end , end : start } ;
982984 }
983985
984986 getSelectionEndClientRect ( ) : { readonly right : number ; readonly bottom : number } | null {
@@ -1006,7 +1008,6 @@ export class GhosttyTerminalSurface {
10061008 this . core . clearSelection ( ) ;
10071009 this . selectionEnd = null ;
10081010 this . selectionAnchorScreen = null ;
1009- this . selectionEndScreen = null ;
10101011 this . selectionMode = "cell" ;
10111012 this . selectionBase = null ;
10121013 this . setSelectionAutoscroll ( 0 ) ;
@@ -1406,15 +1407,13 @@ export class GhosttyTerminalSurface {
14061407 this . selectionBase = range . screen ;
14071408 this . selectionEnd = range . viewport . end ;
14081409 this . selectionAnchorScreen = range . screen . start ;
1409- this . selectionEndScreen = range . screen . end ;
14101410 this . options . onSelectionChange ( ) ;
14111411 } else {
14121412 this . selectionMode = "cell" ;
14131413 this . selectionBase = null ;
14141414 this . selectionEnd = cell ;
14151415 const screen = this . core . viewportPointToScreen ( cell . x , cell . y ) ;
14161416 this . selectionAnchorScreen = screen ;
1417- this . selectionEndScreen = screen ;
14181417 if ( screen ) {
14191418 this . core . setSelection ( { ...screen , tag : 2 } , { ...screen , tag : 2 } ) ;
14201419 } else {
@@ -1497,8 +1496,20 @@ export class GhosttyTerminalSurface {
14971496 } ;
14981497
14991498 private extendSelectionTo ( clientX : number , clientY : number ) : void {
1500- const anchorScreen = this . selectionAnchorScreen ;
1501- if ( anchorScreen === null ) return ;
1499+ if ( this . selectionAnchorScreen === null ) return ;
1500+ // Output can scroll the selected rows mid-drag. Ghostty keeps the selection,
1501+ // anchor first, on its rows, so the gesture's anchor and word/line base
1502+ // move with it instead of reselecting from where the rows used to be.
1503+ const anchorScreen = this . core . selectionRange ( ) ?. start ;
1504+ if ( ! anchorScreen ) return ;
1505+ const rowDelta = anchorScreen . y - this . selectionAnchorScreen . y ;
1506+ if ( this . selectionBase && rowDelta !== 0 ) {
1507+ const { start, end } = this . selectionBase ;
1508+ this . selectionBase = {
1509+ start : { x : start . x , y : start . y + rowDelta } ,
1510+ end : { x : end . x , y : end . y + rowDelta } ,
1511+ } ;
1512+ }
15021513 const cell = this . cellAt ( clientX , clientY ) ;
15031514 this . selectionMoved = true ;
15041515 this . selectionEnd = cell ;
@@ -1518,7 +1529,6 @@ export class GhosttyTerminalSurface {
15181529 const anchor = base === null ? anchorScreen : beforeBase ? base . end : base . start ;
15191530 const end = range === null ? cellScreen : beforeBase ? range . screen . start : range . screen . end ;
15201531 this . selectionAnchorScreen = anchor ;
1521- this . selectionEndScreen = end ;
15221532 this . core . setSelection ( { ...anchor , tag : 2 } , { ...end , tag : 2 } ) ;
15231533 this . options . onSelectionChange ( ) ;
15241534 this . forceFullRender = true ;
0 commit comments