123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- # Nathan McRae
- # me@nathanmcrae.name
- # 2021-03-25
- function calfind {
- [CmdletBinding()]
- Param(
- $Input,
- [switch]$ChooseDate,
- [switch]$DontMerge,
- # ParameterSet 1
- [int]$DaysAgo = 5
- # $Since: all days since
- # $LastWeek: All days last week
- # $Today (alias -t)
- # $Date
- )
- $global:calfind = @()
- $startDate = ((Get-Date).AddDays(-$DaysAgo + 1)).ToString("yyyy-?MM-?dd")
- $endDate = (Get-Date).ToString("yyyy-?MM-?dd")
- # Regex is faster matching exact digits (rather than \d), so use a pattern with the common
- # prefix of the date range we're searching.
- $dateRgxList = @()
- foreach($i in 0..($startDate.Length - 1)) {
- if($startDate[$i] -eq $endDate[$i]) {
- $dateRgxList += $startDate[$i]
- } else {
- $dateRgxList += "\d"
- }
- }
- $dateRgx = $dateRgxList -join ""
- $dateFiles = Search-Everything -Global -RegularExpression $dateRgx
- # $dateFiles = fd . $Input | grep $dateRgx
- $AppData = (Join-Path (Resolve-Path ~) AppData) -replace "\\","\\"
- $ignoreRgx = "^C:\\Windows|^$AppData|^C:\\ProgramData|^C:\\Program Files"
- $dateLists = New-Object Hashtable
- $dateListsSync = [System.Collections.Hashtable]::Synchronized($dateLists)
- ($DaysAgo - 1)..0 | ForEach-Object -Parallel {
- $dayAgo = $_
- $date = (Get-Date).AddDays(-$dayAgo)
- $dateString = $date.ToString("yyyy-MM-dd")
- $thisDateRgx = $date.ToString("yyyy-?MM-?dd")
- $matchingFiles = ($using:dateFiles | ?{($_ -match $thisDateRgx) -and (-not ($_ -match $using:ignoreRgx))})
- ($using:dateListsSync).Add($dateString, $matchingFiles)
- }
- $rgx = "(\d{4}.?\d\d.?\d\d[^\\/\d])\d\d(.?)\d\d(.?)\d\d"
- $options = $dateLists.keys | Sort-Object | %{$dateLists[$_] -replace $rgx,'$1..$2..$3..' | sort | unique ;$_}
- $choices = $options | fzf --tac --multi --bind ctrl-a:toggle-all,ctrl-d:page-down,ctrl-u:page-up,ctrl-t:toggle
- foreach($choice in $choices){
- # If the date itself is selected, return all matches for the date
- if($choice -Match "^(\d{4})-(\d\d)-(\d\d)$") {
- $global:calfind += $dateLists[$choice]
- $dateLists[$choice]
- }
- # If multiple matches were collected into one entry, return all matches
- if($choice -Match "\.\..?\.\..?\.\.") {
- $dateMatch = ([regex]"(\d{4})-(\d\d)-(\d\d)").Match($choice)
- $dateString = "$($dateMatch.Groups[1].Value)-$($dateMatch.Groups[2].Value)-$($dateMatch.Groups[3].Value)"
- $choiceRgx = $choice -replace "\\","\\"
- Write-Host "This is my date string: $dateString"
- Write-Host "choice: $choiceRgx"
- $choiceMatches = $dateLists[$dateString] | ?{ $_ -match $choiceRgx}
- $global:calfind += $choiceMatches
- $choiceMatches
- } else {
- $global:calfind += $choice
- $choice
- }
- }
- }
- function IgnoreStuff {
- Param(
- [Parameter(ValueFromPipeline=$True)]
- $Input
- )
- Begin {
- $AppData = (Join-Path (Resolve-Path ~) AppData)
- }
- Process {
- $_ | ?{(-not $_.StartsWith("C:\Windows") -and (-not $_.StartsWith($AppData))) -and (-not $_.StartsWith("C:\ProgramData")) -and (-not $_.StartsWith("C:\Program Files"))}
- }
- }
- function Search-Date {
- Param(
- [DateTime]$Date
- )
- $results = Search-Everything -Global -RegularExpression $Date.ToString("yyyy-?MM-?dd")
- }
|