# -*- coding: utf-8 -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8

"""Des fonctions pour visionner des fichiers.
"""

__all__ = ('page_file',)

from .base import isseq
from .uio import _s
from .env import get_pager, get_pager_options
from .args import split_args
from .paths import in_PATH
from .procs import spawn

PAGER_CAPS = {
    'less': ('-S',),
    }

def get_default_pagers():
    return ('less', 'more', 'pg')

def get_pager_caps():
    """Obtenir les caractéristiques du pager pour utools

    @return (pager, options)
    """
    options = None

    pager = get_pager()
    if pager is None:
        for pager in get_default_pagers():
            if in_PATH(pager): break
        else:
            raise OSError("Unable to find a default pager. Please set UTOOLS_PAGER.")

        if PAGER_CAPS.has_key(pager):
            options = PAGER_CAPS[pager]

    if options is None:
        options = split_args(get_pager_options())
    if options is None and PAGER_CAPS.has_key(pager):
        options = PAGER_CAPS[pager]

    return pager, options

def page_file(file):
    pager, options = get_pager_caps()

    cmd = [pager]
    if options:
        if isseq(options): cmd.extend(options)
        else: cmd.append(options)
    cmd.append(file)
    spawn(*cmd)
