Skip to content

Instantly share code, notes, and snippets.

@trevren11
Last active January 31, 2020 19:34
Show Gist options
  • Save trevren11/e1b19675e7d4255efeae64735915895a to your computer and use it in GitHub Desktop.
Save trevren11/e1b19675e7d4255efeae64735915895a to your computer and use it in GitHub Desktop.
Basic git menu options to share
###############################################################################
# Simple Textbased Powershell Menu
# Author : Michael Albert
# E-Mail : info@michlstechblog.info
# License: none, feel free to modify
# usage:
# Source the menu.ps1 file in your script:
# . .\menu.ps1
# fShowMenu requieres 2 Parameters:
# Parameter 1: [string]MenuTitle
# Parameter 2: [hashtable]@{[string]"ReturnString1"=[string]"Menu Entry 1";[string]"ReturnString2"=[string]"Menu Entry 2";[string]"ReturnString3"=[string]"Menu Entry 3"
# Return : Select String
# For example:
# fShowMenu "Choose your favorite Band" @{"sl"="Slayer";"me"="Metallica";"ex"="Exodus";"an"="Anthrax"}
# #############################################################################
function fShowMenu([System.String]$sMenuTitle,[System.Collections.Hashtable]$hMenuEntries, [System.Int16]$highlight)
{
# Orginal
[System.Int16]$iSavedBackgroundColor=[System.Console]::BackgroundColor
[System.Int16]$iSavedForegroundColor=[System.Console]::ForegroundColor
# Menu Colors
# inverse fore- and backgroundcolor
[System.Int16]$iMenuForeGroundColor=$iSavedForegroundColor
[System.Int16]$iMenuBackGroundColor=$iSavedBackgroundColor
# [System.Int16]$iMenuBackGroundColorSelectedLine=$iMenuForeGroundColor
[System.Int16]$iMenuForeGroundColorSelectedLine=$iMenuBackGroundColor
# Alternative, colors
# 4 is red (to use when deleting or something similar)
[System.Int16]$iMenuBackGroundColor=0
[System.Int16]$iMenuForeGroundColor=8
[System.Int16]$iMenuBackGroundColorSelectedLine=2
if ($highlight){
$iMenuBackGroundColorSelectedLine = $highlight
}
# Init
[System.Int16]$iMenuStartLineAbsolute=0
[System.Int16]$iMenuLoopCount=0
[System.Int16]$iMenuSelectLine=1
[System.Int16]$iMenuEntries=$hMenuEntries.Count
[Hashtable]$hMenu=@{};
[Hashtable]$hMenuHotKeyList=@{};
[Hashtable]$hMenuHotKeyListReverse=@{};
[System.Int16]$iMenuHotKeyChar=0
[System.String]$sValidChars=""
[System.Console]::WriteLine(" "+$sMenuTitle)
$iMenuLoopCount=1
$iMenuHotKeyChar=49
foreach ($sKey in $hMenuEntries.Keys){
$hMenu.Add([System.Int16]$iMenuLoopCount,[System.String]$sKey)
$hMenuHotKeyList.Add([System.Int16]$iMenuLoopCount,[System.Convert]::ToChar($iMenuHotKeyChar))
$hMenuHotKeyListReverse.Add([System.Convert]::ToChar($iMenuHotKeyChar),[System.Int16]$iMenuLoopCount)
$sValidChars+=[System.Convert]::ToChar($iMenuHotKeyChar)
$iMenuLoopCount++
$iMenuHotKeyChar++
if($iMenuHotKeyChar -eq 58){$iMenuHotKeyChar=97}
elseif($iMenuHotKeyChar -eq 123){$iMenuHotKeyChar=65}
elseif($iMenuHotKeyChar -eq 91){
Write-Error " Menu too big!"
exit(99)
}
}
# Remember Menu start
[System.Int16]$iBufferFullOffset=0
$iMenuStartLineAbsolute=[System.Console]::CursorTop
do{
####### Draw Menu #######
[System.Console]::CursorTop=($iMenuStartLineAbsolute-$iBufferFullOffset)
for ($iMenuLoopCount=1;$iMenuLoopCount -le $iMenuEntries;$iMenuLoopCount++){
[System.Console]::Write("`r")
[System.String]$sPreMenuline=""
$sPreMenuline=" "+$hMenuHotKeyList[[System.Int16]$iMenuLoopCount]
$sPreMenuline+=": "
if ($iMenuLoopCount -eq $iMenuSelectLine){
[System.Console]::BackgroundColor=$iMenuBackGroundColorSelectedLine
[System.Console]::ForegroundColor=$iMenuForeGroundColorSelectedLine
}
if ($hMenuEntries.Item([System.String]$hMenu.Item($iMenuLoopCount)).Length -gt 0){
[System.Console]::Write($sPreMenuline+$hMenuEntries.Item([System.String]$hMenu.Item($iMenuLoopCount)))
}
else{
[System.Console]::Write($sPreMenuline+$hMenu.Item($iMenuLoopCount))
}
[System.Console]::BackgroundColor=$iMenuBackGroundColor
[System.Console]::ForegroundColor=$iMenuForeGroundColor
[System.Console]::WriteLine("")
}
[System.Console]::BackgroundColor=$iMenuBackGroundColor
[System.Console]::ForegroundColor=$iMenuForeGroundColor
[System.Console]::Write(" Selected: " )
if (($iMenuStartLineAbsolute+$iMenuLoopCount) -gt [System.Console]::BufferHeight){
$iBufferFullOffset=($iMenuStartLineAbsolute+$iMenuLoopCount)-[System.Console]::BufferHeight
}
####### End Menu #######
####### Read Kex from Console
$oInputChar=[System.Console]::ReadKey($true)
if([System.Int16]$oInputChar.Key -eq [System.ConsoleKey]::Escape -or [System.Int16]$oInputChar.Key -eq [System.ConsoleKey]::Q){
# reset colors
[System.Console]::ForegroundColor=$iSavedForegroundColor
[System.Console]::BackgroundColor=$iSavedBackgroundColor
return ""
}
# Down Arrow or J?
elseif ([System.Int16]$oInputChar.Key -eq [System.ConsoleKey]::DownArrow -or [System.Int16]$oInputChar.Key -eq [System.ConsoleKey]::J){
if ($iMenuSelectLine -lt $iMenuEntries){
$iMenuSelectLine++
}
}
# Up Arrow or K
elseif([System.Int16]$oInputChar.Key -eq [System.ConsoleKey]::UpArrow -or [System.Int16]$oInputChar.Key -eq [System.ConsoleKey]::K){
if ($iMenuSelectLine -gt 1){
$iMenuSelectLine--
}
}
elseif([System.Char]::IsLetterOrDigit($oInputChar.KeyChar)){
[System.Console]::Write($oInputChar.KeyChar.ToString())
}
[System.Console]::BackgroundColor=$iMenuBackGroundColor
[System.Console]::ForegroundColor=$iMenuForeGroundColor
} while(([System.Int16]$oInputChar.Key -ne [System.ConsoleKey]::Enter) -and ($sValidChars.IndexOf($oInputChar.KeyChar) -eq -1))
# reset colors
[System.Console]::ForegroundColor=$iSavedForegroundColor
[System.Console]::BackgroundColor=$iSavedBackgroundColor
if($oInputChar.Key -eq [System.ConsoleKey]::Enter){
[System.Console]::Writeline($hMenuHotKeyList[$iMenuSelectLine])
return([System.String]$hMenu.Item($iMenuSelectLine))
}
else{
[System.Console]::Writeline("")
return($hMenu[$hMenuHotKeyListReverse[$oInputChar.KeyChar]])
}
}
$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
. $ScriptDir\Menu.ps1
function GitBranchesToHashtable {
$branches = git branch
$hash = @{}
$branches | foreach {
$displayName = $_.replace( '*', '').trim()
$value = $_.trim()
$hash.add($displayName, $value)
}
return $hash
}
# usage: db, delete -> delete branch
function Git-DeleteBranch {
$hash = GitBranchesToHashtable
$toDelete = fShowMenu "Delete Branch" $hash -highlight 4
& git branch -d $toDelete
}
New-Alias -Name db -Value Git-DeleteBranch
New-Alias -Name delete -Value Git-DeleteBranch
# usage: check, checkout -> git checkout <branch name> | args, alternately use empty and checkout from menu
function Git-Checkout {
if ([string]::IsNullOrEmpty($args)){
$hash = GitBranchesToHashtable
$checkout = fShowMenu "Checkout Branch" $hash
write-host "checkout $checkout"
& git checkout $checkout
}
else{
& git checkout $args
}
}
New-Alias -Name check -Value Git-Checkout
New-Alias -Name checkout -Value Git-Checkout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment