Skip to content

Instantly share code, notes, and snippets.

@ivarref
Created August 5, 2024 19:38
Show Gist options
  • Save ivarref/e1dffed37e1105745cb0d7929352b98b to your computer and use it in GitHub Desktop.
Save ivarref/e1dffed37e1105745cb0d7929352b98b to your computer and use it in GitHub Desktop.
working intellij progress bar in terminal
import time
import os
from datetime import datetime
columns, lines = os.get_terminal_size()
def write(s):
print(s, end="")
# time.sleep(0.02)
def hide_cursor():
print("\033[?25l", end='', flush=True)
# Function to show the cursor
def show_cursor():
print("\033[?25h", end='', flush=True)
write("\n") # Ensure the last line is available.
write("\0337") # Save cursor position
write(f"\033[0;{lines-1}r") # Reserve the bottom line
write("\0338") # Restore the cursor position
write("\033[1A") # Move up one line
try:
for i in range(255):
time.sleep(0.1)
write(f"Hello {i}\n")
write("\0337") # Save cursor position
# hide_cursor()
write(f"\033[0;{lines}r\033[{lines};0f")
# unreserve bottom line
# Move cursor to the bottom margin
write(datetime.now().isoformat()) # Write the date
write(f"\033[0;{lines-1}r") # add margin again (intellij bug)
write("\0338") # Restore cursor position
# show_cursor()
# write("\n")
except KeyboardInterrupt:
pass
finally:
write("\0337") # Save cursor position
write(f"\033[0;{lines}r") # Drop margin reservation
write(f"\033[{lines};0f") # Move the cursor to the bottom line
write("\033[0K") # Clean that line
write("\0338") # Restore cursor position
# show_cursor()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment