let carapace_completer = {|spans: list<string>|
  ^carapace $spans.0 nushell ...$spans
  | from json
  | if ($in | default [] | where value =~ '^-.*ERR$' | is-empty) { $in } else { null }
}

$env.config = {
  show_banner: false
  buffer_editor: "nvim"
  edit_mode: vi

  cursor_shape: {
    vi_insert: line
    vi_normal: block
  }

  history: {
    max_size: 100_000
    sync_on_enter: true
    file_format: "plaintext"
  }

  completions: {
    case_sensitive: false
    quick: true
    partial: true
    algorithm: "fuzzy"
    external: {
      enable: true
      completer: $carapace_completer
    }
  }

  keybindings: [
    {
      name: cycle_history_args
      modifier: alt
      keycode: char_.
      mode: [vi_insert, emacs]
      event: { send: executehostcommand, cmd: "last-arg-cycle" }
    }
    {
      name: history_cmd_pick
      modifier: alt
      keycode: char_u002c
      mode: [vi_insert, emacs]
      event: { send: executehostcommand, cmd: "history-cmd-pick" }
    }
  ]
}

plugin use gstat
plugin use polars
plugin use formats
plugin use query
plugin use skim
plugin use dns
plugin use audio
plugin use highlight
plugin use file
plugin use ls_colorize
plugin use regex
plugin use json_path
plugin use semver
plugin use parquet

# All files including hidden, sorted by type then name
def ll [path?: path] {
  ls -a ($path | default .) | sort-by type name
}

# Files only, sorted by size descending
def lf [path?: path] {
  ls -a ($path | default .) | where type == file | sort-by size -r
}

# Directories only, sorted by name
def ld [path?: path] {
  ls -a ($path | default .) | where type == dir | sort-by name
}

# Listing with per-file git status column
def lg [path?: path] {
  let files = ls -a ($path | default .) | sort-by type name
  let dirty = try {
    ^git status --porcelain
    | lines
    | each {|l| {name: ($l | str substring 3..), git: ($l | str substring 0..2 | str trim)}}
  } catch { [] }
  if ($dirty | is-empty) { $files } else {
    $files | join $dirty name --left
  }
}

# Repo-level git status as a structured record
def gs [] { gstat }

# Show all custom commands with their descriptions
def nu-help [] {
  help commands
  | where command_type == "custom"
  | select name description
  | sort-by name
}

alias ez  = ^eza --icons
alias ezl = ^eza -la --git --icons

alias cat = bat --paging=never

source ($nu.default-config-dir | path join "zoxide.nu")

source ($nu.default-config-dir | path join "k8s.nu")
source ($nu.default-config-dir | path join "pkg.nu")
source ($nu.default-config-dir | path join "git.nu")
source ($nu.default-config-dir | path join "sys.nu")
source ($nu.default-config-dir | path join "hypr.nu")
source ($nu.default-config-dir | path join "dev.nu")

use ~/.config/nushell/themes/{{ .chezmoi.config.data.theme }}.nu
source ($nu.default-config-dir | path join "prompt.nu")
