<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Piotr Stapp</title>
    <description>The latest articles on DEV Community by Piotr Stapp (@ptrstpp950).</description>
    <link>https://dev.to/ptrstpp950</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F113390%2F26b92bf2-b38d-407f-ad63-48656c88a876.jpeg</url>
      <title>DEV Community: Piotr Stapp</title>
      <link>https://dev.to/ptrstpp950</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ptrstpp950"/>
    <language>en</language>
    <item>
      <title>6 steps to pimp my terminal</title>
      <dc:creator>Piotr Stapp</dc:creator>
      <pubDate>Wed, 02 Jun 2021 09:40:27 +0000</pubDate>
      <link>https://dev.to/ptrstpp950/6-steps-to-pimp-my-terminal-3501</link>
      <guid>https://dev.to/ptrstpp950/6-steps-to-pimp-my-terminal-3501</guid>
      <description>&lt;h2&gt;
  
  
  Tools
&lt;/h2&gt;

&lt;p&gt;My current setup is mixed of tools from following list:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;oh-my-posh + powerlevel10k_rainbow (with mods) theme + custom font&lt;/li&gt;
&lt;li&gt;posh-git&lt;/li&gt;
&lt;li&gt;Set-PSReadlineKeyHandler for "bash style" search + autocomplete&lt;/li&gt;
&lt;li&gt;PSKubectlCompletion with PSKubeContext for kubernetes&lt;/li&gt;
&lt;li&gt;Register-ArgumentCompleter for dotnet&lt;/li&gt;
&lt;li&gt;MagicTooltips which is responsible for displaying tooltips for kubectl and az&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you just want to copy-paste, scroll down to the end and copy what you need from my $PROFILE file 😅&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1 - standard + small hacks
&lt;/h2&gt;

&lt;p&gt;The posh-git and oh-my-posh modules are quite popular, but will intruce them shortly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;posh-git displays info about git status &lt;/li&gt;
&lt;li&gt;oh-my-posh allows to displays a lot of segments (like: time, battery, last exit code, kubernetes, aws, az, ....) &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are numerous themes for oh-my-posh. I'm using &lt;a href="https://ohmyposh.dev/docs/themes/#powerlevel10k%20rainbow" rel="noopener noreferrer"&gt;powerlevel10k rainbow&lt;/a&gt;, but there are missing icons even on the oh-my-posh website. You'll need a unique typeface to get this appearance. Oh-my-posh recommends &lt;a href="https://ohmyposh.dev/docs/fonts" rel="noopener noreferrer"&gt;Meslo LGM NF&lt;/a&gt;, but I went with &lt;a href="https://github.com/adam7/delugia-code" rel="noopener noreferrer"&gt;Delugia Nerd Font Complete&lt;/a&gt;. Is is a Cascadia Code "Extended", which includes Nerd Fonts and powerline glyphs.&lt;/p&gt;

&lt;p&gt;You must "force it" in Windows Terminal JSON after installation (to access settings, press CTRL+, then the gear icon on the left):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; "profiles": 
    {
        "defaults": 
        {
            "fontFace": "Delugia Nerd Font Complete"
        },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The final outcome is awesome:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjfgysvqm61a5dhvmo4wn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjfgysvqm61a5dhvmo4wn.png" alt="terminal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, remember to include your $PROFILE file (the best approach is to modify it using &lt;code&gt;code $PROFILE&lt;/code&gt; if you have VS Code installed):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# posh git
Import-Module-With-Measure posh-git

# oh-my-posh
Import-Module-With-Measure oh-my-posh
Set-PoshPrompt -Theme powerlevel10k_rainbow
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: bash style history search and autocomplete
&lt;/h2&gt;

&lt;p&gt;Do you get tired of typing CTRL+R to look up commands in the history? Is it easier to just use arrows to find the last invocation of &lt;code&gt;kubectl get&lt;/code&gt;, for example? I was one of them. Simply add the following to your profile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# up&amp;amp;down arrow for history search
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add one more if you'd like to view all autocomplete options after pressing TAB:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# menu complete using TAB instead of CTRL+SPACE
Set-PSReadlineKeyHandler -Chord Tab -Function MenuComplete
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Still not convinced? It's time for a little demonstration:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/CZOwAJEZf0M"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Yummy 😋&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Autocomplete
&lt;/h2&gt;

&lt;p&gt;Today I'm only using dotnet and kubernetes autocomplete. I hope that soon I could use also &lt;code&gt;az&lt;/code&gt;.&lt;br&gt;
Kubernetes config I described in &lt;a href="https://stapp.space/pskube-context/" rel="noopener noreferrer"&gt;my last post&lt;/a&gt;, &lt;/p&gt;

&lt;p&gt;Anyway, the dotnet command is easy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#dotnet completion
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
    param($commandName, $wordToComplete, $cursorPosition)
    dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
        [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What about Az CLI? The proof of concept is easy, but it isn't a top priority for Azure team 😥, just check &lt;a href="https://github.com/Azure/azure-cli/pull/13576" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;br&gt;
The &lt;a href="https://gist.github.com/travis-c-lagrone/3ea3393ba8f92b508ee24e237d37bfe2" rel="noopener noreferrer"&gt;gist&lt;/a&gt; is the only thing I've found, but I'm not using it.&lt;/p&gt;

&lt;p&gt;If you need more stuff for autocomplete, just search, implement or rewrite bash to PowerShell. No shorthands, sorry 😥&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 4: Magic tooltips
&lt;/h2&gt;

&lt;p&gt;That's great for displaying dynamic segments. What does this imply? When I type &lt;code&gt;kubectl&amp;lt;SPACE&amp;gt;&lt;/code&gt;, for example, I want to see information about Kubernetes context, but I don't need it most of the time. The same goes for &lt;code&gt;az&lt;/code&gt; and &lt;code&gt;aws&lt;/code&gt;.&lt;br&gt;
The oh-my-posh author &lt;a href="https://github.com/JanDeDobbeleer/oh-my-posh/discussions/772" rel="noopener noreferrer"&gt;guides me&lt;/a&gt; to a &lt;a href="https://github.com/TravisTX/MagicTooltips/" rel="noopener noreferrer"&gt;MagicTooltips module&lt;/a&gt;. I'll probably have to submit a PR there to include namespace as well.&lt;br&gt;
Anyway, I choose to set it up as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$global:MagicTooltipsSettings = @{
    VerticalOffset = 0
    HorizontalOffset = 4
    HorizontalAlignment = "Right"
    Providers= @{
        Kubernetes = @{
            Commands = "kubectl,helm,kubens,kubectx,k9s,k"
        }
    }
}
Import-Module MagicTooltips
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, to make your theme seem good, you'll probably need to set &lt;code&gt;HorizontalOffset&lt;/code&gt; and &lt;code&gt;VerticalOffset&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;My powerlevel 10k setup looks like this (the cluster name is under the clock):&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft2a02077hzhcnggb0w1q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft2a02077hzhcnggb0w1q.png" alt="MagicTooltips demo"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 5: find slow modules/segments
&lt;/h2&gt;

&lt;p&gt;I noticed performance issues after installing various extensions. To repair it, I chose to do the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;remove unused segments in powerlevel theme&lt;/li&gt;
&lt;li&gt;measure Import-Module&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Step 6: having "sudo" mode
&lt;/h2&gt;

&lt;p&gt;Isn't it awesome to use sudo in a terminal?&lt;br&gt;
I used two functions to implement it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fcuk {
    $cmd = (Get-History ((Get-History).Count))[0].CommandLine
    Write-Host "Running $cmd in $PWD"
    start-process pwsh -verb runas -WorkingDirectory $PWD -ArgumentList "-NoExit -Command pushd $PWD; Write-host 'cmd to run: $cmd'; $cmd"
}

function sudo {
    if ($first -eq '!!') {
        fcuk;
    }
    else {
        $file=$args[0];
        [string]$arguments = "";
        if ($args.Count -gt 1) {
            $c = $args.Count - 1;
            [string]$arguments = $args[1..$c]
        }
        Write-Host "file = $file args = $arguments";
        start-process $file -verb runas -WorkingDirectory $PWD -ArgumentList $arguments;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  My $PROFILE file
&lt;/h2&gt;

&lt;p&gt;Finally, here's what my $PROFILE file looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# helper
function Import-Module-With-Measure {
    param ($ModuleName)
    $import = Measure-Command {
        Import-Module $ModuleName
    }
    Write-Host "$ModuleName import $($import.TotalMilliseconds) ms"
}

# posh git
Import-Module-With-Measure posh-git

# oh-my-posh
Import-Module-With-Measure oh-my-posh
Set-PoshPrompt -Theme powerlevel10k_rainbow

# menu complete using TAB instead of CTRL+SPACE
Set-PSReadlineKeyHandler -Chord Tab -Function MenuComplete
# up&amp;amp;down arrow for history search
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward

# kubectl + stuff 
Import-Module-With-Measure PSKubectlCompletion
Set-Alias k -Value kubectl
Register-KubectlCompletion

Import-Module-With-Measure PSKubeContext
Set-Alias kubens -Value Select-KubeNamespace
Set-Alias kubectx -Value Select-KubeContext
Register-PSKubeContextComplete


# magic tooltip config
$global:MagicTooltipsSettings = @{
    VerticalOffset = 0
    HorizontalOffset = 4
    HorizontalAlignment = "Right"
    Providers= @{
        Kubernetes = @{
            Commands = "kubectl,helm,kubens,kubectx,k9s,k"
        }
    }
}
Import-Module MagicTooltips


#dotnet completion
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
    param($commandName, $wordToComplete, $cursorPosition)
    dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
        [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
    }
}

# SUDO functions
function fcuk {
    $cmd = (Get-History ((Get-History).Count))[0].CommandLine
    Write-Host "Running $cmd in $PWD"
    start-process pwsh -verb runas -WorkingDirectory $PWD -ArgumentList "-NoExit -Command pushd $PWD; Write-host 'cmd to run: $cmd'; $cmd"
}

function sudo {
    if ($first -eq '!!') {
        fcuk;
    }
    else {
        $file=$args[0];
        [string]$arguments = "";
        if ($args.Count -gt 1) {
            $c = $args.Count - 1;
            [string]$arguments = $args[1..$c]
        }
        Write-Host "file = $file args = $arguments";
        start-process $file -verb runas -WorkingDirectory $PWD -ArgumentList $arguments;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What comes next?
&lt;/h2&gt;

&lt;p&gt;You can also find some interesting configurations at:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Terminal-Icons module - check &lt;a href="https://github.com/devblackops/Terminal-Icons#screenshots" rel="noopener noreferrer"&gt;screenshot&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dominikjeske.github.io/pimp-my-terminal/" rel="noopener noreferrer"&gt;Pimp my terminal&lt;/a&gt; by Dominik Jeske - check shortcuts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Maybe you have something useful in your setup?  If yes please share!&lt;/p&gt;

</description>
      <category>windows</category>
      <category>terminal</category>
      <category>powershell</category>
      <category>pwsh</category>
    </item>
    <item>
      <title>Make PowerShell with k8s great again</title>
      <dc:creator>Piotr Stapp</dc:creator>
      <pubDate>Mon, 31 May 2021 14:16:27 +0000</pubDate>
      <link>https://dev.to/ptrstpp950/make-powershell-with-k8s-great-again-3pjg</link>
      <guid>https://dev.to/ptrstpp950/make-powershell-with-k8s-great-again-3pjg</guid>
      <description>&lt;p&gt;So, like me, you're using Windows. WSL isn't always reliable (e.g. VPN problems), yet you're working with Kubernetes and require autocomplete tools. I'm the same way :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Kubectl autocomplete
&lt;/h2&gt;

&lt;p&gt;With bash or zsh, having autocomplete is straightforward. Almost only one command is required to complete the task:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source &amp;lt;(kubectl completion bash)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, this will obviously not work in PowerShell😅. Especially when you look what and how many lines the &lt;code&gt;kubectl completion bash&lt;/code&gt; command produces and how many lines it generates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl completion bash | wc -l
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;14143 is the correct answer! To implement autocomplete, more than 14k lines of code were written!&lt;br&gt;
How to use it on Windows?  Is it possible to move it? Yes, and thankfully, someone else did the legwork for you: &lt;a href="https://github.com/mziyabo/PSKubectlCompletion/blob/master/PSKubectlCompletion.psm1"&gt;https://github.com/mziyabo/PSKubectlCompletion/blob/master/PSKubectlCompletion.psm1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use autocomplete we will need the &lt;a href="https://github.com/mziyabo/PSKubectlCompletion"&gt;PSKubectlCompletion module&lt;/a&gt;. Install it with &lt;code&gt;Install-Module PSKubectlCompletion&lt;/code&gt; and include with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Import-Module PSKubectlCompletion
Set-Alias k -Value kubectl
Register-KubectlCompletion
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point the basics are working quite well. But how about other tools? Like ...&lt;/p&gt;

&lt;h2&gt;
  
  
  kubectx and kubens
&lt;/h2&gt;

&lt;p&gt;If you never use the original ones these are two simple commands to switch your current Kubernetes context (&lt;code&gt;kubectx&lt;/code&gt;) and namespace (&lt;code&gt;kubens&lt;/code&gt;). In PowerShell world they should be named rather like Select-KubeContext and Select-KubeNamespace, but I will you aliases instead 😜&lt;/p&gt;

&lt;p&gt;And for above someone else did the work for you once more. It's me this time: &lt;a href="https://github.com/ptrstpp950/PSKubeContext"&gt;https://github.com/ptrstpp950/PSKubeContext&lt;/a&gt;&lt;br&gt;
How it works? Just let me present a small YouTube:&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/_6XjdLD0TWo"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Great, isn't it?  I'm so glad it works that I made and published a module called &lt;a href="https://www.powershellgallery.com/packages/PSKubeContext"&gt;PSKubeContext&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To utilize it, install it with &lt;code&gt;Install-Module PSKubeContext&lt;/code&gt;, and simply add the following to your profile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Import-Module PSKubeContext
Set-Alias kubens -Value Select-KubeNamespace
Set-Alias kubectx -Value Select-KubeContext
Register-PSKubeContextComplete
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's inside:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Select-KubeContext&lt;/code&gt; or if you prefer alias &lt;code&gt;kubectx&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Select-KubeNamespace&lt;/code&gt; or &lt;code&gt;kubens&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;100% autocomplete using  🙈🙉🙊&lt;/li&gt;
&lt;li&gt;A menu in command line to select the context/namespace if something goes wrong 😅&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that's it. Your PowerShell is great again and works well with Kubernetes CLI.&lt;/p&gt;

</description>
      <category>powershell</category>
      <category>pwsh</category>
      <category>k8s</category>
      <category>kubernetes</category>
    </item>
    <item>
      <title>Improve the performance of git on WSL2</title>
      <dc:creator>Piotr Stapp</dc:creator>
      <pubDate>Wed, 19 May 2021 07:24:35 +0000</pubDate>
      <link>https://dev.to/ptrstpp950/improve-the-performance-of-git-on-wsl2-4mmk</link>
      <guid>https://dev.to/ptrstpp950/improve-the-performance-of-git-on-wsl2-4mmk</guid>
      <description>&lt;p&gt;I've returned to Windows after a year on the Mac. I built up pwsh and added a lot of tools, but I still need some Linux/MacOS ones, such as &lt;code&gt;envsubst&lt;/code&gt;. Of course, WSL2 comes to the rescue, but you'll have to checkout the repo twice for Linux and Visual Studio. It is ridiculous.&lt;br&gt;
I set up oh-my-posh with powerline on WSL2, which is a fantastic combination. However, there is a "but": git status is quite slow. What is the reason for this? Just read &lt;a href="https://github.com/microsoft/WSL/issues/4197"&gt;4197&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How can this be resolved? Simply replace the git command with the git function, as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function git() {
  if [[ $(pwd -P) = /mnt/* ]]; then
    git.exe "$@"
  else
    command git "$@"
  fi
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's all 🙈🙉🙊&lt;/p&gt;

</description>
      <category>windows</category>
      <category>wsl2</category>
      <category>git</category>
    </item>
  </channel>
</rss>
