# HG changeset patch # User Martin von Zweigbergk # Date 1648163352 25200 # Node ID 51aed118f9dc34c391e693fee59f85acd5a91db8 # Parent 1138674ecdb8f9896f93f7ae8b6859a437169195 templates: extract function to `stringutil` for getting first line of text It's surprisingly hard to get the first line from a string, so let's have our own function in `stringutil` for it. Differential Revision: https://phab.mercurial-scm.org/D12404 diff -r 1138674ecdb8 -r 51aed118f9dc mercurial/templatefilters.py --- a/mercurial/templatefilters.py Thu Mar 24 15:41:29 2022 -0700 +++ b/mercurial/templatefilters.py Thu Mar 24 16:09:12 2022 -0700 @@ -268,10 +268,7 @@ @templatefilter(b'firstline', intype=bytes) def firstline(text): """Any text. Returns the first line of text.""" - try: - return text.splitlines()[0] - except IndexError: - return b'' + return stringutil.firstline(text) @templatefilter(b'hex', intype=bytes) diff -r 1138674ecdb8 -r 51aed118f9dc mercurial/utils/stringutil.py --- a/mercurial/utils/stringutil.py Thu Mar 24 15:41:29 2022 -0700 +++ b/mercurial/utils/stringutil.py Thu Mar 24 16:09:12 2022 -0700 @@ -685,6 +685,14 @@ return _correctauthorformat.match(author) is not None +def firstline(text): + """Return the first line of the input""" + try: + return text.splitlines()[0] + except IndexError: + return b'' + + def ellipsis(text, maxlength=400): """Trim string to at most maxlength (default: 400) columns in display.""" return encoding.trim(text, maxlength, ellipsis=b'...')