1//go:build windows
2
3package tui
4
5import (
6 "context"
7 "fmt"
8)
9
10// ReadClipboardText reads plain text through the PowerShell installation
11// included with supported Windows versions.
12func ReadClipboardText() (string, bool, error) {
13 text, ok, err := readClipboardTextCommands(
14 clipboardTextCommand{name: "powershell.exe", args: []string{"-NoProfile", "-NonInteractive", "-Command", "[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-Clipboard -Raw -Format Text"}},
15 clipboardTextCommand{name: "pwsh.exe", args: []string{"-NoProfile", "-NonInteractive", "-Command", "Get-Clipboard -Raw"}},
16 )
17 if err == errClipboardCommandUnavailable {
18 return "", false, fmt.Errorf("text clipboard unavailable: PowerShell was not found")
19 }
20 if err != nil {
21 return "", false, fmt.Errorf("read text clipboard: %w", err)
22 }
23 return text, ok, nil
24}
25
26// WriteClipboardText writes plain text through the PowerShell installation
27// included with supported Windows versions.
28func WriteClipboardText(ctx context.Context, text string) error {
29 if err := writeClipboardTextCommands(ctx, text,
30 clipboardTextCommand{name: "powershell.exe", args: []string{"-NoProfile", "-NonInteractive", "-Command", "[Console]::InputEncoding = [System.Text.Encoding]::UTF8; Set-Clipboard -Value ([Console]::In.ReadToEnd())"}},
31 clipboardTextCommand{name: "pwsh.exe", args: []string{"-NoProfile", "-NonInteractive", "-Command", "[Console]::InputEncoding = [System.Text.Encoding]::UTF8; Set-Clipboard -Value ([Console]::In.ReadToEnd())"}},
32 ); err != nil {
33 if err == errClipboardCommandUnavailable {
34 return fmt.Errorf("text clipboard unavailable: PowerShell was not found")
35 }
36 return fmt.Errorf("write text clipboard: %w", err)
37 }
38 return nil
39}