I am trying to use ansi in the menubar for color but no lock, anyone else? I just get the text as normal.
menubar:setTitle(hs.styledtext.ansi('\033[31mRed\033[0m\n'))
Lua strings don't interpret \### as octal; all ascii conversions are done in decimal (up to three digits are allowed, but if its less than 100, you don't have to use a 0 place holder).
e.g.
~lua
hs.console.printStyledtext(hs.styledtext.ansi('27[31mRed27[0mn'))
~
or
~lua
menubar:setTitle(hs.styledtext.ansi('27[31mRed27[0mn'))
~
ohhh thanks @asmagill i was just about to give up...
@asmagill is there a better way to color text? I'd like to be able to use different colors based on a hex (rgb) value.
I am able to convert hex to rgb but then not too sure how to color ansi text with that?
You're not limited to just ansi sequences -- you can create a styled text object and specify the color directly like this:
~lua
hs.console.printStyledtext(hs.styledtext.new("My text", { color = { red = .5, blue = 1, green = 0 }}))
~
ANSI conversion was added to simplify migrating the output of shell scripts which had already been written for things like GeekTool before Hammerspoon could handle things like checking memory and cpu usage itself. At the time only the official ANSI sequences were deemed necessary. While it wouldn't be too difficult to add support for the unofficial sequences (for 256 or RGB specification supported by XTerm), it's not been high on anyone's priority list since you have a lot more control if you use the hs.styledtext.new constructor.
My only issue with that is I need to have differently coloured text in the same menubar.

~lua
hs.console.printStyledtext(hs.styledtext.new({
"RedGreenNormalBlue",
{
starts = 1,
ends = 3,
attributes = { color = { red = 1 }}
}, {
starts = 4,
ends = 8,
attributes = { color = { green = 1 }}
}, {
starts = 15,
attributes = { color = { blue = 1 }}
}
}))
~
@asmagill I can't manage to get it to work consistently, in this example the unknown symbols are from another font. But I'm sure thats not the issue

Hmm, oddly its fine when just printing text...

It would seem that either the fonts character widths are oddly done, or Hammerspoon is missing something
Hammerspoon (and most of the macOS) is Unicode compliant with UTF8 (in the case of the macOS UTF8 or UTF16 depending upon context). Unicode characters outside of block 0 (i.e. those with ascii values between 0 and 127) are represented as 2 - 4 byte characters, which plays merry hell with substring positions...
If you're confident that the source string is valid UTF8, you can use utf8.offset (see a Lua reference or type help.lua.utf8.offset into the Hammerspoon console) to calculate the positions...
e.g.
~~~lua
{
starts = 1, -- will always be 1 at the start of the string, so don't bother with utf8.offset call
ends = utf8.offset(g_output, 3),
attributes = { color = { red = 1 } },
},
etc.
~~~
Check out help.lua.utf8 and help.hs.utf8 for other useful functions for working with non 7bit ascii characters.
Probably safer to do ends = utf8.offset(g_output, n) - 1 where n is the character after you want to end the attributes... this makes sure to get the position of the last part of the entire unicode encoding of the n - 1 character. For starts, you want the string position where it starts, so starts = utf8.offset(g_output, n) would be proper.
Thanks, I tried messing around with that but couldn't get it to work. I think it might just have something to do with the font (FontAwesome)
Are the arguments ends and starts in bytes then?
Sorry to bother you! The other possibility would be if I could concatenate style text , then I could color each part independently, or if setTitle() will accept multiple styledTexts
Nevermind that is documented and .. works on styledtext
Yes, they are bytes... which for 7bit ascii is the same thing as position so it usually surprises people when they first run into it with multi-byte characters.
You can concatenate multiple styled text objects into one styled text object just like you would strings (the metamethod for concat has been defined).
Most helpful comment
~luahs.console.printStyledtext(hs.styledtext.new({
"RedGreenNormalBlue",
{
starts = 1,
ends = 3,
attributes = { color = { red = 1 }}
}, {
starts = 4,
ends = 8,
attributes = { color = { green = 1 }}
}, {
starts = 15,
attributes = { color = { blue = 1 }}
}
}))
~