Browse Source

Merge pull request #34 from haukened/dev

Unicode Emoji Support
pull/37/head
Gregory Rudolph 5 years ago committed by GitHub
parent
commit
938d6c855c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      .gitignore
  2. 47
      emojiMap.go
  3. 2
      go.mod
  4. 2
      go.sum
  5. 75
      mage.go
  6. 15
      main.go
  7. 12
      tabComplete.go

1
.gitignore vendored

@ -6,3 +6,4 @@ emojiList.go
.idea/* .idea/*
.idea .idea
*.log *.log
.travis.yml

47
emojiMap.go

File diff suppressed because one or more lines are too long

2
go.mod

@ -6,7 +6,7 @@ require (
github.com/awesome-gocui/gocui v0.6.0 github.com/awesome-gocui/gocui v0.6.0
github.com/magefile/mage v1.9.0 github.com/magefile/mage v1.9.0
github.com/mattn/go-runewidth v0.0.5 // indirect github.com/mattn/go-runewidth v0.0.5 // indirect
github.com/pelletier/go-toml v1.5.0 github.com/pelletier/go-toml v1.6.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
samhofi.us/x/keybase v0.0.0-20191023034410-b00e56e8dd3c samhofi.us/x/keybase v0.0.0-20191023034410-b00e56e8dd3c
) )

2
go.sum

@ -14,6 +14,8 @@ github.com/mattn/go-runewidth v0.0.5 h1:jrGtp51JOKTWgvLFzfG6OtZOJcK2sEnzc/U+zw7T
github.com/mattn/go-runewidth v0.0.5/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.5/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/pelletier/go-toml v1.5.0 h1:5BakdOZdtKJ1FFk6QdL8iSGrMWsXgchNJcrnarjbmJQ= github.com/pelletier/go-toml v1.5.0 h1:5BakdOZdtKJ1FFk6QdL8iSGrMWsXgchNJcrnarjbmJQ=
github.com/pelletier/go-toml v1.5.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys= github.com/pelletier/go-toml v1.5.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
github.com/pelletier/go-toml v1.6.0 h1:aetoXYr0Tv7xRU/V4B4IZJ2QcbtMUFoNb3ORp7TzIK4=
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

75
mage.go

@ -3,65 +3,13 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http"
"os" "os"
"strings"
"github.com/magefile/mage/mg" "github.com/magefile/mage/mg"
"github.com/magefile/mage/sh" "github.com/magefile/mage/sh"
) )
// emoji related constants
const emojiList = "https://raw.githubusercontent.com/CodeFreezr/emojo/master/db/v5/emoji-v5.json"
const emojiFileName = "emojiList.go"
// json parsing structure
type emoji struct {
Num int `json:"No"`
Emoji string `json:"Emoji"`
Category string `json:"Category"`
SubCategory string `json:"SubCategory"`
Unicode string `json:"Unicode"`
Name string `json:"Name"`
Tags string `json:"Tags"`
Shortcode string `json:"Shortcode"`
}
// This func downloaded and parses the emojis from online into a slice of all shortnames
// to be used as a lookup for tab completion for emojis
// this way the pull from GitHub only has to be done at build time.
func createEmojiSlice() ([]string, error) {
result, err := http.Get(emojiList)
if err != nil {
return nil, err
}
defer result.Body.Close()
emojiList, err := ioutil.ReadAll(result.Body)
if err != nil {
return nil, err
}
var emojis []emoji
if err := json.Unmarshal(emojiList, &emojis); err != nil {
return nil, err
}
var emojiSlice []string
for _, emj := range emojis {
if len(emj.Shortcode) == 0 || strings.Contains(emj.Shortcode, "_tone") {
// dont add them
continue
}
emojiSlice = append(emojiSlice, emj.Shortcode)
}
return emojiSlice, nil
}
func getRemotePackages() error { func getRemotePackages() error {
var packages = []string{ var packages = []string{
"samhofi.us/x/keybase", "samhofi.us/x/keybase",
@ -87,28 +35,6 @@ func exit(err error) {
} }
} }
// Build kbtui with emoji lookup support
func BuildEmoji() error {
mg.Deps(getRemotePackages)
emojis, err := createEmojiSlice()
if err != nil {
return err
}
f, err := os.Create(emojiFileName)
if err != nil {
return err
}
defer f.Close()
fileContent := fmt.Sprintf("package main\n\nvar emojiSlice = %#v", emojis)
_, err = f.WriteString(fileContent)
if err != nil {
return err
}
f.Sync()
return nil
}
// Build kbtui with just the basic commands. // Build kbtui with just the basic commands.
func Build() { func Build() {
mg.Deps(getRemotePackages) mg.Deps(getRemotePackages)
@ -167,7 +93,6 @@ func BuildAllCommandsT() {
// Build kbtui with beta functionality // Build kbtui with beta functionality
func BuildBeta() { func BuildBeta() {
mg.Deps(getRemotePackages) mg.Deps(getRemotePackages)
mg.Deps(BuildEmoji)
if err := sh.Run("go", "build", "-tags", "allcommands showreactionscmd emojiList tabcompletion"); err != nil { if err := sh.Run("go", "build", "-tags", "allcommands showreactionscmd emojiList tabcompletion"); err != nil {
defer func() { defer func() {
exit(err) exit(err)

15
main.go

@ -274,8 +274,12 @@ func printToView(viewName string, message string) {
updatingView, err := g.View(viewName) updatingView, err := g.View(viewName)
if err != nil { if err != nil {
return err return err
} else {
if config.Basics.UnicodeEmojis {
message = emojiUnicodeConvert(message)
}
fmt.Fprintf(updatingView, "%s\n", message)
} }
fmt.Fprintf(updatingView, "%s\n", message)
return nil return nil
}) })
} }
@ -545,10 +549,6 @@ func deleteEmpty(s []string) []string {
func handleInput(viewName string) error { func handleInput(viewName string) error {
clearView(viewName) clearView(viewName)
inputString, _ := getInputString(viewName) inputString, _ := getInputString(viewName)
if newViewTitle := getViewTitle(viewName); newViewTitle != "" {
// restore any tab completion view titles on input commit
setViewTitle(viewName, newViewTitle)
}
if inputString == "" { if inputString == "" {
return nil return nil
} }
@ -572,8 +572,13 @@ func handleInput(viewName string) error {
cmd[0] = inputString[:1] cmd[0] = inputString[:1]
RunCommand(cmd...) RunCommand(cmd...)
} else { } else {
inputString = resolveRootEmojis(inputString)
go sendChat(inputString) go sendChat(inputString)
} }
// restore any tab completion view titles on input commit
if newViewTitle := getViewTitle(viewName); newViewTitle != "" {
setViewTitle(viewName, newViewTitle)
}
go populateList() go populateList()
return nil return nil

12
tabComplete.go

@ -66,7 +66,8 @@ func handleTab(viewName string) error {
// Main tab completion functions // Main tab completion functions
func getEmojiTabCompletionSlice(inputWord string) []string { func getEmojiTabCompletionSlice(inputWord string) []string {
// use the emojiSlice from emojiList.go and filter it for the input word // use the emojiSlice from emojiList.go and filter it for the input word
resultSlice := filterStringSlice(emojiSlice, inputWord) //resultSlice := filterStringSlice(emojiSlice, inputWord)
resultSlice := filterEmojiMap(emojiMap, inputWord)
return resultSlice return resultSlice
} }
func getChannelTabCompletionSlice(inputWord string) []string { func getChannelTabCompletionSlice(inputWord string) []string {
@ -152,6 +153,15 @@ func filterStringSlice(ss []string, fv string) []string {
} }
return rs return rs
} }
func filterEmojiMap(eMap map[string]emojiData, fv string) []string {
var rs []string
for k, _ := range eMap {
if strings.HasPrefix(k, fv) {
rs = append(rs, k)
}
}
return rs
}
func longestCommonPrefix(ss []string) string { func longestCommonPrefix(ss []string) string {
// cover the case where the slice has no or one members // cover the case where the slice has no or one members
switch len(ss) { switch len(ss) {

Loading…
Cancel
Save