Skip to content

Commit

Permalink
👔 up(show): update some for emoji render and some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 19, 2023
1 parent bc43572 commit 67c2b18
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
12 changes: 12 additions & 0 deletions show/banner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package show

/*
eg:
╭──────────────────────────────────────────────────────────────────╮
│ │
│ Update available! 3.21.0 → 3.27.0. │
│ Changelog: https://github.com/gookit/gcli/releases/tag/v3.2.0 │
│ Run "x y z" to update. │
│ │
╰──────────────────────────────────────────────────────────────────╯
*/
49 changes: 27 additions & 22 deletions show/emoji/emoji.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package emoji

import (
"bytes"
"regexp"
"strconv"
"strings"
)

var nameMatch = regexp.MustCompile(`(:\w+:)`)
var codeMatch = regexp.MustCompile(`(:\w+:)`)

// Emoji is alias of the GetByName()
func Emoji(name string) string {
return GetByName(name)
}

// GetByName returns the unicode value for the given emoji name. If the
// specified emoji does not exist, will returns the input string.
// specified emoji does not exist, will return the input string.
func GetByName(name string) string {
if val, ok := emojiMap[name]; ok {
return val
Expand Down Expand Up @@ -50,23 +49,27 @@ func Search(kw string, limits ...int) (ret map[string]string) {
}

// Render a string, parse emoji name, returns rendered string.
//
// Usage:
// msg := Render("a :smile: message")
// fmt.Println(msg)
//
// msg := Render("a :smile: message")
// fmt.Println(msg)
func Render(str string) string {
// not contains emoji name.
if strings.IndexByte(str, ':') == -1 {
return str
}

return nameMatch.ReplaceAllStringFunc(str, func(name string) string {
return codeMatch.ReplaceAllStringFunc(str, func(name string) string {
return GetByName(name) // + " "
})
}

// FromUnicode unicode string to emoji string
//
// Usage:
// emoji := FromUnicode("\U0001f496")
//
// emoji := FromUnicode("\U0001f496")
func FromUnicode(s string) string {
// emoji表情的数据表达式
re := regexp.MustCompile("\\[[\\\\u0-9a-zA-Z]+\\]")
Expand All @@ -84,32 +87,36 @@ func FromUnicode(s string) string {
return s
}

// ToUnicode unicode string to emoji string
// ToUnicode convert emoji to unicode string
// Usage:
// unicode := ToUnicode("💖")
// fmt.Print(unicode) // "1f496"
// // with prefix
// unicode := ToUnicode("💖", "\U000") // "\U0001f496"
// fmt.Print(unicode) // "💖"
//
// unicode := ToUnicode("💖")
// fmt.Print(unicode) // "1f496"
//
// // with prefix
// unicode := ToUnicode("💖", "\U000") // "\U0001f496"
// fmt.Print(unicode) // "💖"
func ToUnicode(emoji string, prefix ...string) string {
code := strconv.FormatInt(int64(emoji[0]), 16)

if len(prefix) > 0 {
return prefix[0] + code
}

return code
}

// Decode a string, convert unicode to emoji chat
//
// Usage:
// str := Decode("a msg [\u1f496]")
//
// str := Decode("a msg [\u1f496]")
func Decode(s string) string {
// emoji表情的数据表达式
re := regexp.MustCompile("\\[[\\\\u0-9a-zA-Z]+\\]")
// 提取emoji数据表达式
reg := regexp.MustCompile("\\[\\\\u|]")
src := re.FindAllString(s, -1)

for i := 0; i < len(src); i++ {
e := reg.ReplaceAllString(src[i], "")
p, err := strconv.ParseInt(e, 16, 32)
Expand All @@ -123,17 +130,15 @@ func Decode(s string) string {

// Encode a string, convert emoji chat to unicode string
func Encode(s string) string {
rs := []rune(s)
buf := new(bytes.Buffer)

for _, r := range rs {
var sb strings.Builder
for _, r := range []rune(s) {
if len(string(r)) == 4 { // is unicode emoji char
code := strconv.FormatInt(int64(r), 16)
buf.WriteString(`[\u` + code + `]`)
sb.WriteString(`[\u` + code + `]`)
} else {
buf.WriteRune(r)
sb.WriteRune(r)
}
}

return buf.String()
return sb.String()
}
6 changes: 4 additions & 2 deletions show/emoji/some-record.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ wget -qO- https://unicode.org/Public/emoji/11.0/emoji-test.txt | cut -f 1 -d ' '

## data links:

- https://www.unicode.org/Public/emoji/11.0/
- https://unicode.org/Public/emoji/15.0/emoji-sequences.txt
- https://raw.githubusercontent.com/github/gemoji/master/db/emoji.json
- https://raw.githubusercontent.com/muan/emoji/gh-pages/javascripts/emojilib/emojis.json
- https://github.com/muan/emojilib/blob/main/dist/emoji-en-US.json
- https://github.com/muan/unicode-emoji-json/blob/main/data-by-emoji.json
- https://www.unicode.org/Public/emoji/11.0/

## sites

Expand Down

0 comments on commit 67c2b18

Please sign in to comment.