# Installed packages as a table, optionally filtered by name
def pkgl [query?: string] {
  ^paru -Q --color never
  | lines
  | each {|l| let p = $l | split row ' '; {name: $p.0, version: $p.1}}
  | if $query != null { where name =~ $query } else { $in }
}

# Packages with available upgrades (name, current, new)
def pkgu [] {
  ^paru -Qu --color never
  | lines
  | each {|l|
      let p = $l | split row ' '
      {name: $p.0, current: $p.1, new: $p.3}
  }
}

# Search repos and AUR (repo, name, version, desc)
def pkgs [query: string] {
  ^paru -Ss --color never $query
  | lines
  | chunks 2
  | each {|pair|
      let header = $pair.0
      let desc = ($pair.1? | default "" | str trim)
      let parts = $header | split row '/'
      let name_ver = ($parts.1 | split row ' ')
      {
        repo:    $parts.0
        name:    $name_ver.0
        version: $name_ver.1
        desc:    $desc
      }
  }
}

# Package info as a structured record
def pkgi [pkg: string] {
  ^paru -Qi --color never $pkg
  | lines
  | each {|l|
      let parts = $l | split row ':'
      if ($parts | length) >= 2 {
        {key: ($parts.0 | str trim), value: ($parts | skip 1 | str join ':' | str trim)}
      }
  }
  | compact
  | transpose -r -d
}

# Which package owns a file
def pkgo [file: string] {
  ^paru -Qo --color never $file
  | parse '{file} is owned by {name} {version}'
  | first
}
