Files

63 lines
1.9 KiB
Go

// Fenrir console screen reader.
// Copyright (c) 2026 Storm Dragon
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
package reader
import (
"strings"
"git.stormux.org/storm/fenrir/internal/presentation"
"git.stormux.org/storm/fenrir/internal/screen"
)
// Track newly emphasized spans, excluding the formerly selected row returning
// to its common appearance. No attributes means no highlight evidence.
func highlights(previous, next screen.Snapshot) []presentation.Message {
if len(previous.Attributes) != len(next.Cells) || len(next.Attributes) != len(next.Cells) {
return nil
}
counts := make(map[screen.Attribute]int)
for _, attribute := range next.Attributes {
counts[attribute]++
}
var result []presentation.Message
for row := range next.Rows {
start := -1
flush := func(end int) {
if start >= 0 {
if text := strings.TrimSpace(string(next.Line(row)[start:end])); text != "" {
result = append(result, presentation.Message{Text: text})
}
start = -1
}
}
for col := range next.Cols {
index := row*next.Cols + col
before, after := previous.Attributes[index], next.Attributes[index]
if before != after && counts[after] < counts[before] {
if start < 0 {
start = col
}
} else {
flush(col)
}
}
flush(next.Cols)
}
return result
}