Patchwork [5,of,5] templatekw: add keyword to pick a random emoji character

login
register
mail settings
Submitter Yuya Nishihara
Date April 1, 2015, 1:25 p.m.
Message ID <e934f1a41e10522c0533.1427894754@mimosa>
Download mbox | patch
Permalink /patch/8414/
State Accepted
Headers show

Comments

Yuya Nishihara - April 1, 2015, 1:25 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1427814041 -32400
#      Wed Apr 01 00:00:41 2015 +0900
# Node ID e934f1a41e10522c05336767cb066ae8b351df9e
# Parent  98290eaf4e6bf993f5e351bd8f954b8f6e99479a
templatekw: add keyword to pick a random emoji character.

This allows us to automate the insertion of a fancy emoji in patch subject.
Currently it does not support Pokemon names nor Hodor quotes.

  [patchbomb]
  flagtemplate = {fortune}
Matt Mackall - April 1, 2015, 10:19 p.m.
On Wed, 2015-04-01 at 22:25 +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1427814041 -32400
> #      Wed Apr 01 00:00:41 2015 +0900
> # Node ID e934f1a41e10522c05336767cb066ae8b351df9e
> # Parent  98290eaf4e6bf993f5e351bd8f954b8f6e99479a
> templatekw: add keyword to pick a random emoji character.
> 
> This allows us to automate the insertion of a fancy emoji in patch subject.
> Currently it does not support Pokemon names nor Hodor quotes.
> 
>   [patchbomb]
>   flagtemplate = {fortune}
> 
> diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
> --- a/mercurial/templatekw.py
> +++ b/mercurial/templatekw.py
> @@ -5,8 +5,9 @@
>  # This software may be used and distributed according to the terms of the
>  # GNU General Public License version 2 or any later version.
>  
> +import random
>  from node import hex
> -import patch, util, error
> +import patch, util, error, encoding
>  import hbisect
>  
>  # This helper class allows us to handle both:
> @@ -321,6 +322,24 @@ def showfiles(**args):
>      """
>      return showlist('file', args['ctx'].files(), **args)
>  
> +_emojicodepoints = []
> +
> +def showfortune(**args):
> +    """:fortune: String. A random emoji character."""
> +    if not _emojicodepoints:
> +        # miscellaneous symbols and pictographs [1f300-1f5ff]:
> +        _emojicodepoints.extend(xrange(0x1f300, 0x1f32d))
> +        _emojicodepoints.extend(xrange(0x1f330, 0x1f37e))
> +        _emojicodepoints.extend(xrange(0x1f380, 0x1f3cf))
> +        _emojicodepoints.extend(xrange(0x1f3d4, 0x1f3f8))
> +        _emojicodepoints.extend(xrange(0x1f400, 0x1f4ff))
> +        _emojicodepoints.extend(xrange(0x1f500, 0x1f54b))
> +        _emojicodepoints.extend(xrange(0x1f550, 0x1f57a))
> +        _emojicodepoints.extend(xrange(0x1f57b, 0x1f5a4))
> +        _emojicodepoints.extend(xrange(0x1f5a5, 0x1f600))
> +    c = random.choice(_emojicodepoints)
> +    return encoding.tolocal(unichr(c).encode('utf-8'))

Certainly the knowledge of what emoji exists belongs in encoding.py,
right?

Templates are sadly lacking an array subscript or substring method and a
random() func. If they existed, this could simply be an emoji = line
under [templates].

Patch

diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py
--- a/mercurial/templatekw.py
+++ b/mercurial/templatekw.py
@@ -5,8 +5,9 @@ 
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
+import random
 from node import hex
-import patch, util, error
+import patch, util, error, encoding
 import hbisect
 
 # This helper class allows us to handle both:
@@ -321,6 +322,24 @@  def showfiles(**args):
     """
     return showlist('file', args['ctx'].files(), **args)
 
+_emojicodepoints = []
+
+def showfortune(**args):
+    """:fortune: String. A random emoji character."""
+    if not _emojicodepoints:
+        # miscellaneous symbols and pictographs [1f300-1f5ff]:
+        _emojicodepoints.extend(xrange(0x1f300, 0x1f32d))
+        _emojicodepoints.extend(xrange(0x1f330, 0x1f37e))
+        _emojicodepoints.extend(xrange(0x1f380, 0x1f3cf))
+        _emojicodepoints.extend(xrange(0x1f3d4, 0x1f3f8))
+        _emojicodepoints.extend(xrange(0x1f400, 0x1f4ff))
+        _emojicodepoints.extend(xrange(0x1f500, 0x1f54b))
+        _emojicodepoints.extend(xrange(0x1f550, 0x1f57a))
+        _emojicodepoints.extend(xrange(0x1f57b, 0x1f5a4))
+        _emojicodepoints.extend(xrange(0x1f5a5, 0x1f600))
+    c = random.choice(_emojicodepoints)
+    return encoding.tolocal(unichr(c).encode('utf-8'))
+
 def showlatesttag(repo, ctx, templ, cache, **args):
     """:latesttag: String. Most recent global tag in the ancestors of this
     changeset.
@@ -435,6 +454,7 @@  keywords = {
     'file_dels': showfiledels,
     'file_mods': showfilemods,
     'files': showfiles,
+    'fortune': showfortune,
     'latesttag': showlatesttag,
     'latesttagdistance': showlatesttagdistance,
     'manifest': showmanifest,