Skip to content

Commit

Permalink
Adds link method for adding hyperlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Jun 5, 2018
1 parent e19cbb7 commit abada74
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dist
tags
virtualenv
build
.tox

# PyCharm
.idea
.idea
12 changes: 12 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ Available formatting constants are::
``Style.RESET_ALL`` resets foreground, background, and brightness. Colorama will
perform this reset automatically on program exit.

Links
-----

Links are a new (2017) ANSI extension that mimics the HTML <a> blocks. You should
be using it with care as many terminals are not supporting it yet.

.. code-block:: python
from colorama import init, link
init()
print(link('http://example.com', 'click me'))
Cursor Positioning
------------------
Expand Down
2 changes: 1 addition & 1 deletion colorama/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
from .initialise import init, deinit, reinit, colorama_text
from .ansi import Fore, Back, Style, Cursor
from .ansi import Fore, Back, Style, Cursor, link
from .ansitowin32 import AnsiToWin32

__version__ = '0.3.9'
Expand Down
2 changes: 2 additions & 0 deletions colorama/ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def clear_screen(mode=2):
def clear_line(mode=2):
return CSI + str(mode) + 'K'

def link(url, text):
return OSC + '8;;' + url + '\a' + text + OSC + '8;;\a'

class AnsiCodes(object):
def __init__(self):
Expand Down
7 changes: 5 additions & 2 deletions colorama/tests/ansi_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
import sys
from unittest import TestCase, main

from ..ansi import Back, Fore, Style
from ..ansi import Back, Fore, Style, link
from ..ansitowin32 import AnsiToWin32

stdout_orig = sys.stdout
stderr_orig = sys.stderr


class AnsiTest(TestCase):

def setUp(self):
# sanity check: stdout should be a file or StringIO object.
# It will only be AnsiToWin32 if init() has previously wrapped it
Expand Down Expand Up @@ -71,6 +71,9 @@ def testStyleAttributes(self):
self.assertEqual(Style.NORMAL, '\033[22m')
self.assertEqual(Style.BRIGHT, '\033[1m')

def testLink(self):
output = link('http://example.com','This is a link')
self.assertEqual(output, '\033]8;;http://example.com\aThis is a link\033]8;;\a')

if __name__ == '__main__':
main()
16 changes: 16 additions & 0 deletions demos/demo09-hyperlinks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import print_function
import fixpath
from colorama import colorama_text, Fore, link


def main():
"""automatically reset stdout"""
with colorama_text():
print(Fore.GREEN + 'text is green')
print(Fore.RESET + 'text is back to normal')
print('This ' + link('https://google.com', 'link') + ' should be clickable')

print('text is back to stdout')

if __name__ == '__main__':
main()

0 comments on commit abada74

Please sign in to comment.