@@ -552,7 +552,6 @@ export class GhosttyTerminalSurface {
552552 private mountHeight = 0 ;
553553 private selectionEnd : { x : number ; y : number } | null = null ;
554554 private selectionAnchorScreen : { x : number ; y : number } | null = null ;
555- private selectionEndScreen : { x : number ; y : number } | null = null ;
556555 private selectionMode : "cell" | "word" | "line" = "cell" ;
557556 private selectionBase : {
558557 start : { x : number ; y : number } ;
@@ -953,11 +952,16 @@ export class GhosttyTerminalSurface {
953952 return this . core . selectionText ( ) ;
954953 }
955954
955+ /** An upper bound on the selection's size, read without formatting its text. */
956+ getSelectionCellCount ( ) : number {
957+ const range = this . core . selectionRange ( ) ;
958+ return range ? ( Math . abs ( range . end . y - range . start . y ) + 1 ) * this . cols : 0 ;
959+ }
960+
956961 selectAll ( ) : void {
957962 this . clearPrimedCopy ( ) ;
958963 const range = this . core . selectAll ( ) ;
959964 this . selectionAnchorScreen = range ?. start ?? null ;
960- this . selectionEndScreen = range ?. end ?? null ;
961965 this . selectionEnd = range ? this . core . screenPointToViewport ( range . end . x , range . end . y ) : null ;
962966 this . selectionMode = "cell" ;
963967 this . selectionBase = null ;
@@ -968,15 +972,13 @@ export class GhosttyTerminalSurface {
968972 }
969973
970974 getSelectionPosition ( ) : GhosttySelectionPosition | null {
971- if ( ! this . selectionAnchorScreen || ! this . selectionEndScreen || ! this . hasSelection ( ) )
972- return null ;
973- const before =
974- this . selectionAnchorScreen . y < this . selectionEndScreen . y ||
975- ( this . selectionAnchorScreen . y === this . selectionEndScreen . y &&
976- this . selectionAnchorScreen . x <= this . selectionEndScreen . x ) ;
977- return before
978- ? { start : this . selectionAnchorScreen , end : this . selectionEndScreen }
979- : { start : this . selectionEndScreen , end : this . selectionAnchorScreen } ;
975+ // Ask Ghostty rather than the gesture's coordinates, which go stale once
976+ // output scrolls the selected rows (alternate screen, scrollback pruning).
977+ const range = this . hasSelection ( ) ? this . core . selectionRange ( ) : null ;
978+ if ( ! range ) return null ;
979+ const { start, end } = range ;
980+ const before = start . y < end . y || ( start . y === end . y && start . x <= end . x ) ;
981+ return before ? { start, end } : { start : end , end : start } ;
980982 }
981983
982984 getSelectionEndClientRect ( ) : { readonly right : number ; readonly bottom : number } | null {
@@ -1004,7 +1006,6 @@ export class GhosttyTerminalSurface {
10041006 this . core . clearSelection ( ) ;
10051007 this . selectionEnd = null ;
10061008 this . selectionAnchorScreen = null ;
1007- this . selectionEndScreen = null ;
10081009 this . selectionMode = "cell" ;
10091010 this . selectionBase = null ;
10101011 this . setSelectionAutoscroll ( 0 ) ;
@@ -1404,15 +1405,13 @@ export class GhosttyTerminalSurface {
14041405 this . selectionBase = range . screen ;
14051406 this . selectionEnd = range . viewport . end ;
14061407 this . selectionAnchorScreen = range . screen . start ;
1407- this . selectionEndScreen = range . screen . end ;
14081408 this . options . onSelectionChange ( ) ;
14091409 } else {
14101410 this . selectionMode = "cell" ;
14111411 this . selectionBase = null ;
14121412 this . selectionEnd = cell ;
14131413 const screen = this . core . viewportPointToScreen ( cell . x , cell . y ) ;
14141414 this . selectionAnchorScreen = screen ;
1415- this . selectionEndScreen = screen ;
14161415 if ( screen ) {
14171416 this . core . setSelection ( { ...screen , tag : 2 } , { ...screen , tag : 2 } ) ;
14181417 } else {
@@ -1495,8 +1494,20 @@ export class GhosttyTerminalSurface {
14951494 } ;
14961495
14971496 private extendSelectionTo ( clientX : number , clientY : number ) : void {
1498- const anchorScreen = this . selectionAnchorScreen ;
1499- if ( anchorScreen === null ) return ;
1497+ if ( this . selectionAnchorScreen === null ) return ;
1498+ // Output can scroll the selected rows mid-drag. Ghostty keeps the selection,
1499+ // anchor first, on its rows, so the gesture's anchor and word/line base
1500+ // move with it instead of reselecting from where the rows used to be.
1501+ const anchorScreen = this . core . selectionRange ( ) ?. start ;
1502+ if ( ! anchorScreen ) return ;
1503+ const rowDelta = anchorScreen . y - this . selectionAnchorScreen . y ;
1504+ if ( this . selectionBase && rowDelta !== 0 ) {
1505+ const { start, end } = this . selectionBase ;
1506+ this . selectionBase = {
1507+ start : { x : start . x , y : start . y + rowDelta } ,
1508+ end : { x : end . x , y : end . y + rowDelta } ,
1509+ } ;
1510+ }
15001511 const cell = this . cellAt ( clientX , clientY ) ;
15011512 this . selectionMoved = true ;
15021513 this . selectionEnd = cell ;
@@ -1516,7 +1527,6 @@ export class GhosttyTerminalSurface {
15161527 const anchor = base === null ? anchorScreen : beforeBase ? base . end : base . start ;
15171528 const end = range === null ? cellScreen : beforeBase ? range . screen . start : range . screen . end ;
15181529 this . selectionAnchorScreen = anchor ;
1519- this . selectionEndScreen = end ;
15201530 this . core . setSelection ( { ...anchor , tag : 2 } , { ...end , tag : 2 } ) ;
15211531 this . options . onSelectionChange ( ) ;
15221532 this . forceFullRender = true ;
0 commit comments