-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemoji.ps1
42 lines (34 loc) Β· 1.32 KB
/
emoji.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Prepare file
$root = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$emojiFile = "$root/emoji.json"
if ((Test-Path $emojiFile) -eq $false) {
Invoke-RestMethod "https://raw.githubusercontent.com/github/gemoji/master/db/emoji.json" | ConvertTo-Json | Out-File -FilePath $emojiFile
}
$rawEmojiData = Get-Content $emojiFile | ConvertFrom-Json
$emojiList = $rawEmojiData | ForEach-Object { $_.aliases }
$emojiTable = @{}
foreach ($emojiInfo in $rawEmojiData) {
foreach($alias in $emojiInfo.aliases) {
$emojiTable[$alias] = $emojiInfo.emoji
}
}
function emojify {
$text = $input
foreach ($emoji in $emojiList) {
$text = $text.Replace(":${emoji}:", $emojiTable[$emoji])
}
Write-Output $text
}
# Set autocompletion
Set-PSReadLineKeyHandler -Chord 'Shift+<' `
-BriefDescription GithubEmojiAutocompletion `
-LongDescription "Autocomplete github emoji" `
-ScriptBlock {
param($key, $arg)
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
$pickedEmoji = $emojiList | Invoke-Fzf
$textToInject = if ($null -eq $pickedEmoji) { "<" } else { $emojiTable[$pickedEmoji] }
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($textToInject)
}