forked from erikrose/blessings
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.17.2: add hyperlinks support and revert setupterm(os.devnull) (#147)
- move "styles" section from colors.rst to terminal.rst - add term.link("https://example.com", "example.com") hyperlink support - add bin/cnn.py news example - revert this setupterm() experiment #59, discussed in #146 - ignore ValueError for multiprocessing environments #146
- Loading branch information
Showing
12 changed files
with
253 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Basic example of hyperlinks -- show CNN news site with clickable URL's.""" | ||
# std imports | ||
import random | ||
|
||
# 3rd party | ||
import requests | ||
|
||
# local | ||
# 3rd-party | ||
from bs4 import BeautifulSoup | ||
# local imports | ||
from blessed import Terminal | ||
|
||
|
||
def embolden(phrase): | ||
# bold some phrases | ||
return phrase.isdigit() or phrase[:1].isupper() | ||
|
||
|
||
def make_bold(term, text): | ||
# embolden text | ||
return ' '.join(term.bold(phrase) if embolden(phrase) else phrase | ||
for phrase in text.split(' ')) | ||
|
||
|
||
def whitespace_only(term, line): | ||
# return only left-hand whitespace of `line'. | ||
return line[:term.length(line) - term.length(line.lstrip())] | ||
|
||
|
||
def find_articles(soup): | ||
return (a_link for a_link in soup.find_all('a') if '/article' in a_link.get('href')) | ||
|
||
|
||
def main(): | ||
term = Terminal() | ||
cnn_url = 'https://lite.cnn.io' | ||
soup = BeautifulSoup(requests.get(cnn_url).content, 'html.parser') | ||
textwrap_kwargs = { | ||
'width': term.width - (term.width // 4), | ||
'initial_indent': ' ' * (term.width // 6) + '* ', | ||
'subsequent_indent': (' ' * (term.width // 6)) + ' ' * 2, | ||
} | ||
for a_href in find_articles(soup): | ||
url_id = int(random.randrange(0, 1 << 24)) | ||
for line in term.wrap(make_bold(term, a_href.text), **textwrap_kwargs): | ||
print(whitespace_only(term, line), end='') | ||
print(term.link(cnn_url + a_href.get('href'), line.lstrip(), url_id)) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.