calfind.psm1 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Nathan McRae
  2. # me@nathanmcrae.name
  3. # 2021-03-25
  4. function calfind {
  5. [CmdletBinding()]
  6. Param(
  7. $Input,
  8. [switch]$ChooseDate,
  9. [switch]$DontMerge,
  10. # ParameterSet 1
  11. [int]$DaysAgo = 5
  12. # $Since: all days since
  13. # $LastWeek: All days last week
  14. # $Today (alias -t)
  15. # $Date
  16. )
  17. $global:calfind = @()
  18. $startDate = ((Get-Date).AddDays(-$DaysAgo + 1)).ToString("yyyy-?MM-?dd")
  19. $endDate = (Get-Date).ToString("yyyy-?MM-?dd")
  20. # Regex is faster matching exact digits (rather than \d), so use a pattern with the common
  21. # prefix of the date range we're searching.
  22. $dateRgxList = @()
  23. foreach($i in 0..($startDate.Length - 1)) {
  24. if($startDate[$i] -eq $endDate[$i]) {
  25. $dateRgxList += $startDate[$i]
  26. } else {
  27. $dateRgxList += "\d"
  28. }
  29. }
  30. $dateRgx = $dateRgxList -join ""
  31. $dateFiles = Search-Everything -Global -RegularExpression $dateRgx
  32. # $dateFiles = fd . $Input | grep $dateRgx
  33. $AppData = (Join-Path (Resolve-Path ~) AppData) -replace "\\","\\"
  34. $ignoreRgx = "^C:\\Windows|^$AppData|^C:\\ProgramData|^C:\\Program Files"
  35. $dateLists = New-Object Hashtable
  36. $dateListsSync = [System.Collections.Hashtable]::Synchronized($dateLists)
  37. ($DaysAgo - 1)..0 | ForEach-Object -Parallel {
  38. $dayAgo = $_
  39. $date = (Get-Date).AddDays(-$dayAgo)
  40. $dateString = $date.ToString("yyyy-MM-dd")
  41. $thisDateRgx = $date.ToString("yyyy-?MM-?dd")
  42. $matchingFiles = ($using:dateFiles | ?{($_ -match $thisDateRgx) -and (-not ($_ -match $using:ignoreRgx))})
  43. ($using:dateListsSync).Add($dateString, $matchingFiles)
  44. }
  45. $rgx = "(\d{4}.?\d\d.?\d\d[^\\/\d])\d\d(.?)\d\d(.?)\d\d"
  46. $options = $dateLists.keys | Sort-Object | %{$dateLists[$_] -replace $rgx,'$1..$2..$3..' | sort | unique ;$_}
  47. $choices = $options | fzf --tac --multi --bind ctrl-a:toggle-all,ctrl-d:page-down,ctrl-u:page-up,ctrl-t:toggle
  48. foreach($choice in $choices){
  49. # If the date itself is selected, return all matches for the date
  50. if($choice -Match "^(\d{4})-(\d\d)-(\d\d)$") {
  51. $global:calfind += $dateLists[$choice]
  52. $dateLists[$choice]
  53. }
  54. # If multiple matches were collected into one entry, return all matches
  55. if($choice -Match "\.\..?\.\..?\.\.") {
  56. $dateMatch = ([regex]"(\d{4})-(\d\d)-(\d\d)").Match($choice)
  57. $dateString = "$($dateMatch.Groups[1].Value)-$($dateMatch.Groups[2].Value)-$($dateMatch.Groups[3].Value)"
  58. $choiceRgx = $choice -replace "\\","\\"
  59. Write-Host "This is my date string: $dateString"
  60. Write-Host "choice: $choiceRgx"
  61. $choiceMatches = $dateLists[$dateString] | ?{ $_ -match $choiceRgx}
  62. $global:calfind += $choiceMatches
  63. $choiceMatches
  64. } else {
  65. $global:calfind += $choice
  66. $choice
  67. }
  68. }
  69. }
  70. function IgnoreStuff {
  71. Param(
  72. [Parameter(ValueFromPipeline=$True)]
  73. $Input
  74. )
  75. Begin {
  76. $AppData = (Join-Path (Resolve-Path ~) AppData)
  77. }
  78. Process {
  79. $_ | ?{(-not $_.StartsWith("C:\Windows") -and (-not $_.StartsWith($AppData))) -and (-not $_.StartsWith("C:\ProgramData")) -and (-not $_.StartsWith("C:\Program Files"))}
  80. }
  81. }
  82. function Search-Date {
  83. Param(
  84. [DateTime]$Date
  85. )
  86. $results = Search-Everything -Global -RegularExpression $Date.ToString("yyyy-?MM-?dd")
  87. }