python-3.xpynputansi-escapewindows-terminal

Changing Terminal font size using script?


I am currently making a terminal app, and wanted to allow user to change zoom of a windows 11 terminal. Is there a way to change Font size or any other way?

As I didn't find anything to do it, and I tried pynput to do it "manually", and it didn't work. I looked for ways to change font size without having the user making it manually. I also tried ANSI escape codes, but didn't work either. (Please don't mind if there are errors in the following scripts, as I deleted all past attemps)

from pynput.keyboard import Controller, Key
    keyboard = Controller()
    while True:
        x,y = get_terminal_size()
        print(f"Actual setting : {x}x{y}")
        ch = input("Increase or decrease quality (+/-)(enter to exit) : ")
        if ch == "+":
            with keyboard.pressed(Key.ctrl):
                Press(KEYPAD_MINUS)
        elif ch == "-":
            with keyboard.pressed(Key.ctrl):
                Press(KEYPAD_PLUS)
        elif ch == "":
            break
        else:
            print("")
from pynput.keyboard import Controller, Key
    keyboard = Controller()
    while True:
        x,y = get_terminal_size()
        print(f"Actual setting : {x}x{y}")
        ch = input("Increase or decrease quality (+/-)(enter to exit) : ")
        if ch == "+":
            size += 10
            print(f"\033[{size}m", end='')
        elif ch == "-":
            size -= 10
            print(f"\033[{size}m", end='')
        elif ch == "":
            break
        else:
            print("")```

Solution

  • The following code worked for me:

    from pynput.keyboard import Controller, Key
    keyboard = Controller()
    while True:
        x,y = get_terminal_size()
        print(f"Actual setting : {x}x{y}")
        ch = input("Increase or decrease quality (+/-)(enter to exit) : ")
        if ch == "+":
            with keyboard.pressed(Key.ctrl):
                keyboard.press('+')
            print('increased')
        elif ch == "-":
            with keyboard.pressed(Key.ctrl):
                keyboard.press('-')
            print('decreased')
        elif ch == "":
            break
        else:
            print("unknown command")
    

    Compared to the code from the question it looks important that the code above uses keyboard.press('+') not KEYPAD_MINUS.

    Here is a session in the terminal:

    (venv_pynput) PS ...\venv_pynput> python .\my_scripts\so_79020368.py
    Actual setting : 144x38
    Increase or decrease quality (+/-)(enter to exit) : +
    increased
    Actual setting : 144x38
    Increase or decrease quality (+/-)(enter to exit) : +
    increased
    Actual setting : 134x35
    Increase or decrease quality (+/-)(enter to exit) : -
    decreased
    Actual setting : 125x33
    Increase or decrease quality (+/-)(enter to exit) : -
    decreased
    Actual setting : 134x35
    Increase or decrease quality (+/-)(enter to exit) :
    (venv_pynput) PS ...\venv_pynput>