Mercurial > ~mikael > mcabber > hg
comparison mcabber/modules/xttitle/xttitle.c @ 1841:8ad982f83fd6
[xttitle] Add option xttitle_short_format
As suggested by Pavel Ulpi in issue #51:
- A title is set as soon as the module is loaded,
- The option xttitle_short_format can be set to 1 to have a very short title
Notes:
- The terminal title will only display the number of unread nuffers after
the unread list has changed (not right after the module loading),
- The title is reset to "MCabber" when the module is unloaded.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sun, 28 Mar 2010 10:56:42 +0200 |
parents | 48b265f8c5cb |
children | 5d5af91a0a69 |
comparison
equal
deleted
inserted
replaced
1840:1f456351abb6 | 1841:8ad982f83fd6 |
---|---|
1 /* | 1 /* |
2 * Module "xttitle" -- Update X terminal title | 2 * Module "xttitle" -- Update X terminal title |
3 * | 3 * |
4 * Copyright (C) 2010 Mikael Berthe <mikael@lilotux.net> | 4 * Copyright (C) 2010 Mikael Berthe <mikael@lilotux.net> |
5 * | |
6 * The option 'xttitle_short_format' can be set to 1 to use a very | |
7 * short terminal title. | |
5 * | 8 * |
6 * This module is free software; you can redistribute it and/or modify | 9 * This module is free software; you can redistribute it and/or modify |
7 * it under the terms of the GNU General Public License as published by | 10 * it under the terms of the GNU General Public License as published by |
8 * the Free Software Foundation; either version 2 of the License, or (at | 11 * the Free Software Foundation; either version 2 of the License, or (at |
9 * your option) any later version. | 12 * your option) any later version. |
47 { | 50 { |
48 guint all_unread = 0; | 51 guint all_unread = 0; |
49 guint muc_unread = 0; | 52 guint muc_unread = 0; |
50 guint muc_attention = 0; | 53 guint muc_attention = 0; |
51 guint unread; // private message count | 54 guint unread; // private message count |
55 static gchar buf[128]; | |
52 | 56 |
53 // Note: We can add "attention" string later, but it isn't used | 57 // Note: We can add "attention" string later, but it isn't used |
54 // yet in mcabber... | 58 // yet in mcabber... |
55 for ( ; args->name; args++) { | 59 for ( ; args->name; args++) { |
56 if (!g_strcmp0(args->name, "unread")) { | 60 if (!g_strcmp0(args->name, "unread")) { |
64 | 68 |
65 // Let's not count the MUC unread buffers that don't have the attention | 69 // Let's not count the MUC unread buffers that don't have the attention |
66 // flag (that is, MUC buffer that have no highlighted messages). | 70 // flag (that is, MUC buffer that have no highlighted messages). |
67 unread = all_unread - (muc_unread - muc_attention); | 71 unread = all_unread - (muc_unread - muc_attention); |
68 | 72 |
73 // TODO: let the user use a format string, instead of hard-coded defaults... | |
74 if (settings_opt_get_int("xttitle_short_format") == 1) { | |
75 // Short title message | |
76 if (!all_unread) | |
77 snprintf(buf, sizeof(buf), "MCabber"); | |
78 else if (unread == all_unread) | |
79 snprintf(buf, sizeof(buf), "MCabber (%u)", unread); | |
80 else | |
81 snprintf(buf, sizeof(buf), "MCabber (%u/%u)", unread, all_unread); | |
82 } else { | |
83 // Long title message | |
84 if (muc_unread) { | |
85 snprintf(buf, sizeof(buf), "MCabber -- %u message%c (total:%u / MUC:%u)", | |
86 unread, (unread > 1 ? 's' : ' '), all_unread, muc_unread); | |
87 } else { | |
88 if (unread) | |
89 snprintf(buf, sizeof(buf), "MCabber -- %u message%c", unread, | |
90 (unread > 1 ? 's' : ' ')); | |
91 else | |
92 snprintf(buf, sizeof(buf), "MCabber -- No message"); | |
93 } | |
94 } | |
95 | |
69 // Update the terminal title | 96 // Update the terminal title |
70 if (muc_unread) { | 97 printf("\033]0;%s\007", buf); |
71 printf("\033]0;MCabber -- %d message%c (total:%d / MUC:%d)\007", | |
72 unread, (unread > 1 ? 's' : ' '), all_unread, muc_unread); | |
73 } else { | |
74 if (unread) | |
75 printf("\033]0;MCabber -- %d message%c\007", unread, | |
76 (unread > 1 ? 's' : ' ')); | |
77 else | |
78 printf("\033]0;MCabber -- No message\007"); | |
79 } | |
80 | 98 |
81 return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS; | 99 return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
82 } | 100 } |
83 | 101 |
84 // Initialization | 102 // Initialization |
85 static void xttitle_init(void) | 103 static void xttitle_init(void) |
86 { | 104 { |
87 // Add hook handler for unread message data | 105 // Add hook handler for unread message data |
88 unread_list_hid = hk_add_handler(unread_list_hh, HOOK_UNREAD_LIST_CHANGE, | 106 unread_list_hid = hk_add_handler(unread_list_hh, HOOK_UNREAD_LIST_CHANGE, |
89 G_PRIORITY_DEFAULT_IDLE, NULL); | 107 G_PRIORITY_DEFAULT_IDLE, NULL); |
108 // Default title | |
109 printf("\033]0;MCabber\007"); | |
90 } | 110 } |
91 | 111 |
92 // Uninitialization | 112 // Uninitialization |
93 static void xttitle_uninit(void) | 113 static void xttitle_uninit(void) |
94 { | 114 { |
95 // Unregister handler | 115 // Unregister handler |
96 hk_del_handler(HOOK_UNREAD_LIST_CHANGE, unread_list_hid); | 116 hk_del_handler(HOOK_UNREAD_LIST_CHANGE, unread_list_hid); |
117 // Reset title | |
118 printf("\033]0;MCabber\007"); | |
97 } | 119 } |
98 | 120 |
99 /* vim: set et cindent cinoptions=>2\:2(0 ts=2 sw=2: For Vim users... */ | 121 /* vim: set et cindent cinoptions=>2\:2(0 ts=2 sw=2: For Vim users... */ |