Dynamically use colors in a shell prompt depending on battery state, time, percentage









up vote
1
down vote

favorite












I'm attempting to create a dynamic prompt that uses colour-encodings for providing visual cues of the battery situation.



I will be using pmset -g batt to get all of this information.



So far I'm working with the fetching of



  1. Battery percentage

  2. Battery charging, discharging, charged -state

  3. Battery estimated time to charge or to discharge (=time left)

Number 1 can be fetched by pmset -g batt | egrep '([0-9]+%).*' -o --colour=auto | cut -f1 -d';' , and the result will be 100% (well, currently, since my battery is fully charged).



Number 2 can be fetched by pmset -g batt | awk '/charging|discharging|charged/ print $4' | cut -f1 -d';', but for it to work properly in the prompt, you'll have to modify it to be pmset -g batt | awk '/charging|discharging|charged/ print $4' | cut -f2 -d';' - which will then show an awkward empty space before the word. so " charged" instead of "charged", " discharging" instead of "discharging" and " charging" instead of "charging". That's fine, I guess, but I'm thinking this could be taken further.



Number 3 can be fetched by pmset -g batt | grep -Eo '([0-9][0-9]|[0-9]):[0-5][0-9]' - but, if your battery is fully charged, you will get a cool 0:00, which makes no sense.



Okay, we've established that this kind of information can be fetched out, and can be displayed at least in the terminal itself, what about the dynamic prompt issue?




Well, here's what I have right now:



[100%] [ charged] [Sat Nov 10 21:24:34] [~] >>



This is not perfectly ideal. For instance, the doesn't need to show at all, considering the info it would show is 0:00. the [ charged] is also a bit needless.



So here's what I propose to ask how to do:



  1. When the battery is full, just simply show the percentage, no "[time left / time to charge]" and no "[ charged]

  2. When the battery is discharging, show the percentage, and the "[time left] (percentage colour could be.. modified?).

  3. When the battery is charging, show the percentage, and the "[time left] (percentage could be yellow).

  4. When the battery is critical (say, 15%) (percentage could be red)

Now, I got this far:



export PS1="[$(pmset -g batt | egrep '([0-9]+%).*' -o --colour=auto | cut -f1 -d';')] [$(pmset -g batt | grep -Eo '([0-9][0-9]|[0-9]):[0-5][0-9]')] [$(pmset -g batt | awk '/charging|discharging|charged/ print $4' | cut -f2 -d';')] [d t]"



which gives me [100%] [0:00] [ charged] [Sat Nov 10 21:32:38]



Anyway, back to that weird cut -f1 vs. cut -f2 difference between terminal and prompt. If I put cut -f1 into the export PS1 prompt, I get this:



[100%] [0:00] [ -InternalBattery-0 (id=4194403) 100%] [Sat Nov 10 21:34:44]



Anyway, I'm thinking there has got to be a way of going "oh, 0:00? don't display this segment of the prompt at all" - but I'm not sure how to do that.



So, to recap, I'm trying to utilise "Charged,Charging,Discharging" to set the battery percentage colour, charged = regular color, charging = yellow, discharging = ..green?, less than 20% = panic red color. 5% blink? :D



I'm also trying to use the "time left to charge" and "time left of charge" time to be displayed in the prompt itself, dynamically.



Any advice would be much appreciated.



p.s. extra bonus points for showing [estimating] if pmset -g batt results in (no estimate) on the time-left-of-charge



p.p.s. extra bonus points#2 for figuring out what to do with (i.e. this is a state that isn't (no estimate) but will sometimes occur.




Now drawing from 'AC Power'
-InternalBattery-0 (id=4194403) 89%; AC attached; not charging present: true










share|improve this question























  • Per your question, it looks like you already are dynamically extracting the battery information. Is your question about making the 0:00 go away, something else, or a set of small questions?
    – New Alexandria
    Nov 9 at 13:37










  • a set of small questions. 1: how to hide 0:00 if there is nothing to display 2: how to use the charged+charging+discharged for altering the colors of the battery percentage, 3: how to display the time-left-to-charge and time-of-charge-left where the 0:00 is but with highlighted colors.. this would make it a pretty compact solution - you'd see at a glance how much time you've got left and whether you're charging or discharging 4: setting "anything below 25% to red" for instance. this kind of coloring + data-reading logic.
    – esaruoho
    Nov 9 at 19:12






  • 1




    @NewAlexandria Hi, I have rewritten the question to be more precise and this should be understandable. If something is missing, please let me know and I'll try and clarify better.
    – esaruoho
    Nov 10 at 19:38














up vote
1
down vote

favorite












I'm attempting to create a dynamic prompt that uses colour-encodings for providing visual cues of the battery situation.



I will be using pmset -g batt to get all of this information.



So far I'm working with the fetching of



  1. Battery percentage

  2. Battery charging, discharging, charged -state

  3. Battery estimated time to charge or to discharge (=time left)

Number 1 can be fetched by pmset -g batt | egrep '([0-9]+%).*' -o --colour=auto | cut -f1 -d';' , and the result will be 100% (well, currently, since my battery is fully charged).



Number 2 can be fetched by pmset -g batt | awk '/charging|discharging|charged/ print $4' | cut -f1 -d';', but for it to work properly in the prompt, you'll have to modify it to be pmset -g batt | awk '/charging|discharging|charged/ print $4' | cut -f2 -d';' - which will then show an awkward empty space before the word. so " charged" instead of "charged", " discharging" instead of "discharging" and " charging" instead of "charging". That's fine, I guess, but I'm thinking this could be taken further.



Number 3 can be fetched by pmset -g batt | grep -Eo '([0-9][0-9]|[0-9]):[0-5][0-9]' - but, if your battery is fully charged, you will get a cool 0:00, which makes no sense.



Okay, we've established that this kind of information can be fetched out, and can be displayed at least in the terminal itself, what about the dynamic prompt issue?




Well, here's what I have right now:



[100%] [ charged] [Sat Nov 10 21:24:34] [~] >>



This is not perfectly ideal. For instance, the doesn't need to show at all, considering the info it would show is 0:00. the [ charged] is also a bit needless.



So here's what I propose to ask how to do:



  1. When the battery is full, just simply show the percentage, no "[time left / time to charge]" and no "[ charged]

  2. When the battery is discharging, show the percentage, and the "[time left] (percentage colour could be.. modified?).

  3. When the battery is charging, show the percentage, and the "[time left] (percentage could be yellow).

  4. When the battery is critical (say, 15%) (percentage could be red)

Now, I got this far:



export PS1="[$(pmset -g batt | egrep '([0-9]+%).*' -o --colour=auto | cut -f1 -d';')] [$(pmset -g batt | grep -Eo '([0-9][0-9]|[0-9]):[0-5][0-9]')] [$(pmset -g batt | awk '/charging|discharging|charged/ print $4' | cut -f2 -d';')] [d t]"



which gives me [100%] [0:00] [ charged] [Sat Nov 10 21:32:38]



Anyway, back to that weird cut -f1 vs. cut -f2 difference between terminal and prompt. If I put cut -f1 into the export PS1 prompt, I get this:



[100%] [0:00] [ -InternalBattery-0 (id=4194403) 100%] [Sat Nov 10 21:34:44]



Anyway, I'm thinking there has got to be a way of going "oh, 0:00? don't display this segment of the prompt at all" - but I'm not sure how to do that.



So, to recap, I'm trying to utilise "Charged,Charging,Discharging" to set the battery percentage colour, charged = regular color, charging = yellow, discharging = ..green?, less than 20% = panic red color. 5% blink? :D



I'm also trying to use the "time left to charge" and "time left of charge" time to be displayed in the prompt itself, dynamically.



Any advice would be much appreciated.



p.s. extra bonus points for showing [estimating] if pmset -g batt results in (no estimate) on the time-left-of-charge



p.p.s. extra bonus points#2 for figuring out what to do with (i.e. this is a state that isn't (no estimate) but will sometimes occur.




Now drawing from 'AC Power'
-InternalBattery-0 (id=4194403) 89%; AC attached; not charging present: true










share|improve this question























  • Per your question, it looks like you already are dynamically extracting the battery information. Is your question about making the 0:00 go away, something else, or a set of small questions?
    – New Alexandria
    Nov 9 at 13:37










  • a set of small questions. 1: how to hide 0:00 if there is nothing to display 2: how to use the charged+charging+discharged for altering the colors of the battery percentage, 3: how to display the time-left-to-charge and time-of-charge-left where the 0:00 is but with highlighted colors.. this would make it a pretty compact solution - you'd see at a glance how much time you've got left and whether you're charging or discharging 4: setting "anything below 25% to red" for instance. this kind of coloring + data-reading logic.
    – esaruoho
    Nov 9 at 19:12






  • 1




    @NewAlexandria Hi, I have rewritten the question to be more precise and this should be understandable. If something is missing, please let me know and I'll try and clarify better.
    – esaruoho
    Nov 10 at 19:38












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm attempting to create a dynamic prompt that uses colour-encodings for providing visual cues of the battery situation.



I will be using pmset -g batt to get all of this information.



So far I'm working with the fetching of



  1. Battery percentage

  2. Battery charging, discharging, charged -state

  3. Battery estimated time to charge or to discharge (=time left)

Number 1 can be fetched by pmset -g batt | egrep '([0-9]+%).*' -o --colour=auto | cut -f1 -d';' , and the result will be 100% (well, currently, since my battery is fully charged).



Number 2 can be fetched by pmset -g batt | awk '/charging|discharging|charged/ print $4' | cut -f1 -d';', but for it to work properly in the prompt, you'll have to modify it to be pmset -g batt | awk '/charging|discharging|charged/ print $4' | cut -f2 -d';' - which will then show an awkward empty space before the word. so " charged" instead of "charged", " discharging" instead of "discharging" and " charging" instead of "charging". That's fine, I guess, but I'm thinking this could be taken further.



Number 3 can be fetched by pmset -g batt | grep -Eo '([0-9][0-9]|[0-9]):[0-5][0-9]' - but, if your battery is fully charged, you will get a cool 0:00, which makes no sense.



Okay, we've established that this kind of information can be fetched out, and can be displayed at least in the terminal itself, what about the dynamic prompt issue?




Well, here's what I have right now:



[100%] [ charged] [Sat Nov 10 21:24:34] [~] >>



This is not perfectly ideal. For instance, the doesn't need to show at all, considering the info it would show is 0:00. the [ charged] is also a bit needless.



So here's what I propose to ask how to do:



  1. When the battery is full, just simply show the percentage, no "[time left / time to charge]" and no "[ charged]

  2. When the battery is discharging, show the percentage, and the "[time left] (percentage colour could be.. modified?).

  3. When the battery is charging, show the percentage, and the "[time left] (percentage could be yellow).

  4. When the battery is critical (say, 15%) (percentage could be red)

Now, I got this far:



export PS1="[$(pmset -g batt | egrep '([0-9]+%).*' -o --colour=auto | cut -f1 -d';')] [$(pmset -g batt | grep -Eo '([0-9][0-9]|[0-9]):[0-5][0-9]')] [$(pmset -g batt | awk '/charging|discharging|charged/ print $4' | cut -f2 -d';')] [d t]"



which gives me [100%] [0:00] [ charged] [Sat Nov 10 21:32:38]



Anyway, back to that weird cut -f1 vs. cut -f2 difference between terminal and prompt. If I put cut -f1 into the export PS1 prompt, I get this:



[100%] [0:00] [ -InternalBattery-0 (id=4194403) 100%] [Sat Nov 10 21:34:44]



Anyway, I'm thinking there has got to be a way of going "oh, 0:00? don't display this segment of the prompt at all" - but I'm not sure how to do that.



So, to recap, I'm trying to utilise "Charged,Charging,Discharging" to set the battery percentage colour, charged = regular color, charging = yellow, discharging = ..green?, less than 20% = panic red color. 5% blink? :D



I'm also trying to use the "time left to charge" and "time left of charge" time to be displayed in the prompt itself, dynamically.



Any advice would be much appreciated.



p.s. extra bonus points for showing [estimating] if pmset -g batt results in (no estimate) on the time-left-of-charge



p.p.s. extra bonus points#2 for figuring out what to do with (i.e. this is a state that isn't (no estimate) but will sometimes occur.




Now drawing from 'AC Power'
-InternalBattery-0 (id=4194403) 89%; AC attached; not charging present: true










share|improve this question















I'm attempting to create a dynamic prompt that uses colour-encodings for providing visual cues of the battery situation.



I will be using pmset -g batt to get all of this information.



So far I'm working with the fetching of



  1. Battery percentage

  2. Battery charging, discharging, charged -state

  3. Battery estimated time to charge or to discharge (=time left)

Number 1 can be fetched by pmset -g batt | egrep '([0-9]+%).*' -o --colour=auto | cut -f1 -d';' , and the result will be 100% (well, currently, since my battery is fully charged).



Number 2 can be fetched by pmset -g batt | awk '/charging|discharging|charged/ print $4' | cut -f1 -d';', but for it to work properly in the prompt, you'll have to modify it to be pmset -g batt | awk '/charging|discharging|charged/ print $4' | cut -f2 -d';' - which will then show an awkward empty space before the word. so " charged" instead of "charged", " discharging" instead of "discharging" and " charging" instead of "charging". That's fine, I guess, but I'm thinking this could be taken further.



Number 3 can be fetched by pmset -g batt | grep -Eo '([0-9][0-9]|[0-9]):[0-5][0-9]' - but, if your battery is fully charged, you will get a cool 0:00, which makes no sense.



Okay, we've established that this kind of information can be fetched out, and can be displayed at least in the terminal itself, what about the dynamic prompt issue?




Well, here's what I have right now:



[100%] [ charged] [Sat Nov 10 21:24:34] [~] >>



This is not perfectly ideal. For instance, the doesn't need to show at all, considering the info it would show is 0:00. the [ charged] is also a bit needless.



So here's what I propose to ask how to do:



  1. When the battery is full, just simply show the percentage, no "[time left / time to charge]" and no "[ charged]

  2. When the battery is discharging, show the percentage, and the "[time left] (percentage colour could be.. modified?).

  3. When the battery is charging, show the percentage, and the "[time left] (percentage could be yellow).

  4. When the battery is critical (say, 15%) (percentage could be red)

Now, I got this far:



export PS1="[$(pmset -g batt | egrep '([0-9]+%).*' -o --colour=auto | cut -f1 -d';')] [$(pmset -g batt | grep -Eo '([0-9][0-9]|[0-9]):[0-5][0-9]')] [$(pmset -g batt | awk '/charging|discharging|charged/ print $4' | cut -f2 -d';')] [d t]"



which gives me [100%] [0:00] [ charged] [Sat Nov 10 21:32:38]



Anyway, back to that weird cut -f1 vs. cut -f2 difference between terminal and prompt. If I put cut -f1 into the export PS1 prompt, I get this:



[100%] [0:00] [ -InternalBattery-0 (id=4194403) 100%] [Sat Nov 10 21:34:44]



Anyway, I'm thinking there has got to be a way of going "oh, 0:00? don't display this segment of the prompt at all" - but I'm not sure how to do that.



So, to recap, I'm trying to utilise "Charged,Charging,Discharging" to set the battery percentage colour, charged = regular color, charging = yellow, discharging = ..green?, less than 20% = panic red color. 5% blink? :D



I'm also trying to use the "time left to charge" and "time left of charge" time to be displayed in the prompt itself, dynamically.



Any advice would be much appreciated.



p.s. extra bonus points for showing [estimating] if pmset -g batt results in (no estimate) on the time-left-of-charge



p.p.s. extra bonus points#2 for figuring out what to do with (i.e. this is a state that isn't (no estimate) but will sometimes occur.




Now drawing from 'AC Power'
-InternalBattery-0 (id=4194403) 89%; AC attached; not charging present: true







bash macos awk terminal prompt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 20:55

























asked Nov 9 at 8:36









esaruoho

291114




291114











  • Per your question, it looks like you already are dynamically extracting the battery information. Is your question about making the 0:00 go away, something else, or a set of small questions?
    – New Alexandria
    Nov 9 at 13:37










  • a set of small questions. 1: how to hide 0:00 if there is nothing to display 2: how to use the charged+charging+discharged for altering the colors of the battery percentage, 3: how to display the time-left-to-charge and time-of-charge-left where the 0:00 is but with highlighted colors.. this would make it a pretty compact solution - you'd see at a glance how much time you've got left and whether you're charging or discharging 4: setting "anything below 25% to red" for instance. this kind of coloring + data-reading logic.
    – esaruoho
    Nov 9 at 19:12






  • 1




    @NewAlexandria Hi, I have rewritten the question to be more precise and this should be understandable. If something is missing, please let me know and I'll try and clarify better.
    – esaruoho
    Nov 10 at 19:38
















  • Per your question, it looks like you already are dynamically extracting the battery information. Is your question about making the 0:00 go away, something else, or a set of small questions?
    – New Alexandria
    Nov 9 at 13:37










  • a set of small questions. 1: how to hide 0:00 if there is nothing to display 2: how to use the charged+charging+discharged for altering the colors of the battery percentage, 3: how to display the time-left-to-charge and time-of-charge-left where the 0:00 is but with highlighted colors.. this would make it a pretty compact solution - you'd see at a glance how much time you've got left and whether you're charging or discharging 4: setting "anything below 25% to red" for instance. this kind of coloring + data-reading logic.
    – esaruoho
    Nov 9 at 19:12






  • 1




    @NewAlexandria Hi, I have rewritten the question to be more precise and this should be understandable. If something is missing, please let me know and I'll try and clarify better.
    – esaruoho
    Nov 10 at 19:38















Per your question, it looks like you already are dynamically extracting the battery information. Is your question about making the 0:00 go away, something else, or a set of small questions?
– New Alexandria
Nov 9 at 13:37




Per your question, it looks like you already are dynamically extracting the battery information. Is your question about making the 0:00 go away, something else, or a set of small questions?
– New Alexandria
Nov 9 at 13:37












a set of small questions. 1: how to hide 0:00 if there is nothing to display 2: how to use the charged+charging+discharged for altering the colors of the battery percentage, 3: how to display the time-left-to-charge and time-of-charge-left where the 0:00 is but with highlighted colors.. this would make it a pretty compact solution - you'd see at a glance how much time you've got left and whether you're charging or discharging 4: setting "anything below 25% to red" for instance. this kind of coloring + data-reading logic.
– esaruoho
Nov 9 at 19:12




a set of small questions. 1: how to hide 0:00 if there is nothing to display 2: how to use the charged+charging+discharged for altering the colors of the battery percentage, 3: how to display the time-left-to-charge and time-of-charge-left where the 0:00 is but with highlighted colors.. this would make it a pretty compact solution - you'd see at a glance how much time you've got left and whether you're charging or discharging 4: setting "anything below 25% to red" for instance. this kind of coloring + data-reading logic.
– esaruoho
Nov 9 at 19:12




1




1




@NewAlexandria Hi, I have rewritten the question to be more precise and this should be understandable. If something is missing, please let me know and I'll try and clarify better.
– esaruoho
Nov 10 at 19:38




@NewAlexandria Hi, I have rewritten the question to be more precise and this should be understandable. If something is missing, please let me know and I'll try and clarify better.
– esaruoho
Nov 10 at 19:38












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Your question is really about how to handle complex prompts. The actually complicated question here is how to handle conditional use of ANSI colors in a prompt.



The sub-questions are 'normal' string chomping, conditional, and ANSI code. All of this I handle in my dotfiles, in profile and functions_colors.sh.



What you really want is



export PS1="[$(__batt_pct)]$(__batt_time) [$(__batt_state)] [d t] "


and then



yellow=$(tput setaf 184)
green=$(tput setaf 120)
red=$(tput setaf 160)
reset=$(tput init)

function __batt_pct() egrep '([0-9]+)%.*' -o

# now, as a function, we can easily handle a conditional
function __batt_time()
btime=$(pmset -g batt

# I need to cut field-1 for this to trim evenly
function __batt_state() charged/ print $4'


We use __ because to follow a unix convention for function names that won't need to be manually used at the CLI.



For the colors, just follow any ANSI color guide, e.g. the ones I linked in my dotfiles. Then change that __batt_state function



# I need to cut field-1 for this to trim evenly
function __batt_state() discharging



I decided to drop all of this in a repo, so that I can use it easier.






share|improve this answer






















  • Hi, thanks for your clarifications. I tried what you sent, and got these results: [100%] [8:10] [33[31mdischarging33[00m] [Fri Nov 09 21:16:21] [~] >> so not sure what's going on in here :)
    – esaruoho
    Nov 9 at 19:17






  • 1




    Just the way the ANSI codes were echoed. I"ll look at the more correct solution later
    – New Alexandria
    Nov 10 at 1:03










  • Note that the --colour=auto option to (e)grep is useless, as its output goes to a pipe
    – Andrea Corbellini
    Nov 10 at 21:44










  • @AndreaCorbellini thanks! i've removed all --colour=auto from everything :) nice to get rid of uselessness.
    – esaruoho
    Nov 11 at 9:08






  • 2




    I recommend using tput rather than hard-coding sequences
    – Rafael
    Nov 11 at 13:47










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53222271%2fdynamically-use-colors-in-a-shell-prompt-depending-on-battery-state-time-perce%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Your question is really about how to handle complex prompts. The actually complicated question here is how to handle conditional use of ANSI colors in a prompt.



The sub-questions are 'normal' string chomping, conditional, and ANSI code. All of this I handle in my dotfiles, in profile and functions_colors.sh.



What you really want is



export PS1="[$(__batt_pct)]$(__batt_time) [$(__batt_state)] [d t] "


and then



yellow=$(tput setaf 184)
green=$(tput setaf 120)
red=$(tput setaf 160)
reset=$(tput init)

function __batt_pct() egrep '([0-9]+)%.*' -o

# now, as a function, we can easily handle a conditional
function __batt_time()
btime=$(pmset -g batt

# I need to cut field-1 for this to trim evenly
function __batt_state() charged/ print $4'


We use __ because to follow a unix convention for function names that won't need to be manually used at the CLI.



For the colors, just follow any ANSI color guide, e.g. the ones I linked in my dotfiles. Then change that __batt_state function



# I need to cut field-1 for this to trim evenly
function __batt_state() discharging



I decided to drop all of this in a repo, so that I can use it easier.






share|improve this answer






















  • Hi, thanks for your clarifications. I tried what you sent, and got these results: [100%] [8:10] [33[31mdischarging33[00m] [Fri Nov 09 21:16:21] [~] >> so not sure what's going on in here :)
    – esaruoho
    Nov 9 at 19:17






  • 1




    Just the way the ANSI codes were echoed. I"ll look at the more correct solution later
    – New Alexandria
    Nov 10 at 1:03










  • Note that the --colour=auto option to (e)grep is useless, as its output goes to a pipe
    – Andrea Corbellini
    Nov 10 at 21:44










  • @AndreaCorbellini thanks! i've removed all --colour=auto from everything :) nice to get rid of uselessness.
    – esaruoho
    Nov 11 at 9:08






  • 2




    I recommend using tput rather than hard-coding sequences
    – Rafael
    Nov 11 at 13:47














up vote
2
down vote



accepted










Your question is really about how to handle complex prompts. The actually complicated question here is how to handle conditional use of ANSI colors in a prompt.



The sub-questions are 'normal' string chomping, conditional, and ANSI code. All of this I handle in my dotfiles, in profile and functions_colors.sh.



What you really want is



export PS1="[$(__batt_pct)]$(__batt_time) [$(__batt_state)] [d t] "


and then



yellow=$(tput setaf 184)
green=$(tput setaf 120)
red=$(tput setaf 160)
reset=$(tput init)

function __batt_pct() egrep '([0-9]+)%.*' -o

# now, as a function, we can easily handle a conditional
function __batt_time()
btime=$(pmset -g batt

# I need to cut field-1 for this to trim evenly
function __batt_state() charged/ print $4'


We use __ because to follow a unix convention for function names that won't need to be manually used at the CLI.



For the colors, just follow any ANSI color guide, e.g. the ones I linked in my dotfiles. Then change that __batt_state function



# I need to cut field-1 for this to trim evenly
function __batt_state() discharging



I decided to drop all of this in a repo, so that I can use it easier.






share|improve this answer






















  • Hi, thanks for your clarifications. I tried what you sent, and got these results: [100%] [8:10] [33[31mdischarging33[00m] [Fri Nov 09 21:16:21] [~] >> so not sure what's going on in here :)
    – esaruoho
    Nov 9 at 19:17






  • 1




    Just the way the ANSI codes were echoed. I"ll look at the more correct solution later
    – New Alexandria
    Nov 10 at 1:03










  • Note that the --colour=auto option to (e)grep is useless, as its output goes to a pipe
    – Andrea Corbellini
    Nov 10 at 21:44










  • @AndreaCorbellini thanks! i've removed all --colour=auto from everything :) nice to get rid of uselessness.
    – esaruoho
    Nov 11 at 9:08






  • 2




    I recommend using tput rather than hard-coding sequences
    – Rafael
    Nov 11 at 13:47












up vote
2
down vote



accepted







up vote
2
down vote



accepted






Your question is really about how to handle complex prompts. The actually complicated question here is how to handle conditional use of ANSI colors in a prompt.



The sub-questions are 'normal' string chomping, conditional, and ANSI code. All of this I handle in my dotfiles, in profile and functions_colors.sh.



What you really want is



export PS1="[$(__batt_pct)]$(__batt_time) [$(__batt_state)] [d t] "


and then



yellow=$(tput setaf 184)
green=$(tput setaf 120)
red=$(tput setaf 160)
reset=$(tput init)

function __batt_pct() egrep '([0-9]+)%.*' -o

# now, as a function, we can easily handle a conditional
function __batt_time()
btime=$(pmset -g batt

# I need to cut field-1 for this to trim evenly
function __batt_state() charged/ print $4'


We use __ because to follow a unix convention for function names that won't need to be manually used at the CLI.



For the colors, just follow any ANSI color guide, e.g. the ones I linked in my dotfiles. Then change that __batt_state function



# I need to cut field-1 for this to trim evenly
function __batt_state() discharging



I decided to drop all of this in a repo, so that I can use it easier.






share|improve this answer














Your question is really about how to handle complex prompts. The actually complicated question here is how to handle conditional use of ANSI colors in a prompt.



The sub-questions are 'normal' string chomping, conditional, and ANSI code. All of this I handle in my dotfiles, in profile and functions_colors.sh.



What you really want is



export PS1="[$(__batt_pct)]$(__batt_time) [$(__batt_state)] [d t] "


and then



yellow=$(tput setaf 184)
green=$(tput setaf 120)
red=$(tput setaf 160)
reset=$(tput init)

function __batt_pct() egrep '([0-9]+)%.*' -o

# now, as a function, we can easily handle a conditional
function __batt_time()
btime=$(pmset -g batt

# I need to cut field-1 for this to trim evenly
function __batt_state() charged/ print $4'


We use __ because to follow a unix convention for function names that won't need to be manually used at the CLI.



For the colors, just follow any ANSI color guide, e.g. the ones I linked in my dotfiles. Then change that __batt_state function



# I need to cut field-1 for this to trim evenly
function __batt_state() discharging



I decided to drop all of this in a repo, so that I can use it easier.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 20:22

























answered Nov 9 at 14:50









New Alexandria

5,27733858




5,27733858











  • Hi, thanks for your clarifications. I tried what you sent, and got these results: [100%] [8:10] [33[31mdischarging33[00m] [Fri Nov 09 21:16:21] [~] >> so not sure what's going on in here :)
    – esaruoho
    Nov 9 at 19:17






  • 1




    Just the way the ANSI codes were echoed. I"ll look at the more correct solution later
    – New Alexandria
    Nov 10 at 1:03










  • Note that the --colour=auto option to (e)grep is useless, as its output goes to a pipe
    – Andrea Corbellini
    Nov 10 at 21:44










  • @AndreaCorbellini thanks! i've removed all --colour=auto from everything :) nice to get rid of uselessness.
    – esaruoho
    Nov 11 at 9:08






  • 2




    I recommend using tput rather than hard-coding sequences
    – Rafael
    Nov 11 at 13:47
















  • Hi, thanks for your clarifications. I tried what you sent, and got these results: [100%] [8:10] [33[31mdischarging33[00m] [Fri Nov 09 21:16:21] [~] >> so not sure what's going on in here :)
    – esaruoho
    Nov 9 at 19:17






  • 1




    Just the way the ANSI codes were echoed. I"ll look at the more correct solution later
    – New Alexandria
    Nov 10 at 1:03










  • Note that the --colour=auto option to (e)grep is useless, as its output goes to a pipe
    – Andrea Corbellini
    Nov 10 at 21:44










  • @AndreaCorbellini thanks! i've removed all --colour=auto from everything :) nice to get rid of uselessness.
    – esaruoho
    Nov 11 at 9:08






  • 2




    I recommend using tput rather than hard-coding sequences
    – Rafael
    Nov 11 at 13:47















Hi, thanks for your clarifications. I tried what you sent, and got these results: [100%] [8:10] [33[31mdischarging33[00m] [Fri Nov 09 21:16:21] [~] >> so not sure what's going on in here :)
– esaruoho
Nov 9 at 19:17




Hi, thanks for your clarifications. I tried what you sent, and got these results: [100%] [8:10] [33[31mdischarging33[00m] [Fri Nov 09 21:16:21] [~] >> so not sure what's going on in here :)
– esaruoho
Nov 9 at 19:17




1




1




Just the way the ANSI codes were echoed. I"ll look at the more correct solution later
– New Alexandria
Nov 10 at 1:03




Just the way the ANSI codes were echoed. I"ll look at the more correct solution later
– New Alexandria
Nov 10 at 1:03












Note that the --colour=auto option to (e)grep is useless, as its output goes to a pipe
– Andrea Corbellini
Nov 10 at 21:44




Note that the --colour=auto option to (e)grep is useless, as its output goes to a pipe
– Andrea Corbellini
Nov 10 at 21:44












@AndreaCorbellini thanks! i've removed all --colour=auto from everything :) nice to get rid of uselessness.
– esaruoho
Nov 11 at 9:08




@AndreaCorbellini thanks! i've removed all --colour=auto from everything :) nice to get rid of uselessness.
– esaruoho
Nov 11 at 9:08




2




2




I recommend using tput rather than hard-coding sequences
– Rafael
Nov 11 at 13:47




I recommend using tput rather than hard-coding sequences
– Rafael
Nov 11 at 13:47

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53222271%2fdynamically-use-colors-in-a-shell-prompt-depending-on-battery-state-time-perce%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo