# -*- coding: utf-8 -*-
"""teuk_display.py - a small WeeChat visual preset, inspired by r3m.

Inspired by Pascal Poitras (r3m):
https://gist.github.com/pascalpoitras/8406501

I would never claim to recreate the WeeChat setup that r3m has built over
the years. This little script simply lets you borrow a tiny part of that
look. Thank you for your support throughout all these years, r3m.

Designed for WeeChat 4.10.1. Requires the Python, IRC and buflist plugins.
Use a dark terminal with UTF-8 and 256-color support.

Save this file in your home directory, then run INSIDE WeeChat:
    /python load ~/teuk_display.py
    /python unload teuk_display

Applies colors, message formats and bars, then saves the configuration.
Unloading does not undo these settings. Do not put this script in autoload.
Download: https://teuk.org/downloads/weechat/teuk_display.py
"""

import weechat as w


SETTINGS = {
    # Messages : heures HHMM, pseudos alignes, separateur cyan.
    "weechat.look.buffer_time_format": "${color:245}%H${color:253}%M",
    "weechat.look.prefix_align": "right",
    "weechat.look.prefix_align_min": "0",
    "weechat.look.prefix_align_max": "10",
    "weechat.look.prefix_suffix": "│",
    "weechat.look.read_marker_string": "─",
    "weechat.look.separator_horizontal": "",
    "weechat.look.separator_vertical": "",
    "weechat.look.item_time_format": "%H:%M",
    "weechat.look.bar_more_left": "…",
    "weechat.look.bar_more_right": "…",
    "weechat.look.bar_more_up": "",
    "weechat.look.bar_more_down": "",
    "weechat.color.chat": "252",
    "weechat.color.chat_time": "245",
    "weechat.color.chat_delimiters": "31",
    "weechat.color.chat_prefix_suffix": "31",
    "weechat.color.chat_prefix_join": "121",
    "weechat.color.chat_prefix_quit": "131",
    "weechat.color.chat_prefix_network": "31",
    "weechat.color.chat_prefix_error": "lightred",
    "weechat.color.chat_read_marker": "31",
    "weechat.color.chat_nick_self": "252",
    "weechat.color.chat_nick_colors": "cyan,magenta,green,brown,lightblue,lightcyan,lightmagenta,lightgreen,blue",
    "weechat.color.chat_highlight": "lightred",
    "weechat.color.chat_highlight_bg": "default",
    "weechat.color.separator": "31",
    "weechat.color.bar_more": "229",
    # Pseudos : couleurs uniquement.
    "irc.look.color_nicks_in_nicklist": "on",
    "weechat.color.nicklist_away": "240",
    "weechat.color.nicklist_group": "31",
    # Liste des buffers : numero courant >>, branches cyan, ligne serveur bleue.
    "buflist.format.number": "${if:${current_buffer}?${color:white}>>:${color:245}${number}}${color:default} ",
    "buflist.format.indent": "${color:31}├ ${color:default}",
    "buflist.format.name": "${name}",
    "buflist.format.buffer": "${if:${type}==server?${color:253,31}${cutscr:20,,${format_number}${name}${repeat:20, }}${color:reset}:${format_number}${indent}${if:${current_buffer}?${color:white}:${color_hotlist}}${format_name}${format_hotlist}}",
    "buflist.format.buffer_current": "${format_buffer}",
    "buflist.format.hotlist": " ${color:245}(${hotlist})",
    "buflist.format.hotlist_none": "${color:252}",
    "buflist.format.hotlist_low": "${color:245}",
    "buflist.format.hotlist_message": "${color:229}",
    "buflist.format.hotlist_private": "${color:121}",
    "buflist.format.hotlist_highlight": "${color:lightred}",
    # Barre d'etat compacte.
    "weechat.color.status_time": "white",
    "weechat.color.status_number": "white",
    "weechat.color.status_name": "121",
    "weechat.color.status_name_tls": "121",
    "weechat.color.status_name_insecure": "229",
    "weechat.color.status_nicklist_count": "white",
    "weechat.color.status_data_msg": "229",
    "weechat.color.status_data_private": "121",
    "weechat.color.status_data_highlight": "lightred",
}

BAR_SETTINGS = {
    "buflist": {
        "position": "left", "size": "20", "size_max": "20",
        "separator": "on", "color_fg": "252", "color_bg": "default",
        "color_delim": "31", "hidden": "off", "priority": "2000",
        "filling_left_right": "vertical",
        "items": "teuk_version,teuk_rule,buflist",
    },
    "nicklist": {
        "position": "right", "size": "14", "size_max": "14",
        "separator": "on", "color_fg": "252", "color_bg": "default",
        "color_delim": "31", "hidden": "off",
    },
    "title": {
        "position": "top", "size": "1", "separator": "off",
        "color_fg": "white", "color_bg": "31", "color_delim": "white",
        "hidden": "off", "items": "buffer_title",
    },
    "status": {
        "position": "bottom", "size": "1", "separator": "off",
        "color_fg": "252", "color_bg": "236", "color_delim": "31",
        "hidden": "off",
        "items": "time,teuk_arrow,buffer_number+:+buffer_name,buffer_modes,teuk_arrow,buffer_nicklist_count,hotlist,completion,scroll",
    },
    "input": {
        "position": "bottom", "size": "1", "separator": "off",
        "color_fg": "252", "color_bg": "default", "color_delim": "31",
        "hidden": "off", "items": "[input_prompt],input_search,input_paste,input_text",
    },
}


def apply_display():
    missing = [name for name in SETTINGS if not w.config_get(name)]
    if missing:
        w.prnt("", w.prefix("error") + "Habillage non applique : options absentes : " + ", ".join(missing))
        return
    # La commande native cree la barre seulement si elle n'existe pas.
    w.command("", "/buflist bar")
    for bar, properties in BAR_SETTINGS.items():
        for prop, value in properties.items():
            SETTINGS["weechat.bar." + bar + "." + prop] = value
    missing = [name for name in SETTINGS if not w.config_get(name)]
    if missing:
        w.prnt("", w.prefix("error") + "Barres absentes : " + ", ".join(missing))
        return
    failures = []
    for name, value in SETTINGS.items():
        if w.config_option_set(w.config_get(name), value, 1) == w.WEECHAT_CONFIG_OPTION_SET_ERROR:
            failures.append(name)
    if failures:
        w.prnt("", w.prefix("error") + "Reglages refuses : " + ", ".join(failures))
        return
    w.command("", '/item addreplace teuk_version "" "${color:245}       ${info:version}"')
    w.command("", '/item addreplace teuk_rule "" "${color:31}${repeat:20,─}"')
    w.command("", '/item addreplace teuk_arrow "" "${color:31}❯"')
    w.command("", "/buflist refresh")
    w.command("", "/save")
    w.prnt("", "Habillage Teuk applique et sauvegarde. Tu peux faire /python unload teuk_display.")


if w.register("teuk_display", "Codex", "1.0", "MIT", "Habillage visuel pour Teuk", "", ""):
    apply_display()
