22
33import curses
44import signal
5+ import os
56
67# Usage:
78# import progress_bar <- Import this module
2930TRAPPING_ENABLED = False
3031TRAP_SET = False
3132original_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
3347def 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
5874def 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
8096def 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
100121def 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
120141def __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
135156def __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 } "
0 commit comments