sup.psm1

function Get-NvimInstances {
    <#
    .SYNOPSIS
        Get all running Neovim instances.

    .DESCRIPTION
        Finds all Neovim server named pipes on Windows.
        Returns pipe names in format: nvim.<PID>.0

    .EXAMPLE
        Get-NvimInstances
        # Returns: nvim.12345.0, nvim.67890.0, etc.

    .OUTPUTS
        System.String[]
    #>


    [CmdletBinding()]
    param()

    Get-ChildItem '//./pipe/' -ErrorAction SilentlyContinue |
        Where-Object { $_.Name -match '^nvim\.\d+\.0$' } |
        Select-Object -ExpandProperty Name
}

function Send-Sup {
    <#
    .SYNOPSIS
        Send a notification to all running Neovim instances.

    .DESCRIPTION
        Finds all Neovim server pipes and sends a vim.notify() call to each
        using nvim --server --remote-expr via RPC.

        Requires nvim to be installed and available in PATH.

    .PARAMETER Message
        The notification message to send.

    .PARAMETER Level
        The notification level: INFO, WARN, or ERROR. Default is INFO.

    .PARAMETER Quiet
        Suppress output about which instances were notified.

    .EXAMPLE
        Send-Sup "Build complete!"
        # Sends INFO notification to all nvim instances

    .EXAMPLE
        Send-Sup "Something went wrong" -Level ERROR
        # Sends ERROR notification

    .EXAMPLE
        sup "Hello world"
        # Using the alias

    .LINK
        https://neovim.io/doc/user/remote.html
    #>


    [CmdletBinding()]
    [Alias('sup')]
    param(
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
        [string]$Message,

        [Parameter(Mandatory = $false)]
        [ValidateSet("INFO", "WARN", "ERROR")]
        [Alias('l')]
        [string]$Level = "INFO",

        [Parameter(Mandatory = $false)]
        [Alias('q')]
        [switch]$Quiet
    )

    begin {
        $levelMap = @{
            "INFO"  = "vim.log.levels.INFO"
            "WARN"  = "vim.log.levels.WARN"
            "ERROR" = "vim.log.levels.ERROR"
        }

        $pipes = Get-NvimInstances

        if (-not $pipes) {
            Write-Warning "No running Neovim instances found"
            return
        }
    }

    process {
        $escapedMessage = $Message -replace "'", "''" -replace '"', '\"'
        $luaLevel = $levelMap[$Level]

        $count = 0
        foreach ($pipe in $pipes) {
            $serverAddr = "\\.\pipe\$pipe"
            try {
                $null = nvim --server $serverAddr --remote-expr "luaeval(""vim.notify('$escapedMessage', $luaLevel)"")" 2>&1
                $count++
                if (-not $Quiet) {
                    Write-Host "Notified: $pipe" -ForegroundColor Green
                }
            }
            catch {
                if (-not $Quiet) {
                    Write-Host "Failed: $pipe" -ForegroundColor Red
                }
            }
        }

        if (-not $Quiet) {
            Write-Host "Sent to $count instance(s)" -ForegroundColor Cyan
        }
    }
}