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

Matt Mackall mpm at selenic.com
Wed Apr 1 17:19:36 CDT 2015


On Wed, 2015-04-01 at 22:25 +0900, Yuya Nishihara wrote:
> # 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'))

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].

-- 
Mathematics is the supreme nostalgia of our time.




More information about the Mercurial-devel mailing list