Files

136 lines
4.0 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 scripts preserves Python Fenrir's external script-key contract.
// Scripts are executables, not in-process extensions.
package scripts
import (
"fmt"
"os"
"path/filepath"
"strings"
"git.stormux.org/storm/fenrir/internal/command"
"git.stormux.org/storm/fenrir/internal/input"
)
const SystemPath = "/usr/share/fenrirscreenreader/scripts"
type Script struct {
ID command.ID
Name, Path string
Keys []string
}
// Paths follows CommandManager.get_script_paths, including the resource root
// itself (there is no additional user "scripts" subdirectory).
func Paths(home, configured, bundled string) []string {
var paths []string
seen := map[string]bool{}
for _, path := range []string{filepath.Join(home, ".local/stormux/fenrir"), configured, bundled} {
if path == "" {
continue
}
if path == "~" {
path = home
} else if strings.HasPrefix(path, "~/") {
path = filepath.Join(home, path[2:])
}
path, err := filepath.Abs(path)
if err == nil && !seen[path] {
paths = append(paths, path)
seen[path] = true
}
}
return paths
}
func Parse_filename(filename string) (Script, bool) {
if strings.HasPrefix(filename, ".") {
return Script{}, false
}
stem := strings.TrimSuffix(filename, filepath.Ext(filename))
if strings.HasPrefix(stem, "__") {
return Script{}, false
}
parts := strings.Split(strings.ToUpper(stem), "__-__")
if len(parts) > 2 {
return Script{}, false
}
keys := strings.Split(parts[len(parts)-1], "__+__")
hasScript := false
for _, key := range keys {
if !input.Valid_script_key(key) {
return Script{}, false
}
hasScript = hasScript || key == "KEY_SCRIPT"
}
if !hasScript {
keys = append(keys, "KEY_SCRIPT")
}
return Script{ID: command.ID("script:" + strings.ToUpper(stem)), Name: parts[0], Keys: keys}, true
}
// Discover does not execute files or require their executable bit. Python
// checks executability on invocation. Directory errors affect that directory
// only. Same-directory variants may share a name; earlier directories shadow
// all later variants of that name. ReadDir provides Python's sorted order.
func Discover(paths []string, occupied map[string]bool) ([]Script, []error) {
loadedNames := map[string]bool{}
loadedIDs := map[command.ID]bool{}
shortcuts := map[string]bool{}
for chord, used := range occupied {
shortcuts[chord] = used
}
var result []Script
var problems []error
for _, path := range paths {
entries, err := os.ReadDir(path)
if os.IsNotExist(err) {
continue
}
if err != nil {
problems = append(problems, fmt.Errorf("scripts %s: %w", path, err))
continue
}
names := map[string]bool{}
for _, entry := range entries {
fullPath := filepath.Join(path, entry.Name())
info, err := os.Stat(fullPath) // Follow symlinks, as os.path.isfile does.
if err != nil || !info.Mode().IsRegular() {
continue
}
script, ok := Parse_filename(entry.Name())
if !ok || loadedNames[script.Name] || loadedIDs[script.ID] {
continue
}
chord := input.Script_chord(script.Keys)
if shortcuts[chord] {
continue
}
script.Path = fullPath
result = append(result, script)
names[script.Name], loadedIDs[script.ID], shortcuts[chord] = true, true, true
}
for name := range names {
loadedNames[name] = true
}
}
return result, problems
}