def grep(haystack, needle, ln=0, color=None):
    """Highlight needle in haystack and return a few lines around it.

    Mimic UNIX's grep method. Search for a string (needle) in a text chunk
    (haystack).

    Return a list of matches. Each match will have ln number of lines
    before and after it (similar to "grep -C NUM"). The matching line will be
    highlighted with a color from a pre-defined list.

    Examples:

    >>> grep('a\nb\na1\na2\na3', 'b', 0)
    ['b']
    >>> grep('a\nb\na1\na2\na3', 'b', 1)
    ['a\nb\na1']
    >>> grep('a\nb\na1\na2\na3', 'b', 3)
    ['a\nb\na1\na2\na3']
    >>> grep('a\nb\na1\na2\na3', 'b', 3, 'RED')
    ['a\n\x1b[0;31mb\x1b[0m\na1\na2\na3']
    >>> grep('a\nb\na1\na2\na3', 'a')
    ['a', 'a1', 'a2', 'a3']
    >>> grep('a\nb\na1\na2\na3', 'a', 1)
    ['a\nb', 'b\na1\na2', 'a1\na2\na3', 'a2\na3']

    """

    #TODO: Merge two overlapping matches together.
    #TODO: Grep for more than one possible string (joined with logical OR).
    ret = []
    lines = haystack.split()
    for i, line in enumerate(lines):
        if needle in line:
            # Before
            first_possible = i-ln
            start = first_possible if first_possible >= 0 else 0
            before = '\n'.join(lines[start:i])
            # After
            last_possible = len(lines)
            end = i+1+ln if i+1+ln <= last_possible else last_possible
            after = '\n'.join(lines[i+1:end])
            # Let's join them with newlines if needed.
            txt = '%(before)s%(match)s%(after)s' % {
                'before': before + '\n' if before else '',
                'match': color_text(lines[i], color) if color else lines[i],
                'after': '\n' + after if after else ''}
            ret.append(txt)
    return ret
dataset/ETHPy150Open rvanlaar/easy-transifex/src/transifex/transifex/txcommon/tests/utils.py/grep/original