Zoxide Database to PowerShell object. #748
josephwhite
started this conversation in
Show and tell
Replies: 1 comment
-
In case you instead wanted to format the object to something Zoxide can import, use this: <#
.Synopsis
Format Zoxide Database.
.Description
Cases for each format to support the various "smart change directory" programs.
Many of them store their databases as text files.
.Parameter ZoxideDatabase
Zoxide Database, represented as a PSCustomObject.
Validates the objects having the same properties as the output object from Get-ZoxideDatabase.
See Get-ZoxideDatabase for description of the properties needed.
.Parameter Format
Style to format the ZoxideDatabase entries as the following.
| Value | Description | Applicability
|:---------|:---------------------------------|:----------------------------------------------
|"Autojump"| Autojump Database file. | https://github.com/wting/autojump
| | |
|"Z" | Z Database file. | https://github.com/rupa/z
| | | https://github.com/skywind3000/z.lua
| | | https://github.com/agkozak/zsh-z
| | | https://github.com/jethrokuan/z
| | | https://github.com/clvv/fasd
| | | https://github.com/quackduck/WarpDrive
| | | https://github.com/knl/rh
| | |
|"Jump" | Jump Scores Database (JSON) file.| https://github.com/gsamokovarov/jump
.Example
# Export the Zoxide Database in Autojump format.
$zdata = Get-ZoxideDatabase
$zdata_content = Format-ZoxideDatabase -ZoxideDatabase $zdata -Format "Autojump"
$zdata_content | Out-File -FilePath "C:\foo\zoxide_database_autojump.txt"
.Example
# Export the Zoxide Database in Z format to a file.
$zdata = Get-ZoxideDatabase
$zdata_content = Format-ZoxideDatabase -ZoxideDatabase $zdata -Format "Z"
$zdata_content | Out-File -FilePath "C:\foo\zoxide_database_z.txt"
.Example
# Export the Zoxide Database in Jump format to a file.
$zdata = Get-ZoxideDatabase
$zdata_content = Format-ZoxideDatabase -ZoxideDatabase $zdata -Format "Jump"
$zdata_content | Out-File -FilePath "C:\foo\zoxide_database_jump_scores.json"
.Outputs
String[]
Zoxide Database in given format.
.Notes
Zoxide's Import command only supports Autojump and Z files.
Convert Jump to Z file.
https://github.com/ajeetdsouza/zoxide/issues/706
To see valid values for Format parameter.
((Get-Command Format-ZoxideDatabase).Parameters['Format'].Attributes | Where-Object { $_ -is [ValidateSet] }).ValidValues
#>
function Format-ZoxideDatabase {
[OutputType([String[]])]
param(
[Parameter()]
[ValidateScript({
$Members = @(
'Path'
'Score'
)
foreach ($member in $members) {
if (!(Get-Member -inputobject $_ -name $member -Membertype Properties)) {
throw "$_ is invalid. Missing $member property."
}
}
$true
})]
[Alias("InputObject", "Input")]
[PSCustomObject[]]$ZoxideDatabase,
[Parameter()]
[ValidateSet(
"Autojump",
"Z",
"Jump"
)]
[string]$Format = "Z"
)
switch($Format) {
"Autojump" {
$line_buffer = [string]"{0}|{1}|0";
$content = ForEach ($entry in $ZoxideDatabase) {
$line = $line_buffer -f $entry.Path, $entry.Score
Write-Output $line
}
}
"Z" {
$line_buffer = [string]"{1}`t{0}";
$content = ForEach ($entry in $ZoxideDatabase) {
$line = $line_buffer -f $entry.Path, $entry.Score
Write-Output $line
}
}
"Jump" {
$jump_json_db_obj = ForEach ($entry in $ZoxideDatabase) {
[PSCustomObject]@{
Path = $entry.Path
Score = @{
Weight = $entry.Score
Age = (Get-Item $entry.Path).LastAccessTime
}
}
}
$content = $jump_json_db_obj | ConvertTo-Json;
}
}
return $content;
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi.
Below is a function I wrote to parse a full database dump.
The main goal I had was having a CSV mirror of the database and being able to update it in demand.
Beta Was this translation helpful? Give feedback.
All reactions