@@ -573,7 +573,6 @@ export class GhosttyTerminalSurface {
573573 private mountHeight = 0 ;
574574 private selectionEnd : { x : number ; y : number } | null = null ;
575575 private selectionAnchorScreen : { x : number ; y : number } | null = null ;
576- private selectionEndScreen : { x : number ; y : number } | null = null ;
577576 private selectionMode : "cell" | "word" | "line" = "cell" ;
578577 private selectionBase : {
579578 start : { x : number ; y : number } ;
@@ -985,11 +984,16 @@ export class GhosttyTerminalSurface {
985984 return this . core . selectionText ( ) ;
986985 }
987986
987+ /** An upper bound on the selection's size, read without formatting its text. */
988+ getSelectionCellCount ( ) : number {
989+ const range = this . core . selectionRange ( ) ;
990+ return range ? ( Math . abs ( range . end . y - range . start . y ) + 1 ) * this . cols : 0 ;
991+ }
992+
988993 selectAll ( ) : void {
989994 this . clearPrimedCopy ( ) ;
990995 const range = this . core . selectAll ( ) ;
991996 this . selectionAnchorScreen = range ?. start ?? null ;
992- this . selectionEndScreen = range ?. end ?? null ;
993997 this . selectionEnd = range ? this . core . screenPointToViewport ( range . end . x , range . end . y ) : null ;
994998 this . selectionMode = "cell" ;
995999 this . selectionBase = null ;
@@ -1000,15 +1004,13 @@ export class GhosttyTerminalSurface {
10001004 }
10011005
10021006 getSelectionPosition ( ) : GhosttySelectionPosition | null {
1003- if ( ! this . selectionAnchorScreen || ! this . selectionEndScreen || ! this . hasSelection ( ) )
1004- return null ;
1005- const before =
1006- this . selectionAnchorScreen . y < this . selectionEndScreen . y ||
1007- ( this . selectionAnchorScreen . y === this . selectionEndScreen . y &&
1008- this . selectionAnchorScreen . x <= this . selectionEndScreen . x ) ;
1009- return before
1010- ? { start : this . selectionAnchorScreen , end : this . selectionEndScreen }
1011- : { start : this . selectionEndScreen , end : this . selectionAnchorScreen } ;
1007+ // Ask Ghostty rather than the gesture's coordinates, which go stale once
1008+ // output scrolls the selected rows (alternate screen, scrollback pruning).
1009+ const range = this . hasSelection ( ) ? this . core . selectionRange ( ) : null ;
1010+ if ( ! range ) return null ;
1011+ const { start, end } = range ;
1012+ const before = start . y < end . y || ( start . y === end . y && start . x <= end . x ) ;
1013+ return before ? { start, end } : { start : end , end : start } ;
10121014 }
10131015
10141016 getSelectionEndClientRect ( ) : { readonly right : number ; readonly bottom : number } | null {
@@ -1036,7 +1038,6 @@ export class GhosttyTerminalSurface {
10361038 this . core . clearSelection ( ) ;
10371039 this . selectionEnd = null ;
10381040 this . selectionAnchorScreen = null ;
1039- this . selectionEndScreen = null ;
10401041 this . selectionMode = "cell" ;
10411042 this . selectionBase = null ;
10421043 this . setSelectionAutoscroll ( 0 ) ;
@@ -1421,15 +1422,13 @@ export class GhosttyTerminalSurface {
14211422 this . selectionBase = range . screen ;
14221423 this . selectionEnd = range . viewport . end ;
14231424 this . selectionAnchorScreen = range . screen . start ;
1424- this . selectionEndScreen = range . screen . end ;
14251425 this . options . onSelectionChange ( ) ;
14261426 } else {
14271427 this . selectionMode = "cell" ;
14281428 this . selectionBase = null ;
14291429 this . selectionEnd = cell ;
14301430 const screen = this . core . viewportPointToScreen ( cell . x , cell . y ) ;
14311431 this . selectionAnchorScreen = screen ;
1432- this . selectionEndScreen = screen ;
14331432 if ( screen ) {
14341433 this . core . setSelection ( { ...screen , tag : 2 } , { ...screen , tag : 2 } ) ;
14351434 } else {
@@ -1511,8 +1510,18 @@ export class GhosttyTerminalSurface {
15111510 } ;
15121511
15131512 private extendSelectionTo ( clientX : number , clientY : number ) : void {
1514- const anchorScreen = this . selectionAnchorScreen ;
1515- if ( anchorScreen === null ) return ;
1513+ if ( this . selectionAnchorScreen === null ) return ;
1514+ // Ghostty moves its selection with output; keep the drag on those rows too.
1515+ const anchorScreen = this . core . selectionRange ( ) ?. start ;
1516+ if ( ! anchorScreen ) return ;
1517+ const rowDelta = anchorScreen . y - this . selectionAnchorScreen . y ;
1518+ if ( this . selectionBase && rowDelta !== 0 ) {
1519+ const { start, end } = this . selectionBase ;
1520+ this . selectionBase = {
1521+ start : { x : start . x , y : start . y + rowDelta } ,
1522+ end : { x : end . x , y : end . y + rowDelta } ,
1523+ } ;
1524+ }
15161525 const cell = this . cellAt ( clientX , clientY ) ;
15171526 this . selectionMoved = true ;
15181527 this . selectionEnd = cell ;
@@ -1532,7 +1541,6 @@ export class GhosttyTerminalSurface {
15321541 const anchor = base === null ? anchorScreen : beforeBase ? base . end : base . start ;
15331542 const end = range === null ? cellScreen : beforeBase ? range . screen . start : range . screen . end ;
15341543 this . selectionAnchorScreen = anchor ;
1535- this . selectionEndScreen = end ;
15361544 this . core . setSelection ( { ...anchor , tag : 2 } , { ...end , tag : 2 } ) ;
15371545 this . options . onSelectionChange ( ) ;
15381546 this . forceFullRender = true ;
0 commit comments