I'm having an interesting time trying to get my Pebble watch to honor the escape sequence character \t
when pushing data to my watch (using SimplyJS).
The following snippet is the code that I have been using:
simply.scrollable(true);
simply.style('small');
simply.fullscreen(true);
var aflLadderUrl = 'http://www.sportal.com.au/feeds/sss/afl_ladder.json';
var ladderContent = '';
ajax({ url: aflLadderUrl, type: 'json'},
function(data) {
var i = 0;
while (i < 18){
ladderContent = ladderContent + data.ladder[i].friendly_name + '\t\t\t' + data.ladder[i].percentage + '\n';
i++;
}
simply.text({
title: 'AFL Ladder',
body: ladderContent
});
},
function() {
simply.text({
title: 'AFL Ladder',
body: 'No internet connection'
});
}
);
What I am currently observing is that the \n
is being honored and I can see that on my watch, each row of data is being displayed on a separate line. However, it appears that my \t
are being ignored and instead of a tab being inserted into my line, zero whitespace is being displayed (i.e. 'my name is' + '\t\t\t' + 'dave'
is displayed as my name isdave
).
I have also tried compiling a Hello World program using just the Pebble SDK (taking the code from https://github.com/kristofvc/pebble-hello-world/blob/master/src/pebble_hello_world.c and adding a couple of \t\t
in the string to be printed on line 11) and have also noticed that the SDK honors \n
characters but not \t
characters (just as my SimplyJS app did).
My question is: is it possible to have the Pebble (either via the SDK or SimplyJS) display tabs the same way you'd expect them to work when printing to console? I understand the \t character may not be supported and instead of using \t
I could just use spaces, but this one had me curious.
Please let me know if you require any more information.
Thanks in advance!
Because \t
is a control (non-printable) character, it doesn't correspond to any glyph and won't get "displayed" consistently, if at all. Terminal emulators will interpret it differently than spreadsheet software reading a TSV file, for example. It sounds like the part of the Pebble's firmware that converts a bytestring to pixels on a display just ignores \t
, although I couldn't find this in the SDK documentation.
If you're using a fixed-width font, you can implement your own tabs using spaces, like this:
var data = {ladder: [
{friendly_name: "Cool Team", percentage: 80.0},
{friendly_name: "Really Cool Team", percentage: 80.0},
{friendly_name: "The Coolest Team of All", percentage: 80.0}
]}
var len = function(obj) {return obj.friendly_name.length}
var longer = function(a, b) {return len(a) > len(b) ? a : b}
var longestName = len(data.ladder.reduce(longer))
var tab = function(obj) { return new Array(longestName - len(obj) + 2).join(" ") }
var print = function(obj) { return obj.friendly_name + tab(obj) + obj.percentage }
var ladderContent = data.ladder.map(print).join("\n")
// Cool Team 80
// Really Cool Team 80
// The Coolest Team of All 80