Files

38 lines
1015 B
Go

// Copyright (c) 2026 Storm Dragon
// SPDX-License-Identifier: GPL-3.0-or-later
package screen
import "strings"
// Region uses inclusive screen-cell coordinates, independently of text width
// in bytes. ToLineEnd follows the current right edge when the terminal resizes.
// Other coordinates stay fixed and are clipped only while reading/filtering.
type Region struct {
Left, Top, Right, Bottom int
ToLineEnd bool
}
func (r Region) Right_edge(cols int) int {
if r.ToLineEnd {
return cols - 1
}
return min(r.Right, cols-1)
}
func (r Region) Contains(x, y, cols int) bool {
return x >= r.Left && x <= r.Right_edge(cols) && y >= r.Top && y <= r.Bottom
}
func (r Region) Text(s Snapshot) string {
right := r.Right_edge(s.Cols)
if r.Left > right || r.Top >= s.Rows {
return ""
}
var lines []string
for row := r.Top; row <= min(r.Bottom, s.Rows-1); row++ {
lines = append(lines, strings.TrimRight(string(s.Line(row)[r.Left:right+1]), " \x00"))
}
return strings.Join(lines, "\n")
}