From b883a52b0953e44a20020cac090456bc6a505b80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Sun, 15 Nov 2015 21:50:29 +0100 Subject: [PATCH 1/7] Date change message prototype --- js/handlers.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/js/handlers.js b/js/handlers.js index 98f088c..25ba797 100644 --- a/js/handlers.js +++ b/js/handlers.js @@ -19,12 +19,39 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific models.closeBuffer(bufferId); }; + // inject a fake buffer line for date change + var injectDateChangeMessage = function(message, buffer, date) { + var content = "Date changed to " + date.toDateString(); + var line = { + buffer: buffer, + date: date, + prefix: '—', + tags_array: [], + displayed: true, + highlight: 0, + message: content + }; + var new_message = new models.BufferLine(line); + buffer.addLine(new_message); + }; + var handleLine = function(line, manually) { var message = new models.BufferLine(line); var buffer = models.getBuffer(message.buffer); buffer.requestedLines++; // Only react to line if its displayed if (message.displayed) { + // Check for date change + if (buffer.lines.length > 0) { + var previous_date = new Date(buffer.lines[buffer.lines.length - 1].date), + current_date = new Date(message.date); + previous_date.setHours(0, 0, 0, 0); + current_date.setHours(0, 0, 0, 0); + if (previous_date.valueOf() !== current_date.valueOf()) { + injectDateChangeMessage(message, buffer, current_date); + } + } + message = plugins.PluginManager.contentForMessage(message); buffer.addLine(message); From 0a1726e359792deb93d3e787642f46b0af5943de Mon Sep 17 00:00:00 2001 From: Patrick Melanson Date: Fri, 20 Nov 2015 17:52:39 -0500 Subject: [PATCH 2/7] date change messages are chat_day_change color, multiple messages generated for diff of 1-5 days --- js/handlers.js | 24 ++++++++++++++++++++---- js/weechat.js | 2 +- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/js/handlers.js b/js/handlers.js index 25ba797..be66ea3 100644 --- a/js/handlers.js +++ b/js/handlers.js @@ -21,11 +21,11 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific // inject a fake buffer line for date change var injectDateChangeMessage = function(message, buffer, date) { - var content = "Date changed to " + date.toDateString(); + var content = "\u001943Date changed to " + date.toDateString(); var line = { buffer: buffer, date: date, - prefix: '—', + prefix: '\u001943\u2500\u2500', tags_array: [], displayed: true, highlight: 0, @@ -47,8 +47,24 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific current_date = new Date(message.date); previous_date.setHours(0, 0, 0, 0); current_date.setHours(0, 0, 0, 0); - if (previous_date.valueOf() !== current_date.valueOf()) { - injectDateChangeMessage(message, buffer, current_date); + var dateDifference = + Math.round((current_date - previous_date)/(24*60*60*1000)); + if (dateDifference !== 0) { + console.log(dateDifference); + // if it's a small, positive number display a message + // for each one. Otherwise, just display one big + // date change message + // The range [1,5] was chosen arbitrarily + if (dateDifference >= 1 && dateDifference <= 5) { + var prev_date_clone = previous_date; + for (var i = 1; i <= dateDifference; ++i) { + prev_date_clone.setDate(prev_date_clone.getDate()+1); + injectDateChangeMessage(message, buffer, + prev_date_clone); + } + } else { + injectDateChangeMessage(message, buffer, current_date); + } } } diff --git a/js/weechat.js b/js/weechat.js index 126eda6..4a6d4f0 100644 --- a/js/weechat.js +++ b/js/weechat.js @@ -290,7 +290,7 @@ var ret = {}; var optionCode = parseInt(m[1]); - if (optionCode > 43) { + if (optionCode >= WeeChatProtocol._colorsOptionsNames.length) { // should never happen return { fgColor: null, From 9955056ed927da8922aec0f3ae4be822e9a0bfdc Mon Sep 17 00:00:00 2001 From: Patrick Melanson Date: Fri, 20 Nov 2015 22:26:56 -0500 Subject: [PATCH 3/7] dates change with weechat 2date behaviour --- js/handlers.js | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/js/handlers.js b/js/handlers.js index be66ea3..fa6f818 100644 --- a/js/handlers.js +++ b/js/handlers.js @@ -20,11 +20,26 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific }; // inject a fake buffer line for date change - var injectDateChangeMessage = function(message, buffer, date) { - var content = "\u001943Date changed to " + date.toDateString(); + var injectDateChangeMessage = function(message, buffer, d1, d2) { + var d1_plus_one = d1; + d1_plus_one.setDate(d1_plus_one.getDate() + 1); + d1.setHours(0, 0, 0, 0); + d2.setHours(0, 0, 0, 0); + // it's not always true that a date with time 00:00:00 + // plus one day will be time 00:00:00 + d1_plus_one.setHours(0, 0, 0, 0); + var content = ""; + if (d1_plus_one.valueOf() === d2.valueOf()) { + content = "\u001943Date changed to " + d2.toDateString(); + } else { + var date_diff = Math.round((d2 - d1)/(24*60*60*1000)); + date_diff = date_diff >= 0 ? date_diff : date_diff + 1; + content = "\u001943Date changed to " + d2.toDateString(); + content += " (" + date_diff + " days from " + d1.toDateString() + ")"; + } var line = { buffer: buffer, - date: date, + date: d2, prefix: '\u001943\u2500\u2500', tags_array: [], displayed: true, @@ -47,24 +62,8 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific current_date = new Date(message.date); previous_date.setHours(0, 0, 0, 0); current_date.setHours(0, 0, 0, 0); - var dateDifference = - Math.round((current_date - previous_date)/(24*60*60*1000)); - if (dateDifference !== 0) { - console.log(dateDifference); - // if it's a small, positive number display a message - // for each one. Otherwise, just display one big - // date change message - // The range [1,5] was chosen arbitrarily - if (dateDifference >= 1 && dateDifference <= 5) { - var prev_date_clone = previous_date; - for (var i = 1; i <= dateDifference; ++i) { - prev_date_clone.setDate(prev_date_clone.getDate()+1); - injectDateChangeMessage(message, buffer, - prev_date_clone); - } - } else { - injectDateChangeMessage(message, buffer, current_date); - } + if (previous_date.valueOf() !== current_date.valueOf()) { + injectDateChangeMessage(message, buffer, previous_date, current_date); } } From 5dc40cb1342cb738be3fa4b9608d804e9fff69c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Mon, 23 Nov 2015 10:59:00 +0100 Subject: [PATCH 4/7] Date change: prettier messages, code cleaning Has a weird bug: https://github.com/glowing-bear/glowing-bear/pull/677#issuecomment-158891237 --- js/handlers.js | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/js/handlers.js b/js/handlers.js index fa6f818..eab2101 100644 --- a/js/handlers.js +++ b/js/handlers.js @@ -20,26 +20,32 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific }; // inject a fake buffer line for date change - var injectDateChangeMessage = function(message, buffer, d1, d2) { - var d1_plus_one = d1; - d1_plus_one.setDate(d1_plus_one.getDate() + 1); - d1.setHours(0, 0, 0, 0); - d2.setHours(0, 0, 0, 0); + var injectDateChangeMessage = function(message, buffer, old_date, new_date) { + var old_date_plus_one = old_date; + old_date_plus_one.setDate(old_date.getDate() + 1); + + new_date.setHours(0, 0, 0, 0); + old_date.setHours(0, 0, 0, 0); // it's not always true that a date with time 00:00:00 // plus one day will be time 00:00:00 - d1_plus_one.setHours(0, 0, 0, 0); - var content = ""; - if (d1_plus_one.valueOf() === d2.valueOf()) { - content = "\u001943Date changed to " + d2.toDateString(); - } else { - var date_diff = Math.round((d2 - d1)/(24*60*60*1000)); - date_diff = date_diff >= 0 ? date_diff : date_diff + 1; - content = "\u001943Date changed to " + d2.toDateString(); - content += " (" + date_diff + " days from " + d1.toDateString() + ")"; + old_date_plus_one.setHours(0, 0, 0, 0); + + var content = "\u001943" + // styling + "Date changed to " + new_date.toDateString(); + // Comparing dates in javascript is beyond tedious + if (old_date_plus_one.valueOf() !== new_date.valueOf()) { + var date_diff = Math.round((new_date - old_date)/(24*60*60*1000)); + if (date_diff < 0) { + date_diff = -1*(date_diff + 1); + content += " (" + date_diff + " days before " + old_date.toDateString() + ")"; + } else { + content += " (" + date_diff + " days have passed since " + old_date.toDateString() + ")"; + } } + var line = { buffer: buffer, - date: d2, + date: new_date, prefix: '\u001943\u2500\u2500', tags_array: [], displayed: true, From 271a5d0626b65fbc602c791daf78f11e331e69fb Mon Sep 17 00:00:00 2001 From: Patrick Melanson Date: Fri, 27 Nov 2015 11:24:32 -0500 Subject: [PATCH 5/7] when switching buffers, will inject date change if date changed --- js/handlers.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/js/handlers.js b/js/handlers.js index eab2101..a2240de 100644 --- a/js/handlers.js +++ b/js/handlers.js @@ -20,7 +20,7 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific }; // inject a fake buffer line for date change - var injectDateChangeMessage = function(message, buffer, old_date, new_date) { + var injectDateChangeMessage = function(buffer, old_date, new_date) { var old_date_plus_one = old_date; old_date_plus_one.setDate(old_date.getDate() + 1); @@ -56,6 +56,15 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific buffer.addLine(new_message); }; + // wrapper to do the logic checking + var injectDateChangeMessageIfNeeded = function(buffer, previous_date, current_date) { + previous_date.setHours(0, 0, 0, 0); + current_date.setHours(0, 0, 0, 0); + if (previous_date.valueOf() !== current_date.valueOf()) { + injectDateChangeMessage(buffer, previous_date, current_date); + } + }; + var handleLine = function(line, manually) { var message = new models.BufferLine(line); var buffer = models.getBuffer(message.buffer); @@ -66,11 +75,7 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific if (buffer.lines.length > 0) { var previous_date = new Date(buffer.lines[buffer.lines.length - 1].date), current_date = new Date(message.date); - previous_date.setHours(0, 0, 0, 0); - current_date.setHours(0, 0, 0, 0); - if (previous_date.valueOf() !== current_date.valueOf()) { - injectDateChangeMessage(message, buffer, previous_date, current_date); - } + injectDateChangeMessageIfNeeded(buffer, previous_date, current_date); } message = plugins.PluginManager.contentForMessage(message); @@ -235,6 +240,13 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific lines.forEach(function(l) { handleLine(l, manually); }); + if (message.objects[0].content.length > 0) { + var last_line = + message.objects[0].content[message.objects[0].content.length-1]; + var last_message = new models.BufferLine(last_line); + var buffer = models.getBuffer(last_message.buffer); + injectDateChangeMessageIfNeeded(buffer, last_message.date, new Date()); + } }; /* From 9334f44cdeeb1144d4ca9c14dbf8f9917ca9b96e Mon Sep 17 00:00:00 2001 From: Patrick Melanson Date: Fri, 27 Nov 2015 14:38:18 -0500 Subject: [PATCH 6/7] more human readable date change, per #679 For one day later: Friday (November 27) (examples in en-US) For n>1 days: Friday (November 27, 2 days later) For n<0 days: Friday (November 27, 1 day before) For different years: Friday (November 27, 2015, 2156 days later) --- js/handlers.js | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/js/handlers.js b/js/handlers.js index a2240de..3541aa8 100644 --- a/js/handlers.js +++ b/js/handlers.js @@ -30,23 +30,49 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific // plus one day will be time 00:00:00 old_date_plus_one.setHours(0, 0, 0, 0); - var content = "\u001943" + // styling - "Date changed to " + new_date.toDateString(); + var content = "\u001943"; // this colour corresponds to chat_day_change + content += new_date.toLocaleDateString(window.navigator.language, + {weekday: "long"}); + // if you're testing different date formats, + // make sure to test different locales such as "en-US", + // "en-US-u-ca-persian" (which has different weekdays, year 0, and an ERA) + // "ja-JP-u-ca-persian-n-thai" (above, diff numbering, diff text) + var extra_date_format = { + day: "numeric", + month: "long" + }; + if (new_date.getYear() !== old_date.getYear()) { + extra_date_format.year = "numeric"; + } + content += " ("; + content += new_date.toLocaleDateString(window.navigator.language, + extra_date_format); + // Result should be something like + // Friday (November 27) + // or if the year is different, + // Friday (November 27, 2015) + // Comparing dates in javascript is beyond tedious if (old_date_plus_one.valueOf() !== new_date.valueOf()) { - var date_diff = Math.round((new_date - old_date)/(24*60*60*1000)); + var date_diff = Math.round((new_date - old_date)/(24*60*60*1000)) + 1; if (date_diff < 0) { - date_diff = -1*(date_diff + 1); - content += " (" + date_diff + " days before " + old_date.toDateString() + ")"; + date_diff = -1*(date_diff); + if (date_diff === 1) { + content += ", 1 day before"; + } else { + content += ", " + date_diff + " days before"; + } } else { - content += " (" + date_diff + " days have passed since " + old_date.toDateString() + ")"; + content += ", " + date_diff + " days later"; } + // Result: Friday (November 27, 5 days later) } + content += ")"; var line = { buffer: buffer, date: new_date, - prefix: '\u001943\u2500\u2500', + prefix: '\u001943\u2500', tags_array: [], displayed: true, highlight: 0, From da7a7f85e87d7328d76a6530ef5c53c1e1ebe2fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Sat, 28 Nov 2015 13:23:41 +0100 Subject: [PATCH 7/7] Date change: refactoring --- js/handlers.js | 128 +++++++++++++++++++++++-------------------------- 1 file changed, 61 insertions(+), 67 deletions(-) diff --git a/js/handlers.js b/js/handlers.js index 3541aa8..bf41604 100644 --- a/js/handlers.js +++ b/js/handlers.js @@ -19,75 +19,69 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific models.closeBuffer(bufferId); }; - // inject a fake buffer line for date change - var injectDateChangeMessage = function(buffer, old_date, new_date) { - var old_date_plus_one = old_date; - old_date_plus_one.setDate(old_date.getDate() + 1); - - new_date.setHours(0, 0, 0, 0); + // inject a fake buffer line for date change if needed + var injectDateChangeMessageIfNeeded = function(buffer, old_date, new_date) { old_date.setHours(0, 0, 0, 0); - // it's not always true that a date with time 00:00:00 - // plus one day will be time 00:00:00 - old_date_plus_one.setHours(0, 0, 0, 0); + new_date.setHours(0, 0, 0, 0); + // Check if the date changed + if (old_date.valueOf() !== new_date.valueOf()) { + var old_date_plus_one = old_date; + old_date_plus_one.setDate(old_date.getDate() + 1); + // it's not always true that a date with time 00:00:00 + // plus one day will be time 00:00:00 + old_date_plus_one.setHours(0, 0, 0, 0); - var content = "\u001943"; // this colour corresponds to chat_day_change - content += new_date.toLocaleDateString(window.navigator.language, - {weekday: "long"}); - // if you're testing different date formats, - // make sure to test different locales such as "en-US", - // "en-US-u-ca-persian" (which has different weekdays, year 0, and an ERA) - // "ja-JP-u-ca-persian-n-thai" (above, diff numbering, diff text) - var extra_date_format = { - day: "numeric", - month: "long" - }; - if (new_date.getYear() !== old_date.getYear()) { - extra_date_format.year = "numeric"; - } - content += " ("; - content += new_date.toLocaleDateString(window.navigator.language, - extra_date_format); - // Result should be something like - // Friday (November 27) - // or if the year is different, - // Friday (November 27, 2015) - - // Comparing dates in javascript is beyond tedious - if (old_date_plus_one.valueOf() !== new_date.valueOf()) { - var date_diff = Math.round((new_date - old_date)/(24*60*60*1000)) + 1; - if (date_diff < 0) { - date_diff = -1*(date_diff); - if (date_diff === 1) { - content += ", 1 day before"; - } else { - content += ", " + date_diff + " days before"; - } - } else { - content += ", " + date_diff + " days later"; + var content = "\u001943"; // this colour corresponds to chat_day_change + // Add day of the week + content += new_date.toLocaleDateString(window.navigator.language, + {weekday: "long"}); + // if you're testing different date formats, + // make sure to test different locales such as "en-US", + // "en-US-u-ca-persian" (which has different weekdays, year 0, and an ERA) + // "ja-JP-u-ca-persian-n-thai" (above, diff numbering, diff text) + var extra_date_format = { + day: "numeric", + month: "long" + }; + if (new_date.getYear() !== old_date.getYear()) { + extra_date_format.year = "numeric"; } - // Result: Friday (November 27, 5 days later) - } - content += ")"; + content += " ("; + content += new_date.toLocaleDateString(window.navigator.language, + extra_date_format); + // Result should be something like + // Friday (November 27) + // or if the year is different, + // Friday (November 27, 2015) - var line = { - buffer: buffer, - date: new_date, - prefix: '\u001943\u2500', - tags_array: [], - displayed: true, - highlight: 0, - message: content - }; - var new_message = new models.BufferLine(line); - buffer.addLine(new_message); - }; + // Comparing dates in javascript is beyond tedious + if (old_date_plus_one.valueOf() !== new_date.valueOf()) { + var date_diff = Math.round((new_date - old_date)/(24*60*60*1000)) + 1; + if (date_diff < 0) { + date_diff = -1*(date_diff); + if (date_diff === 1) { + content += ", 1 day before"; + } else { + content += ", " + date_diff + " days before"; + } + } else { + content += ", " + date_diff + " days later"; + } + // Result: Friday (November 27, 5 days later) + } + content += ")"; - // wrapper to do the logic checking - var injectDateChangeMessageIfNeeded = function(buffer, previous_date, current_date) { - previous_date.setHours(0, 0, 0, 0); - current_date.setHours(0, 0, 0, 0); - if (previous_date.valueOf() !== current_date.valueOf()) { - injectDateChangeMessage(buffer, previous_date, current_date); + var line = { + buffer: buffer, + date: new_date, + prefix: '\u001943\u2500', + tags_array: [], + displayed: true, + highlight: 0, + message: content + }; + var new_message = new models.BufferLine(line); + buffer.addLine(new_message); } }; @@ -99,9 +93,9 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific if (message.displayed) { // Check for date change if (buffer.lines.length > 0) { - var previous_date = new Date(buffer.lines[buffer.lines.length - 1].date), - current_date = new Date(message.date); - injectDateChangeMessageIfNeeded(buffer, previous_date, current_date); + var old_date = new Date(buffer.lines[buffer.lines.length - 1].date), + new_date = new Date(message.date); + injectDateChangeMessageIfNeeded(buffer, old_date, new_date); } message = plugins.PluginManager.contentForMessage(message);