-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswipesort.ps1
246 lines (214 loc) · 7.55 KB
/
swipesort.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<#
.NOTES
File Name : swipesort.ps1
Author : MollyInanna
Prerequisite : PowerShell V3 or higher
Copyright : (c) 2024 MollyInanna
License : AGPL-3.0
Version : 1.0
Creation Date : 2024
Last Modified : 2024-07-27
.SYNOPSIS
A Tinder-like PowerShell script for sorting text file contents via keyboard input.
.DESCRIPTION
This script allows users to process text files line by line, sorting each line into 'left' or 'right' categories
using arrow key inputs. It supports a configurable UI mode and remembers progress between sessions.
.PARAMETER inputFileName
The name of the input file to process. This should be a text file.
.PARAMETER ui
Used to set the UI mode. Use '-ui on' for verbose mode or '-ui off' for minimal mode.
.EXAMPLE
Set UI mode to verbose:
.\swipesort.ps1 -ui on
.EXAMPLE
Set UI mode to minimal:
.\swipesort.ps1 -ui off
.EXAMPLE
Process a file:
.\swipesort.ps1 input.txt
.LINK
https://github.com/mollyrealized/swipesort
#>
param(
[Parameter(Mandatory=$false, Position=0)]
[string]$inputFileName,
[Parameter(Mandatory=$false)]
[string]$ui
)
# Function to handle errors consistently
function Write-ErrorAndExit {
param([string]$ErrorMessage)
Write-Host "Error: $ErrorMessage" -ForegroundColor Red
exit 1
}
$uiConfigFilePath = Join-Path ([System.Environment]::GetFolderPath('ApplicationData')) "swipesort-ui.cfg"
# Handle UI configuration
function Set-UiConfig {
param([string]$Value)
Set-Content -Path $uiConfigFilePath -Value $Value -Force
}
function Get-UiConfig {
if (Test-Path $uiConfigFilePath) {
return Get-Content $uiConfigFilePath
}
return "1" # Default to verbose if file doesn't exist
}
# Handle UI setting command
if ($ui -eq "on") {
Set-UiConfig "1"
exit 0
} elseif ($ui -eq "off") {
Set-UiConfig "0"
exit 0
}
# If no input file is provided and no UI command, show usage
if (-not $inputFileName) {
Write-Host "Usage:"
Write-Host " Set UI mode: .\swipesort.ps1 -ui on|off"
Write-Host " Run script: .\swipesort.ps1 <input_file>"
exit 0
}
$uiMode = Get-UiConfig
# Check if the input file exists and is a text file
if (-not (Test-Path $inputFileName -PathType Leaf)) {
Write-ErrorAndExit "Input file not found or is not a file."
}
try {
$fileContent = Get-Content $inputFileName -Raw -ErrorAction Stop
if ($fileContent -match '[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]') {
Write-ErrorAndExit "File contains non-text characters."
}
if ([string]::IsNullOrWhiteSpace($fileContent)) {
Write-ErrorAndExit "Input file is empty."
}
} catch {
Write-ErrorAndExit "The specified file is not a valid text file or cannot be read."
}
# Derive left and right output files from input file name
$leftFileName = $inputFileName -replace '\.(\w+)$', '-left.$1'
$rightFileName = $inputFileName -replace '\.(\w+)$', '-right.$1'
$configFilePath = Join-Path ([System.Environment]::GetFolderPath('ApplicationData')) "swipesort.cfg"
# Function definitions
function Read-LastIndexConfig {
param(
[string]$ConfigFilePath,
[string]$InputFileName
)
$config = @{}
if (Test-Path $ConfigFilePath) {
$configContent = Get-Content $ConfigFilePath -ErrorAction Stop
foreach ($line in $configContent) {
$pair = $line -split '=',2
if($pair.Count -eq 2) {
$config[$pair[0]] = [int]$pair[1]
}
}
}
if ($config.ContainsKey($InputFileName)) {
return $config[$InputFileName]
} else {
return 0
}
}
function Write-LastIndexConfig {
param(
[string]$ConfigFilePath,
[string]$InputFileName,
[int]$LastIndex
)
$config = @{}
if (Test-Path $ConfigFilePath) {
$configContent = Get-Content $ConfigFilePath -ErrorAction Stop
foreach ($line in $configContent) {
$pair = $line -split '=',2
if($pair.Count -eq 2) {
$config[$pair[0]] = $pair[1]
}
}
}
$config[$InputFileName] = $LastIndex
$config.GetEnumerator() | ForEach-Object {
"$($_.Key)=$($_.Value)"
} | Out-File $ConfigFilePath -Force -ErrorAction SilentlyContinue
}
function Append-ToFile {
param(
[string]$Path,
[string]$Content
)
try {
Add-Content $Path $Content -ErrorAction Stop
} catch {
Write-ErrorAndExit "Unable to write to file $Path. Please check permissions and try again."
}
}
# Main script logic
try {
$currentIndex = Read-LastIndexConfig -ConfigFilePath $configFilePath -InputFileName $inputFileName
$lines = Get-Content $inputFileName
do {
Clear-Host
if ($currentIndex -ge $lines.Length) { break }
$currentLine = $lines[$currentIndex]
Write-Host $currentLine
if ($uiMode -eq "1") {
Write-Host "Swipe Left <- | Swipe Right -> | Undo ^ | Quit Q"
}
$key = $null
while ($null -eq $key) {
$input = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
switch ($input.VirtualKeyCode) {
37 { # Left arrow key code
Append-ToFile -Path $leftFileName -Content $currentLine
$currentIndex++
$key = 'left'
}
39 { # Right arrow key code
Append-ToFile -Path $rightFileName -Content $currentLine
$currentIndex++
$key = 'right'
}
38 { # Up arrow key code
if ($currentIndex -gt 0) {
$currentIndex--
# Remove the last line from the respective file
$lastChoice = if(Test-Path $rightFileName) { (Get-Content $rightFileName)[-1] } else { $null }
if ($lastChoice -eq $currentLine) {
$allLines = Get-Content $rightFileName
$allLines = $allLines[0..($allLines.Count - 2)]
$allLines | Set-Content $rightFileName -Force
} else {
$allLines = Get-Content $leftFileName
$allLines = $allLines[0..($allLines.Count - 2)]
$allLines | Set-Content $leftFileName -Force
}
}
$key = 'up'
}
81 { # Q key code
Write-LastIndexConfig -ConfigFilePath $configFilePath -InputFileName $inputFileName -LastIndex $currentIndex
if ($uiMode -eq "1") {
Write-Host "Progress saved. Exiting..."
}
return
}
default {
if ($uiMode -eq "1") {
Write-Host "Invalid key. Please use arrow keys or 'Q' to quit." -ForegroundColor Yellow
}
$key = $null
}
}
}
Write-LastIndexConfig -ConfigFilePath $configFilePath -InputFileName $inputFileName -LastIndex $currentIndex
} while ($currentIndex -lt $lines.Length)
if ($uiMode -eq "1") {
Write-Host "All lines processed. Files for left and right swipes are up to date."
}
} catch {
Write-ErrorAndExit "An unexpected error occurred: $_"
} finally {
if ($currentIndex -lt $lines.Length) {
Write-LastIndexConfig -ConfigFilePath $configFilePath -InputFileName $inputFileName -LastIndex $currentIndex
}
}