import curses
import json
import requests

# URL de l'API
url = "http://192.168.1.18/getData"

def fetch_data(url):
    response = requests.get(url)
    if response.status_code == 200:
        return response.json()
    else:
        return {}

def display_data(screen, data):
    screen.clear()
    
    rows, cols = screen.getmaxyx()

    # Clés et valeurs
    keys = list(data.keys())
    values = list(data.values())

    # Largeur des colonnes
    col_width = max(len(key) for key in keys) + 2  # Largeur de la colonne des clés
    col_width_value = max(len(str(value)) for value in values) + 2  # Largeur de la colonne des valeurs

    for idx, (key, value) in enumerate(data.items()):
        key_str = f"{key}:"
        value_str = str(value)
        screen.addstr(idx, 0, key_str.ljust(col_width))
        screen.addstr(idx, col_width, value_str.ljust(col_width_value))

    screen.refresh()

def main(screen):
    data = fetch_data(url)
    display_data(screen, data)
    screen.getch()

if __name__ == "__main__":
    curses.wrapper(main)

