You are not logged in.

#1 2016-08-24 21:32:38

frank604
Member
From: BC, Canada
Registered: 2011-04-20
Posts: 1,212

[Solved] Adding color output to C statusbar

I recently came across slstatus github.  This is a status bar for dwm that is written in C.  I am not strong in C and wish to learn how to add colors to the output.

Adding \e[1;34m to:

/* function     format          argument */
     { cpu_perc,     "\e[1;34m \uE022 %s    ",     NULL },

gives me an output for make:

slstatus build options:
CFLAGS   = -std=c99 -pedantic -Wall -O0 -I. -I/usr/include -I/usr/X11R6/include -DVERSION="1.0" -D_GNU_SOURCE
LDFLAGS  = -L/usr/lib -lc -L/usr/X11R6/lib -lX11 -lasound
CC       = cc
CC slstatus.c
In file included from slstatus.c:67:0:
config.h:48:18: warning: non-ISO-standard escape sequence, '\e'
  { cpu_perc,     "\e[1;34m \uE022 %s    ",     NULL },
                  ^~~~~~~~~~~~~~~~~~~~~~~~
CC -o slstatus

I looked up the error about the escape sequence and found this where it says:

Use \033 instead. \e is a GNU shortcut.

However, make builds without error but the colors don't change and I see in text [1;34m piped out. 

Similarily, I've tried different escape sequences but they just pipe out as text.  What did I do wrong?  Any help appreciated.

Edit: perhaps this is an incorrect way to go about adding color.  Would love to hear some suggestions.


Solution:  Instead of the above escape codes, in VIM, using Cntrl+V then X02, produces the proper code to pipe out color.  My new issue is that the dwm base I am using is unable to pipe color even with my bash script that pipes colors as intended.  I will look to investigate further to get the results I'm looking for.  The purpose of this thread is satisfied.  Thank you Trilby.

Last edited by frank604 (2016-08-25 03:31:42)

Offline

#2 2016-08-24 22:18:31

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [Solved] Adding color output to C statusbar

Those are terminal color codes.  Terminals interpret them and change the color of the text printed.  DWM does not do this with the text of the root window name.

DWM can do this if you have the statuscolors patch, but then you need to use color codes it recognizes, not those used in a terminal.  For an example, see here:
https://github.com/TrilbyWhite/dwmStatu … r/status.c

Also read the statuscolors documentation (if you opt for that patch):
http://dwm.suckless.org/patches/statuscolors


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2016-08-24 23:19:24

frank604
Member
From: BC, Canada
Registered: 2011-04-20
Posts: 1,212

Re: [Solved] Adding color output to C statusbar

@Trilby,  Thank you.  I was kinda hoping you or Unia would see this thread.  Ah, that makes more sense with why it was failing.  I imagined with a C coded statusbar that we would have to use terminal ansi escapes which I see is wrong now.

In my bash script for statusbar I used \x01...\x04 and created an alias to color0...color4 which worked well.
Looking at your status.c I see the ^B, which I assume was entered in by pressing Cntrl+V and x01. 
However, I did the same using Cntrl+V and x+0+1 to get an output of ^B.  Yet, a unknown glyph appears instead of a color output.

Is my thinking going down a wrong path?  Or am I just missing the obvious? hmm

Offline

#4 2016-08-24 23:24:15

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [Solved] Adding color output to C statusbar

Are you using the statuscolors patch?  EDIT: oops, sorry, it works with bash, so I guess that's a yes ... standby

Yes, 'Ctrl-V x 0 1' in vim to enter the equivalent of \x01, but this just resets to the first color.  'Ctrl-V x 0 2' for the second color, etc.  Can you show the code you are using?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2016-08-24 23:43:34

frank604
Member
From: BC, Canada
Registered: 2011-04-20
Posts: 1,212

Re: [Solved] Adding color output to C statusbar

Sorry, I forgot to mention the statuscolors was patched in.  Your guess is on the mark. smile
The code I'm working on is here
I'm mostly editing the config.h to make these changes.  I haven't pushed any commits of my attempts at adding color though.  Only thing I've done after forking is adding icon glyphs in lieu of text, "wifi", "batt", etc.

Yes, x01 is the first color however I get an unknown glyph piped out hmm

Offline

#6 2016-08-25 00:38:16

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [Solved] Adding color output to C statusbar

frank604 wrote:

I haven't pushed any commits of my attempts at adding color though.

Then post them here.  I want to see the code that *isn't* working.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2016-08-25 00:46:58

frank604
Member
From: BC, Canada
Registered: 2011-04-20
Posts: 1,212

Re: [Solved] Adding color output to C statusbar

Sorry.  I guess I should've foreseen that.  It is pushed! smile

Only edited line 48 with a cntrl+v x02 showing ^B
Output of bar is here
It is the unknown glyph to the left left of the cpu %

Offline

#8 2016-08-25 01:31:54

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [Solved] Adding color output to C statusbar

Looks good to me, and works on my system - not for dwm, just setting the root window name.

Run slstatus from the command line and check the root window name (e.g., `xprop -root`) to see what was actually set.  Then try your bash script and see what the root window name is set to.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#9 2016-08-25 03:28:13

frank604
Member
From: BC, Canada
Registered: 2011-04-20
Posts: 1,212

Re: [Solved] Adding color output to C statusbar

Oh!  hoho, this is great news.  Must be an issue with my dwm config or patchwork.  I'll investigate further.  Thank you for viewing and running the code.  *high five*  time to go home soon so I'lll look into it.  Thanks Trilby!!

edit: I will mark this thread as solved as you have given me the knowledge to pipe out color from the C statusbar.  The new issue lies elsewhere.

Last edited by frank604 (2016-08-25 03:29:15)

Offline

Board footer

Powered by FluxBB