fully working transcription

This commit is contained in:
LeonardoTrapani
2025-08-18 19:43:14 +02:00
parent f03c9c6a87
commit bcaf1181a0
5 changed files with 294 additions and 38 deletions
+53
View File
@@ -0,0 +1,53 @@
package injection
import (
"context"
"fmt"
"os/exec"
"strings"
"time"
)
// getClipboard retrieves the current clipboard content using wl-paste
func getClipboard(ctx context.Context, timeout time.Duration) (string, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
cmd := exec.CommandContext(ctx, "wl-paste", "--no-newline")
output, err := cmd.Output()
if err != nil {
// wl-paste returns non-zero exit code if clipboard is empty or unavailable
// This is normal behavior, so we return empty string instead of error
return "", nil
}
return string(output), nil
}
// setClipboard sets the clipboard content using wl-copy
func setClipboard(ctx context.Context, text string, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
cmd := exec.CommandContext(ctx, "wl-copy")
cmd.Stdin = strings.NewReader(text)
if err := cmd.Run(); err != nil {
return fmt.Errorf("wl-copy failed: %w", err)
}
return nil
}
// checkClipboardAvailable checks if wl-clipboard tools are available
func checkClipboardAvailable() error {
if _, err := exec.LookPath("wl-copy"); err != nil {
return fmt.Errorf("wl-copy not found: %w (install wl-clipboard)", err)
}
if _, err := exec.LookPath("wl-paste"); err != nil {
return fmt.Errorf("wl-paste not found: %w (install wl-clipboard)", err)
}
return nil
}
+112
View File
@@ -0,0 +1,112 @@
package injection
import (
"context"
"fmt"
"time"
)
// Injector interface for text injection
type Injector interface {
Inject(ctx context.Context, text string) error
}
// Config for text injection
type Config struct {
Mode string // "clipboard", "type", "fallback"
AlwaysCopyClipboard bool // Always copy to clipboard regardless of mode
RestoreClipboard bool // Restore original clipboard after injection
WtypeTimeout time.Duration // Timeout for wtype commands
ClipboardTimeout time.Duration // Timeout for clipboard operations
}
// DefaultConfig returns sensible defaults for injection
func DefaultConfig() Config {
return Config{
Mode: "fallback",
AlwaysCopyClipboard: true,
RestoreClipboard: true,
WtypeTimeout: 5 * time.Second,
ClipboardTimeout: 3 * time.Second,
}
}
// injector implements the Injector interface
type injector struct {
config Config
}
// NewInjector creates a new injector with the given config
func NewInjector(config Config) Injector {
return &injector{
config: config,
}
}
// NewDefaultInjector creates an injector with default configuration
func NewDefaultInjector() Injector {
return NewInjector(DefaultConfig())
}
// Inject performs text injection based on the configured mode
func (i *injector) Inject(ctx context.Context, text string) error {
if text == "" {
return fmt.Errorf("cannot inject empty text")
}
// Always copy to clipboard if configured
var originalClipboard string
var err error
if i.config.AlwaysCopyClipboard || i.config.Mode == "clipboard" || i.config.Mode == "fallback" {
if err := checkClipboardAvailable(); err != nil {
return fmt.Errorf("clipboard tools not available: %w", err)
}
if i.config.RestoreClipboard {
originalClipboard, _ = getClipboard(ctx, i.config.ClipboardTimeout)
}
if err := setClipboard(ctx, text, i.config.ClipboardTimeout); err != nil {
return fmt.Errorf("failed to copy text to clipboard: %w", err)
}
}
// Handle different injection modes
switch i.config.Mode {
case "clipboard":
// Already handled above
return nil
case "type":
err = typeText(ctx, text, i.config.WtypeTimeout)
if err != nil {
return fmt.Errorf("failed to type text: %w", err)
}
case "fallback":
// Try typing first, fallback to clipboard
err = typeText(ctx, text, i.config.WtypeTimeout)
if err != nil {
// Typing failed, but clipboard is already set from above
// Just log the typing error but don't fail the injection
return nil
}
default:
return fmt.Errorf("unsupported injection mode: %s", i.config.Mode)
}
// Restore original clipboard if configured and we have it
if i.config.RestoreClipboard && originalClipboard != "" {
// Restore after a short delay to ensure the text has been processed
go func() {
time.Sleep(100 * time.Millisecond)
restoreCtx, cancel := context.WithTimeout(context.Background(), i.config.ClipboardTimeout)
defer cancel()
setClipboard(restoreCtx, originalClipboard, i.config.ClipboardTimeout)
}()
}
return nil
}
+36
View File
@@ -0,0 +1,36 @@
package injection
import (
"context"
"fmt"
"os/exec"
"time"
)
// typeText types the given text using wtype
func typeText(ctx context.Context, text string, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
// Check if wtype is available
if err := checkWtypeAvailable(); err != nil {
return err
}
cmd := exec.CommandContext(ctx, "wtype", text)
if err := cmd.Run(); err != nil {
return fmt.Errorf("wtype failed: %w", err)
}
return nil
}
// checkWtypeAvailable checks if wtype is available on the system
func checkWtypeAvailable() error {
if _, err := exec.LookPath("wtype"); err != nil {
return fmt.Errorf("wtype not found: %w (install wtype package)", err)
}
return nil
}