Skip to content

Commit b92cc23

Browse files
author
Polle Vanhoof
committed
Add better support for changing terminal sizes
1 parent c2c5a70 commit b92cc23

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

python_progress_bar/progress_bar.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import curses
44
import signal
5+
import os
56

67
# Usage:
78
# import progress_bar <- Import this module
@@ -29,16 +30,31 @@
2930
TRAPPING_ENABLED = False
3031
TRAP_SET = False
3132
original_sigint_handler = None
33+
CURRENT_NR_LINES = 0
34+
35+
def get_current_nr_lines():
36+
stream = os.popen('tput lines')
37+
output = stream.read()
38+
return int(output)
39+
40+
41+
def get_current_nr_cols():
42+
stream = os.popen('tput cols')
43+
output = stream.read()
44+
return int(output)
45+
3246

3347
def setup_scroll_area():
48+
global CURRENT_NR_LINES
3449
# Setup curses support (to get information about the terminal we are running in)
3550
curses.setupterm()
3651

3752
# If trapping is enabled, we will want to activate it whenever we setup the scroll area and remove it when we break the scroll area
3853
if TRAPPING_ENABLED:
3954
__trap_on_interrupt()
4055

41-
lines = curses.tigetnum("lines") - 1
56+
CURRENT_NR_LINES = get_current_nr_lines()
57+
lines = CURRENT_NR_LINES - 1
4258
# Scroll down a bit to avoid visual glitch when the screen area shrinks by one row
4359
__print_control_code("\n")
4460

@@ -56,7 +72,7 @@ def setup_scroll_area():
5672

5773

5874
def destroy_scroll_area():
59-
lines = curses.tigetnum("lines")
75+
lines = get_current_nr_lines()
6076
# Save cursor
6177
__print_control_code(CODE_SAVE_CURSOR)
6278
# Set scroll region (this will place the cursor in the top left)
@@ -79,7 +95,12 @@ def destroy_scroll_area():
7995

8096
def draw_progress_bar(percentage):
8197
global PROGRESS_BLOCKED
82-
lines = curses.tigetnum("lines")
98+
global CURRENT_NR_LINES
99+
lines = get_current_nr_lines()
100+
101+
if lines != CURRENT_NR_LINES:
102+
setup_scroll_area()
103+
83104
# Save cursor
84105
__print_control_code(CODE_SAVE_CURSOR)
85106

@@ -99,7 +120,7 @@ def draw_progress_bar(percentage):
99120

100121
def block_progress_bar(percentage):
101122
global PROGRESS_BLOCKED
102-
lines = curses.tigetnum("lines")
123+
lines = get_current_nr_lines()
103124
# Save cursor
104125
__print_control_code(CODE_SAVE_CURSOR)
105126

@@ -118,7 +139,7 @@ def block_progress_bar(percentage):
118139

119140

120141
def __clear_progress_bar():
121-
lines = curses.tigetnum("lines")
142+
lines = get_current_nr_lines()
122143
# Save cursor
123144
__print_control_code(CODE_SAVE_CURSOR)
124145

@@ -133,7 +154,7 @@ def __clear_progress_bar():
133154

134155

135156
def __print_bar_text(percentage):
136-
cols = curses.tigetnum("cols")
157+
cols = get_current_nr_cols()
137158
bar_size = cols - 17
138159

139160
color = f"{COLOR_FG}{COLOR_BG}"

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
setup(
44
name = 'python_progress_bar',
55
packages = ['python_progress_bar'],
6-
version = '1.03',
6+
version = '1.10',
77
license='MIT',
88
description = 'A progress bar for python shell scripts (Linux)',
99
author = 'Polle Vanhoof',
1010
author_email = 'vanhoofpolle@gmail.com',
1111
long_description= 'Please refer to the full description on github',
1212
url = 'https://github.com/pollev/python_progress_bar',
13-
download_url = 'https://github.com/pollev/python_progress_bar/archive/v1.03.tar.gz',
13+
download_url = 'https://github.com/pollev/python_progress_bar/archive/v1.10.tar.gz',
1414
keywords = ['progress', 'bar', 'indicator'],
1515
install_requires=[
1616
],

0 commit comments

Comments
 (0)