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

Yuya Nishihara yuya at tcha.org
Wed Apr 1 08:25:54 CDT 2015


# HG changeset patch
# User Yuya Nishihara <yuya at 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'))
+
 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,


More information about the Mercurial-devel mailing list