# -*- coding: utf-8 -*-
"""
Display the amount of windows in your i3 scratchpad.

Configuration parameters:
    cache_timeout: How often we refresh this module in seconds (default 5)
    format: Format of indicator. {} replaces with count of windows
        (default '{} ⌫')
    hide_when_none: Hide indicator when there is no windows (default False)


@author shadowprince
@license Eclipse Public License
"""

import i3


def find_scratch(tree):
    if tree["name"] == "__i3_scratch":
        return tree
    else:
        for x in tree["nodes"]:
            result = find_scratch(x)
            if result:
                return result
        return None


class Py3status:
    """
    """
    # available configuration parameters
    cache_timeout = 5
    format = u"{} ⌫"
    hide_when_none = False

    def __init__(self):
        self.count = -1

    def scratchpad_counter(self):
        count = len(find_scratch(i3.get_tree()).get("floating_nodes", []))

        if self.count != count:
            transformed = True
            self.count = count
        else:
            transformed = False

        response = {
            'cached_until': self.py3.time_in(self.cache_timeout),
            'transformed': transformed
        }
        if self.hide_when_none and count == 0:
            response['full_text'] = ''
        else:
            response['full_text'] = self.format.format(count)

        return response


if __name__ == "__main__":
    """
    Run module in test mode.
    """
    from py3status.module_test import module_test
    module_test(Py3status)

# -*- coding: utf-8 -*-
"""
Display the amount of windows in your i3 scratchpad.

Configuration parameters:
    cache_timeout: How often we refresh this module in seconds (default 5)
    format: string to print when one or more window (default '{counter} ⌫')
    format_none: string to print when no window (default '{counter} ⌫')
    hide_when_none: old setting. use format_none instead (default False)

Format placeholders:
    {counter} number of scratchpad windows

@author shadowprince
@license Eclipse Public License
"""

import i3


def find_scratch(tree):
    if tree["name"] == "__i3_scratch":
        return tree
    else:
        for x in tree["nodes"]:
            result = find_scratch(x)
            if result:
                return result
        return None


class Py3status:
    """
    """
    # available configuration parameters
    cache_timeout = 5
    format = u"{counter} ⌫"
    format_none = u"{counter} ⌫"
    hide_when_none = False

    def __init__(self):
        self.count = -1

    def scratchpad_counter(self):
        count = len(find_scratch(i3.get_tree()).get("floating_nodes", []))

        if self.count != count:
            transformed = True
            self.count = count
        else:
            transformed = False

        response = {
            'cached_until': self.py3.time_in(self.cache_timeout),
            'full_text': self.py3.safe_format(self.format_none, {'counter': count}),
            'transformed': transformed
        }

        if count > 0:
            response['full_text'] = self.py3.safe_format(self.format, {'counter': count})

        # backward compatible (1/11/17)
        if self.hide_when_none and count == 0:
            response['full_text'] = ''

        return response


if __name__ == "__main__":
    """
    Run module in test mode.
    """
    from py3status.module_test import module_test
    module_test(Py3status)

Add format and backward compatible-ish