After commit c018f5fd29 Fenrir is being switched over to go. If you do not want to be part of the testing process, please switch to master.
This commit is contained in:
1 parent
c018f5fd29
commit
0e50de4c76
1047 files changed
+58364
-63747
No files matched your search
@@ -0,0 +1,160 @@
|
||||
// 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 progress
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.stormux.org/storm/fenrir/internal/diagnostic"
|
||||
"git.stormux.org/storm/fenrir/internal/screen"
|
||||
)
|
||||
|
||||
// Monitor owns detection state on the reader loop. No goroutines, subprocesses,
|
||||
// whole-screen strings, or regexp compilation are needed for an update.
|
||||
type Monitor struct {
|
||||
active bool
|
||||
pending *Indicator
|
||||
last Indicator
|
||||
lastAt time.Time
|
||||
}
|
||||
|
||||
func (m *Monitor) Reset() { *m = Monitor{} }
|
||||
|
||||
// Update ignores editingRow when a screen-confirmed input deletion changed it.
|
||||
// Pass -1 when no row should be excluded. Other output remains eligible.
|
||||
func (m *Monitor) Update(previous, next screen.Snapshot, editingRow int) {
|
||||
reason := "session-change"
|
||||
changedCount, retainedCount, candidateCount := 0, 0, 0
|
||||
if diagnostic.Enabled() {
|
||||
defer func() {
|
||||
diagnostic.Record("progress detection", "reason", reason, "changed_rows", changedCount, "retained_rows", retainedCount, "candidate_rows", candidateCount)
|
||||
}()
|
||||
}
|
||||
if !next.Active || !previous.Active || previous.Terminal != next.Terminal || previous.Generation != next.Generation || previous.Rows != next.Rows || previous.Cols != next.Cols || next.Rows == 0 || next.Cols == 0 {
|
||||
m.Reset()
|
||||
return
|
||||
}
|
||||
reason = "prompt"
|
||||
if promptPattern.MatchString(next.Text(next.Y)) || promptPattern.MatchString(next.Text(next.Rows-1)) {
|
||||
m.Reset()
|
||||
return
|
||||
}
|
||||
reason = "cursor-jump"
|
||||
if next.Y != previous.Y && (abs(next.Y-previous.Y) > 2 || abs(next.X-previous.X) > 20) {
|
||||
m.Reset()
|
||||
return
|
||||
}
|
||||
// A cursor move over unchanged text is navigation, not ongoing work.
|
||||
reason = "unchanged"
|
||||
if slices.Equal(previous.Cells, next.Cells) {
|
||||
if previous.X != next.X || previous.Y != next.Y {
|
||||
m.Reset()
|
||||
}
|
||||
return
|
||||
}
|
||||
// Scrolling moves old output through many rows. Remove the retained
|
||||
// overlap before applying size/multiline filters to genuinely new output.
|
||||
var changedRows []int
|
||||
for row := range next.Rows {
|
||||
if !slices.Equal(previous.Line(row), next.Line(row)) {
|
||||
changedRows = append(changedRows, row)
|
||||
}
|
||||
}
|
||||
changedCount = len(changedRows)
|
||||
scrollStart, scrollEnd := 0, 0
|
||||
if len(changedRows) > 1 {
|
||||
scrollStart, scrollEnd = screen.Scrolled_overlap(previous, next)
|
||||
}
|
||||
var lines []string
|
||||
length := 0
|
||||
for _, row := range changedRows {
|
||||
if row == editingRow {
|
||||
continue
|
||||
}
|
||||
if row >= scrollStart && row < scrollEnd {
|
||||
retainedCount++
|
||||
continue
|
||||
}
|
||||
line := next.Text(row)
|
||||
// Erased rows are not new multiline output (common after a scroll).
|
||||
if strings.TrimSpace(line) == "" {
|
||||
continue
|
||||
}
|
||||
lines = append(lines, line)
|
||||
length += len([]rune(line))
|
||||
}
|
||||
reason = "no-pattern"
|
||||
if editingRow >= 0 && len(lines) == 0 {
|
||||
reason = "editing"
|
||||
}
|
||||
var selected Indicator
|
||||
found := false
|
||||
for _, line := range lines {
|
||||
if (length > 200 || len(lines) > 1) && !explicit(line) {
|
||||
reason = "repaint-filter"
|
||||
continue
|
||||
}
|
||||
candidateCount++
|
||||
if value, ok := Detect(strings.TrimSpace(line)); ok {
|
||||
selected, found = value, true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
m.Reset()
|
||||
return
|
||||
}
|
||||
// Replace pending output even when throttled. The final value is flushed by
|
||||
// the loop's existing timer if the program finishes between two audio ticks.
|
||||
reason = "measured"
|
||||
if selected.Interval != 0 {
|
||||
reason = "activity"
|
||||
}
|
||||
m.active = true
|
||||
m.pending = &selected
|
||||
}
|
||||
|
||||
func (m *Monitor) Next(now time.Time) (float64, bool) {
|
||||
if m.pending == nil {
|
||||
return 0, false
|
||||
}
|
||||
value := *m.pending
|
||||
interval := value.Interval
|
||||
if interval == 0 {
|
||||
interval = 100 * time.Millisecond
|
||||
}
|
||||
if !m.lastAt.IsZero() && now.Sub(m.lastAt) < interval {
|
||||
return 0, false
|
||||
}
|
||||
m.pending = nil
|
||||
if value.Interval == 0 && !m.lastAt.IsZero() && value == m.last {
|
||||
return 0, false
|
||||
}
|
||||
m.last, m.lastAt = value, now
|
||||
return value.Percentage, true
|
||||
}
|
||||
|
||||
func abs(value int) int {
|
||||
if value < 0 {
|
||||
return -value
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func (m *Monitor) Active() bool { return m.active }
|
||||
@@ -0,0 +1,155 @@
|
||||
// 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 progress
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.stormux.org/storm/fenrir/internal/screen"
|
||||
)
|
||||
|
||||
func snapshot(lines ...string) screen.Snapshot {
|
||||
s := screen.Snapshot{Terminal: 1, Active: true, Rows: len(lines), Cols: 300}
|
||||
for _, line := range lines {
|
||||
s.Cells = append(s.Cells, []rune(line+strings.Repeat(" ", s.Cols-len([]rune(line))))...)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func TestWholeRowThrottlingAndFinalValue(t *testing.T) {
|
||||
var m Monitor
|
||||
now := time.Unix(100, 0)
|
||||
before := snapshot("Download 49%", "")
|
||||
after := snapshot("Download 50%", "")
|
||||
m.Update(before, after, -1)
|
||||
if value, ok := m.Next(now); !ok || value != 50 {
|
||||
t.Fatal(value, ok)
|
||||
}
|
||||
for percentage := 51; percentage <= 100; percentage++ {
|
||||
next := snapshot("Download "+format_percent(percentage), "")
|
||||
m.Update(after, next, -1)
|
||||
after = next
|
||||
}
|
||||
if _, ok := m.Next(now.Add(50 * time.Millisecond)); ok {
|
||||
t.Fatal("flood not throttled")
|
||||
}
|
||||
if value, ok := m.Next(now.Add(100 * time.Millisecond)); !ok || value != 100 {
|
||||
t.Fatal("lost final pending value", value, ok)
|
||||
}
|
||||
if _, ok := m.Next(now.Add(time.Second)); ok {
|
||||
t.Fatal("repeated without output")
|
||||
}
|
||||
next := snapshot("Download 100% complete", "")
|
||||
m.Update(after, next, -1)
|
||||
if _, ok := m.Next(now.Add(time.Second)); ok {
|
||||
t.Fatal("duplicate percentage")
|
||||
}
|
||||
// A new operation may decrease; completion never disables monitoring.
|
||||
m.Update(next, snapshot("Download 1%", ""), -1)
|
||||
if value, ok := m.Next(now.Add(2 * time.Second)); !ok || value != 1 {
|
||||
t.Fatal(value, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMonitorResetFilters(t *testing.T) {
|
||||
for _, mode := range []string{"initial", "inactive", "terminal", "generation", "resize", "typing", "prompt", "password", "navigation", "large cursor", "ordinary output"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
var m Monitor
|
||||
now := time.Unix(100, 0)
|
||||
before, after := snapshot("Download 20%", "", "", ""), snapshot("Download 30%", "", "", "")
|
||||
m.Update(before, after, -1)
|
||||
m.Next(now)
|
||||
next := snapshot("Download 40%", "", "", "")
|
||||
editingRow := -1
|
||||
switch mode {
|
||||
case "initial":
|
||||
after = screen.Snapshot{}
|
||||
case "inactive":
|
||||
next.Active = false
|
||||
case "terminal":
|
||||
next.Terminal++
|
||||
case "generation":
|
||||
next.Generation++
|
||||
case "resize":
|
||||
next = snapshot("Download 40%", "")
|
||||
case "typing":
|
||||
editingRow = next.Y
|
||||
case "prompt":
|
||||
next = snapshot("Download 40%", "Username@host:~$", "", "")
|
||||
next.Y = 1
|
||||
case "password":
|
||||
next = snapshot("Download 40%", "Password:", "", "")
|
||||
next.Y = 1
|
||||
case "navigation":
|
||||
next = after
|
||||
next.X = 1
|
||||
case "large cursor":
|
||||
next.Y = 3
|
||||
case "ordinary output":
|
||||
next = snapshot("done", "", "", "")
|
||||
}
|
||||
m.Update(after, next, editingRow)
|
||||
if _, ok := m.Next(now.Add(time.Second)); ok || m.Active() {
|
||||
t.Fatal("retained progress")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultilineLongRowsAndActivity(t *testing.T) {
|
||||
now := time.Unix(100, 0)
|
||||
for _, testCase := range []struct {
|
||||
lines []string
|
||||
want float64
|
||||
ok bool
|
||||
}{
|
||||
{[]string{"package 20% [## ]", "Total 80% [######## ]", ""}, 80, true},
|
||||
{[]string{strings.Repeat("package", 35) + " 60% |###### |", "", ""}, 60, true},
|
||||
{[]string{"humidity 50%", "some other output", ""}, 0, false},
|
||||
{[]string{strings.Repeat("unrelated", 30) + " 50%", "", ""}, 0, false},
|
||||
{[]string{"50%", "some other output", ""}, 0, false},
|
||||
} {
|
||||
var m Monitor
|
||||
m.Update(snapshot("", "", ""), snapshot(testCase.lines...), -1)
|
||||
value, ok := m.Next(now)
|
||||
if ok != testCase.ok || ok && value != testCase.want {
|
||||
t.Fatal(testCase, value, ok)
|
||||
}
|
||||
}
|
||||
var m Monitor
|
||||
before, after := snapshot("", ""), snapshot("⠋ Working", "")
|
||||
m.Update(before, after, -1)
|
||||
if value, ok := m.Next(now); !ok || value != 50 {
|
||||
t.Fatal(value, ok)
|
||||
}
|
||||
m.Update(after, snapshot("⠙ Working", ""), -1)
|
||||
if _, ok := m.Next(now.Add(500 * time.Millisecond)); ok {
|
||||
t.Fatal("activity flood")
|
||||
}
|
||||
if _, ok := m.Next(now.Add(time.Second)); !ok {
|
||||
t.Fatal("missing activity")
|
||||
}
|
||||
if _, ok := m.Next(now.Add(2 * time.Second)); ok {
|
||||
t.Fatal("beeping stopped spinner")
|
||||
}
|
||||
}
|
||||
|
||||
func format_percent(value int) string { return fmt.Sprint(value) + "%" }
|
||||
@@ -0,0 +1,175 @@
|
||||
// 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>.
|
||||
|
||||
// Detection patterns adapted from Python Fenrir (Chrys, Storm Dragon, and contributors).
|
||||
//
|
||||
// Package progress recognizes terminal progress without performing I/O.
|
||||
package progress
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type Indicator struct {
|
||||
Percentage float64
|
||||
Interval time.Duration // zero for measured progress, otherwise activity
|
||||
}
|
||||
|
||||
var (
|
||||
percentPattern = regexp.MustCompile(`(?:^|[^\d.\-])(\d+(?:\.\d+)?)\s*%`)
|
||||
statsPattern = regexp.MustCompile(`(?i)\b(?:humidity|cpu|memory|disk|usage|temp|weather|forecast)\b`)
|
||||
fractionPattern = regexp.MustCompile(`\b(\d+)\s*(?:of|/)\s*(\d+)\b`)
|
||||
fractionNoise = regexp.MustCompile(`(?i)\b(?:page|chapter|section|line|row|column|year|month|day)\b|\d{1,2}/\d{1,2}/\d{2,4}`)
|
||||
barPattern = regexp.MustCompile(`\[[#=*>█▉▊▋▌▍▎▏▒▓░. \t-]{2,}\]|\|[#=*>█▉▊▋▌▍▎▏▒▓░. \t-]{2,}\|`)
|
||||
urlPattern = regexp.MustCompile(`(?i)\S+://\S+|www\.[^\s]+\.[a-z]{2,}`)
|
||||
ratePattern = regexp.MustCompile(`(?i)\b\d+(?:\.\d+)?\s*[kmgt](?:i?b)?/s\b`)
|
||||
fieldPattern = regexp.MustCompile(`(?i)\b([a-z][a-z0-9_-]*)=\s*\S+`)
|
||||
timeField = regexp.MustCompile(`(?i)\b(?:time|elapsed|eta)=\s*\d+(?::\d{2}){1,2}(?:\.\d+)?\b`)
|
||||
rateField = regexp.MustCompile(`(?i)\b(?:speed|fps|bitrate)=\s*[\d.]+[a-z/]*\b`)
|
||||
transferPattern = regexp.MustCompile(`(?i)(?:remaining|elapsed|left|eta)[:;\s]*\d+s|\d+s\s+(?:remaining|elapsed|left)|(?:processing|generating|used|consumed)\s+\d+\s+tokens|\d+\s+bytes.*copied.*\d+\s+s.*[kmgt]?b/s|\d+(?:\.\d+)?\s+[kmgt]ib\s+\d+(?:\.\d+)?\s+[kmgt]ib/s\s+\d+:\d+|\d+\s+\d+[kmgt]?\s+\d+\s+\d+[kmgt]?.*\d+\.\d+[kmgt].*\d+:\d+:\d+`)
|
||||
activityWords = `thinking|working|processing|analyzing|searching|reading|writing|planning|running|executing|updating|building|installing|compiling|downloading|reviewing|generating|responding|applying|fixing|editing|creating|preparing|checking|opening|loading|fetching|retrieving|scanning|indexing|summarizing`
|
||||
activityPattern = regexp.MustCompile(`(?i)\b(?:loading|processing|working|installing|downloading|compiling|building)\b.*\.{2,}`)
|
||||
interruptPattern = regexp.MustCompile(`(?i)(?:[*✢✽✶✻·•◦○●◆]\s+\S+|^\s*(?:` + activityWords + `)\b).*\([^)]*(?:esc|ctrl\+c) to interrupt[^)]*\)`)
|
||||
bulletPattern = regexp.MustCompile(`(?i)^\s*[•◦]\s+.*(?:…|\.{3,}|\b(?:` + activityWords + `)\b)`)
|
||||
thinkingPattern = regexp.MustCompile(`(?i)🔄[^\pL\d_]*(?:thinking|processing|working|analyzing)[^\d]*(\d+)s?\b`)
|
||||
taskPattern = regexp.MustCompile(`(?i)[●○◉]\s+(?:task\s+)?(?:output|running|pending|working|processing)\s+[a-z0-9]+`)
|
||||
promptPattern = regexp.MustCompile(`(?i)^\s*(?:[$#>]|\[.*\]\s*[\\$#>]|[a-z0-9._-]+[\\$#>]|\S+@\S+.*[$#>])\s*$|\?\s*\[(?:y/n|yes/no)\]\s*$|(?:\[sudo\]\s*password for .*|password)\s*:\s*$|^\s*(?:press any key to continue|are you sure\?).*$`)
|
||||
curlPattern = regexp.MustCompile(`^\s*(\d+)\s+` + sizeField + `\s+(\d+)\s+` + sizeField + `\s+(\d+)\s+` + sizeField + `\s+` + sizeField + `\s+` + sizeField + `\s+` + clockField + `\s+` + clockField + `\s+` + clockField + `\s+` + sizeField + `(?:\s|$)`)
|
||||
)
|
||||
|
||||
const sizeField = `\d+(?:\.\d+)?(?:[kKmMgGtT](?:i?[bB])?)?`
|
||||
const clockField = `(?:(?:\d+:)?\d{2}:\d{2}|--:--:--)`
|
||||
|
||||
func curl_percentage(text string) (float64, bool) {
|
||||
match := curlPattern.FindStringSubmatch(text)
|
||||
if match == nil {
|
||||
return 0, false
|
||||
}
|
||||
total, _ := strconv.ParseFloat(match[1], 64)
|
||||
received, _ := strconv.ParseFloat(match[2], 64)
|
||||
uploaded, _ := strconv.ParseFloat(match[3], 64)
|
||||
return total, total <= 100 && total == received && uploaded <= 100
|
||||
}
|
||||
|
||||
func status_fields(text string) bool {
|
||||
names := make(map[string]bool)
|
||||
for _, match := range fieldPattern.FindAllStringSubmatch(text, -1) {
|
||||
switch name := strings.ToLower(match[1]); name {
|
||||
case "frame", "fps", "size", "time", "bitrate", "speed", "elapsed", "dup", "drop":
|
||||
names[name] = true
|
||||
}
|
||||
}
|
||||
return len(names) >= 3 && timeField.MatchString(text) && rateField.MatchString(text)
|
||||
}
|
||||
|
||||
// Explicit rows are safe to inspect in large/multiline repaints. This follows
|
||||
// Python's structural filters instead of accepting arbitrary numeric tables.
|
||||
func explicit(text string) bool {
|
||||
if urlPattern.MatchString(text) {
|
||||
return false
|
||||
}
|
||||
if _, ok := curl_percentage(text); ok {
|
||||
return true
|
||||
}
|
||||
return status_fields(text) || percentPattern.MatchString(text) && (barPattern.MatchString(text) || ratePattern.MatchString(text))
|
||||
}
|
||||
|
||||
// Detect ports Python's screen-update detector. Prefer measured progress, then
|
||||
// activity. Indeterminate indicators use the same pitch as measured 50%.
|
||||
func Detect(text string) (Indicator, bool) {
|
||||
if strings.ContainsAny(text, "\r\n") || urlPattern.MatchString(text) {
|
||||
return Indicator{}, false
|
||||
}
|
||||
if match := percentPattern.FindStringSubmatch(text); match != nil {
|
||||
value, _ := strconv.ParseFloat(match[1], 64)
|
||||
if value <= 100 && !statsPattern.MatchString(text) {
|
||||
return Indicator{Percentage: value}, true
|
||||
}
|
||||
return Indicator{}, false
|
||||
}
|
||||
if value, ok := curl_percentage(text); ok {
|
||||
return Indicator{Percentage: value}, true
|
||||
}
|
||||
if transferPattern.MatchString(text) || status_fields(text) {
|
||||
return activity(2 * time.Second), true
|
||||
}
|
||||
if match := fractionPattern.FindStringSubmatch(text); match != nil && !fractionNoise.MatchString(text) {
|
||||
current, _ := strconv.ParseFloat(match[1], 64)
|
||||
total, _ := strconv.ParseFloat(match[2], 64)
|
||||
if total > 0 && total <= 1000 && current <= total {
|
||||
return Indicator{Percentage: 100 * current / total}, true
|
||||
}
|
||||
}
|
||||
if bar := barPattern.FindString(text); bar != "" {
|
||||
content := []rune(bar)[1 : utf8.RuneCountInString(bar)-1]
|
||||
filled, empty := 0.0, false
|
||||
valid := true
|
||||
for _, char := range content {
|
||||
weight := 1.0
|
||||
switch char {
|
||||
case ' ', '\t', '.', '-', '░':
|
||||
weight = 0
|
||||
empty = true
|
||||
case '▏':
|
||||
weight = .125
|
||||
case '▎':
|
||||
weight = .25
|
||||
case '▍':
|
||||
weight = .375
|
||||
case '▌':
|
||||
weight = .5
|
||||
case '▋':
|
||||
weight = .625
|
||||
case '▊':
|
||||
weight = .75
|
||||
case '▉':
|
||||
weight = .875
|
||||
}
|
||||
if weight > 0 && empty {
|
||||
valid = false
|
||||
}
|
||||
filled += weight
|
||||
}
|
||||
if valid && filled > 0 {
|
||||
return Indicator{Percentage: 100 * filled / float64(len(content))}, true
|
||||
}
|
||||
}
|
||||
if activityPattern.MatchString(text) {
|
||||
return activity(2 * time.Second), true
|
||||
}
|
||||
if strings.ContainsAny(text, "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏⡿⣟⣯⣷⣾⣽⣻⢿🌑🌒🌓🌔🌕🌖🌗🌘◐◓◒◑") || interruptPattern.MatchString(text) || bulletPattern.MatchString(text) {
|
||||
return activity(time.Second), true
|
||||
}
|
||||
if match := thinkingPattern.FindStringSubmatch(text); match != nil {
|
||||
seconds, _ := strconv.Atoi(match[1])
|
||||
interval := 1500 * time.Millisecond
|
||||
if seconds >= 10 {
|
||||
interval = 2 * time.Second
|
||||
}
|
||||
return activity(interval), true
|
||||
}
|
||||
if taskPattern.MatchString(text) {
|
||||
return activity(1500 * time.Millisecond), true
|
||||
}
|
||||
return Indicator{}, false
|
||||
}
|
||||
|
||||
func activity(interval time.Duration) Indicator { return Indicator{Percentage: 50, Interval: interval} }
|
||||
@@ -0,0 +1,79 @@
|
||||
// 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 progress
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPythonProgressFormats(t *testing.T) {
|
||||
for _, testCase := range []struct {
|
||||
text string
|
||||
percentage float64
|
||||
interval time.Duration
|
||||
}{
|
||||
{"Downloading 0%", 0, 0}, {"Downloading 25.5%", 25.5, 0}, {"100%", 100, 0},
|
||||
{"15/100", 15, 0}, {"3 of 10", 30, 0}, {"[#### ]", 50, 0}, {"[====> ]", 50, 0}, {"|█████ |", 50, 0}, {"[▌ ]", 12.5, 0},
|
||||
{" 50 100M 50 50M 0 0 10M 0 0:00:10 0:00:05 0:00:05 10M", 50, 0},
|
||||
{"frame= 10 fps=25 size=100kB time=00:00:03.50 bitrate=200kbits/s speed=1.0x", 50, 2 * time.Second},
|
||||
{"remaining: 12s", 50, 2 * time.Second}, {"generating 20 tokens", 50, 2 * time.Second},
|
||||
{"1024 bytes copied, 1 s, 1 kB/s", 50, 2 * time.Second}, {"2.5 MiB 1.2 MiB/s 00:02", 50, 2 * time.Second},
|
||||
{"Loading...", 50, 2 * time.Second}, {"⠋ preparing", 50, time.Second}, {"🌒 working", 50, time.Second}, {"◑", 50, time.Second},
|
||||
{"✻ Generating answer… (esc to interrupt)", 50, time.Second}, {"Working (ctrl+c to interrupt)", 50, time.Second},
|
||||
{"• Checking files…", 50, time.Second}, {"◦ Working on files", 50, time.Second},
|
||||
{"🔄 Thinking 3s", 50, 1500 * time.Millisecond}, {"🔄 Thinking 23s", 50, 2 * time.Second},
|
||||
{"● Task Running job", 50, 1500 * time.Millisecond},
|
||||
} {
|
||||
t.Run(testCase.text, func(t *testing.T) {
|
||||
got, ok := Detect(testCase.text)
|
||||
if !ok || math.Abs(got.Percentage-testCase.percentage) > 0.001 || got.Interval != testCase.interval {
|
||||
t.Fatalf("got %+v, %v", got, ok)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFalsePositives(t *testing.T) {
|
||||
for _, text := range []string{
|
||||
"https://example.invalid/50%", "www.example.invalid/3/10", "humidity 50%", "CPU usage 70%", "weather 30%",
|
||||
"-1%", "1000%", "1.2.50%", "Page 3 of 10", "chapter 1/5", "10/25/2026", "5/0", "20/10", "1/1001",
|
||||
"[#channel]", "[item]", "[*]", "[--]", "10 20 30 40 50 60", "Speed", "normal text", "frame=10 fps=25 size=20",
|
||||
"150 100M 150 150M 0 0 10M 0 0:00:10 0:00:05 0:00:05 10M",
|
||||
"50 100M 30 30M 0 0 10M 0 0:00:10 0:00:05 0:00:05 10M",
|
||||
} {
|
||||
if got, ok := Detect(text); ok {
|
||||
t.Errorf("false positive %q: %+v", text, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExplicitRows(t *testing.T) {
|
||||
for _, text := range []string{"package 50% [#### ]", "package 50% |#### |", "package 50% 2 MiB/s", "frame=10 time=00:01:20 fps=25"} {
|
||||
if !explicit(strings.Repeat("long-name", 30) + " " + text) {
|
||||
t.Error(text)
|
||||
}
|
||||
}
|
||||
for _, text := range []string{"50%", "Speed", "https://example.invalid 50% [#### ]", "normal table 1 2 3"} {
|
||||
if explicit(text) {
|
||||
t.Error(text)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user