ShellServer.psm1

$LocalHost = '127.0.0.1'
# if you change the port number here, you must change by the end of file too
# it's inside Register-EngineEvent { $Sock.Connect('127.0.0.1', 5432)<-THIS }
$Port = 5432
$Enc = [System.Text.Encoding]::UTF8
$Sock = New-Object System.Net.Sockets.UdpClient
$Sock.Connect($LocalHost, $Port)

$Address = [System.Net.IpAddress]::Parse($LocalHost)
$End = New-Object System.Net.IPEndPoint $Address, $Port

$Buffer = $Enc.GetBytes('#Init')
$Sock.Send($Buffer) > $nul

Set-PSReadLineKeyHandler -Key Enter -ScriptBlock {
  $origDollarQuestion = $global:?
  $origLastExitCode = $global:LASTEXITCODE

  $line = $cursor = $null 
  [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)

  [Console]::SetCursorPosition(0, [Console]::GetCursorPosition().Item2 - 2)
  [Console]::Write("`e[J")

  [Console]::Write("`e[34m❯`e[0m $line")

  [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()

  $global:LASTEXITCODE = $origLastExitCode
  if ($global:? -ne $origDollarQuestion) {
      if ($origDollarQuestion) {
          1+1
      } else {
          Write-Error '' -ErrorAction 'Ignore'
      }
  }
}


function global:prompt {
  $origDollarQuestion = $global:?
  $origLastExitCode = $global:LASTEXITCODE

  if (-Not (Test-Path 'ENV:VIRTUAL_ENV')) {
    Write-Host ''
  }

  $width = $Host.UI.RawUI.BufferSize.Width
  $duration = (Get-History -Count 1).Duration
  if (-Not $duration) {
      $duration = 0.0
  } else {
      $duration = [string]$duration.Seconds + '.' + [string]$duration.Milliseconds
    }

  $Buffer = $Enc.GetBytes('%' + [int] $origDollarQuestion + (Get-Location).Path + ';' + $width + ';' + $duration)
  $Sock.Send($Buffer) > $nul

  $response = $Enc.GetString($Sock.Receive([ref] $End))
  $ExecutionContext.InvokeCommand.ExpandString($response)

  $global:LASTEXITCODE = $origLastExitCode
  if ($global:? -ne $origDollarQuestion) {
      if ($origDollarQuestion) {
          1+1
      } else {
          Write-Error '' -ErrorAction 'Ignore'
      }
  }
}

function p {
  if (-not ($args)) {
      Set-Location
    } else {

    $Buffer = $Enc.GetBytes('!' + $args -join ' ')
    $Sock.Send($Buffer) > $nul
    $response = $Enc.GetString($Sock.Receive([ref] $End))

      if ($response) {
       Set-Location $response
      } else {
        Write-Output "ShellServer: No match found."
        }
      }
}

function pz {
  $Buffer = $Enc.GetBytes('*')
  $Sock.Send($Buffer) > $nul
  $response = $Enc.GetString($Sock.Receive([ref] $End))
  $query = $args -Join ' '

  if ($query) {
    $answer = Write-Output $response | fzf --height=~20 --layout=reverse -q $query
  } else {
      $answer = Write-Output $response | fzf --height=~20 --layout=reverse
    }

  $Sock.Send($Enc.GetBytes('*' + $answer)) > $nul
   Set-Location $answer
}

function ls {
  param($opts, $path)
  if ($path) {
    $cwd = (Resolve-Path $path).Path
  } else {
      $cwd = (Get-Location).Path
    }
  $Buffer = $Enc.GetBytes("@$opts;$cwd")
  $Sock.Send($Buffer) > $nul
  $response = $Enc.GetString($Sock.Receive([ref] $End))
  $ExecutionContext.InvokeCommand.ExpandString($response)
}

function ll {
  param($path)
  ls '-lc' $path
}

function la {
  param($path)
  ls '-acl' $path
}

Register-EngineEvent PowerShell.Exiting -action {
  $Sock = New-Object System.Net.Sockets.UdpClient
  #
  # Change port number below if you changed above
  #
  $Sock.Connect('127.0.0.1', 5432)
  $Sock.Send([System.Text.Encoding]::UTF8.GetBytes('#Exit'))
} > $nul

Export-ModuleMember -Function @("p", "pz", "ls", "ll", "la")