Last active
August 8, 2024 15:13
-
-
Save sekika/0b6094335d5c32dcbe04edbb369b76ad to your computer and use it in GitHub Desktop.
print_long - Print long text with pypager
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
# print_long - Print long text with pypager | |
# | |
# Requirement: pypager package | |
# | |
# This script provides a function to display long text in a paginated manner using the pypager package. | |
# If the text fits within a single page, it will be printed directly to the console. If the text spans | |
# multiple pages, it will be displayed using pypager to allow for easy navigation through the content. | |
# | |
# Usage: | |
# 1. Ensure you have pypager installed: | |
# pip install pypager | |
# | |
# 2. Import the function and use it with your text: | |
# from your_script import print_long | |
# print_long(text) | |
# | |
# 3. Example: | |
# long_text = "Your long text here..." | |
# print_long(long_text) | |
# | |
# The function handles text wrapping based on terminal width and calculates the number of pages required | |
# to display the text. If more than one page is needed, it invokes pypager for a better reading experience. | |
# | |
# Author: Katsutoshi Seki | |
# License: MIT License | |
# URL: https://gist.github.com/sekika/0b6094335d5c32dcbe04edbb369b76ad | |
import shutil | |
import unicodedata | |
import pypager | |
DEFAULT_TERMINAL_SIZE = (80, 20) | |
def print_long(text): | |
terminal_size = shutil.get_terminal_size(DEFAULT_TERMINAL_SIZE) | |
lines_per_page = terminal_size.lines - 1 | |
terminal_width = terminal_size.columns | |
wrapped_lines = [] | |
for line in text.split('\n'): | |
wrapped_lines.extend(wrap_text(line, terminal_width)) | |
total_lines = len(wrapped_lines) | |
wrapped_text = '\n'.join(wrapped_lines) | |
if total_lines <= lines_per_page: | |
print(text) | |
else: | |
p = pypager.pager.Pager() | |
p.add_source(pypager.source.StringSource(wrapped_text)) | |
p.run() | |
def calculate_display_width(text): | |
width = 0 | |
for char in text: | |
if unicodedata.east_asian_width(char) in 'WF': | |
width += 2 | |
else: | |
width += 1 | |
return width | |
def wrap_text(text, width): | |
lines = [] | |
current_line = "" | |
current_width = 0 | |
for line in text.split('\n'): | |
if not line: | |
lines.append("") | |
continue | |
for char in line: | |
char_width = 2 if unicodedata.east_asian_width(char) in 'WF' else 1 | |
if current_width + char_width > width: | |
lines.append(current_line) | |
current_line = char | |
current_width = char_width | |
else: | |
current_line += char | |
current_width += char_width | |
if current_line: | |
lines.append(current_line) | |
current_line = "" | |
current_width = 0 | |
if current_line: | |
lines.append(current_line) | |
return lines |
print_long - pypagerを使用して長いテキストを表示する
このスクリプトは、pypagerパッケージを使用して長いテキストをページ分割表示するための関数を提供します。テキストが1ページ内に収まる場合は、コンソールに直接表示されます。テキストが複数ページにわたる場合は、pypagerを使用して簡単にナビゲートできるように表示します。
必要条件
- pypagerパッケージ
使用方法
-
pypagerをインストールしてください:
pip install pypager
-
関数をインポートし、テキストを渡して使用します:
from your_script import print_long print_long(text)
-
使用例:
long_text = "ここに長いテキストを入力..." print_long(long_text)
この関数は、端末の幅に基づいてテキストの折り返しを処理し、テキストを表示するのに必要なページ数を計算します。ページ数が1ページ以内であれば直接printで表示し、複数ページになる場合はpypagerを使用して表示します。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
print_long - Print long text with pypager
This script provides a function to display long text in a paginated manner using the pypager package.
If the text fits within a single page, it will be printed directly to the console. If the text spans
multiple pages, it will be displayed using pypager to allow for easy navigation through the content.
Requirement
Usage
Ensure you have pypager installed:
Import the function and use it with your text:
Example:
The function handles text wrapping based on terminal width and calculates the number of pages required
to display the text. If more than one page is needed, it invokes pypager for a better reading experience.