import json


def to_json(text, *args, **kwargs):
    """
    Creates text that, when printed (with format=json in i3blocks config),
    will set environment variables in order to save state between runs.

    Pass it text for the program to display,
    followed by (variable_name, value) pairs to set environment variables for
    the next time the script is run.
    Alternatively, pass keyword arguments to
    set corresponding environment variables.

    e.g. print(to_json("Spam!", state=state))
    """
    kwargs["full_text"] = text
    kwargs.update(dict(args))
    return json.dumps(kwargs)