[SOLVED] Need help with "Battery" sublet color

Added by tasty minerals about 5 years ago

I need help to make the "battery" sublet colorful, depending on the amount of battery charge.

First, I need an explanation on the following:
When I installed "battery" sublet, I saw this message:

A hash with colors for different percentages can be passed to the
sublet, hash index is the desired percentage and the value the
color.

Example:
sublet :battery do
  colors 10 => "#ff0000", 30 => "#fff000" 
end

But, where in the code can I actually insert it? I've tried some variants, but no success.

Second, I've found this on wiki

# Works with colors too
   s.red = Subtlext::Color.new(s.config[:red] || "#ff0000")
 end

 on :run do |s|
   s.data = s.red + s.some_string
 end

How do these two methods correspond? Which should I choose? How can I modify the code so it would work?

I think, I must add something to this part

s.data = "%s%s%s%d%%" % [
      s.color_icon ? s.color : s.color_def, s.icons[icon],
      s.color_text ? s.color : s.color_def, percent
    ]


Replies (8)

RE: Need help with "Battery" sublet color - Added by Christoph Kappel about 5 years ago

The example in the wiki is for sublet writes not users, the info text of the sublets is the best source for information about this certain sublet. Just use the example of your first code snippet.

@colors 10 => "#ff0000", 30 => "#fff000"@

When the battery percentage is 10% or below it uses #ff0000, from 30% to 11% it uses #fff000. You can add arbitrary values for every percent between 100 and 0.

RE: Need help with "Battery" sublet color - Added by tasty minerals about 5 years ago

ok, I think I shall need a help here, I don't know ruby, so modifying the code can become a time consuming task. I don't have time for that now. I've tried some options, but it does not work.

Here is the stripped "battery" code:

configure :battery do |s| # {{{
  s.interval = 60
  s.full     = 0
  s.color = "" # guess this line should be modified with  '10 => "#FABC11", 30 => "#E81C48"'

  # Options
  s.color_text = true  == s.config[:color_text]
  s.color_icon = false == s.config[:color_icon] ? false : true
  s.color_def  = Subtlext::Subtle.colors[:sublets_fg]

  # Collect colors 
  if(s.config[:colors].is_a?(Hash))
    s.colors = {}

    s.config[:colors].each do |k, v|
      s.colors[k] = Subtlext::Color.new(v)
    end

    # Just sort once
    s.color_keys = s.colors.keys.sort.reverse
  end
  rescue => err
    puts err, err.backtrace
    raise "Could't find any battery" 
  end
end # }}}

on :run do |s| # {{{
  begin
# Select color
    unless(s.colors.nil?)
      # Find start color from top
      s.color_keys.each do |k|
        break if(k < percent)
        s.color = s.colors[k] if(k >= percent)
      end
    end
s.data = "%s%s%s%d%%" % [
      s.color_icon ? s.color : s.color_def, s.icons[icon],
      s.color_text ? s.color : s.color_def, percent
    ]
  rescue => err # Sanitize to prevent unloading
    s.data = "subtle" 
    p err
  end
end # }}}

What lines need to be modified?
p.s. Last time, I installed subte wm and battery sublet, I managed to make it work with the wiki example. Unfortunately, either the code changed or I simply forgot how I did that. Anyway, I won't do it myself, cause I don't understand what some blocks do.

RE: Need help with "Battery" sublet color - Added by Christoph Kappel about 5 years ago

I have no idea what you want to change in the code of the sublet? You can configure colors from your config.

RE: Need help with "Battery" sublet color - Added by tasty minerals about 5 years ago

I'm sorry,
I need the charge numbers and battery icon to become orange, when <50% and red, when <15% battery charge is available.

Oh, and How can I do it from config?

RE: Need help with "Battery" sublet color - Added by Sae Hirak about 5 years ago

Untested, but try putting this into your subtle.rb file:

sublet :battery do
  colors 15 => "#ff0000", 50 => "#ff5400" 
end

You can, of course, change the colors to your liking.

By the way, there's a default subtle config located at /etc/xdg/subtle/subtle.rb. You probably want to copy that to your local config so you can play around with it:

cp /etc/xdg/subtle/subtle.rb "${XDG_CONFIG_HOME-$HOME/.config}/subtle/subtle.rb" 

Now just open up this file in whatever text editor you want and add the sublet code:

echo "${XDG_CONFIG_HOME-$HOME/.config}/subtle/subtle.rb" 

RE: Need help with "Battery" sublet color - Added by tasty minerals about 5 years ago

thank you so much, however this colors only the icon of the sublet, the battery charge value stays grey.

RE: [SOLVED] Need help with "Battery" sublet color - Added by Sae Hirak about 5 years ago

Right... try this, then:

sublet :battery do
  colors 15 => "#ff0000", 50 => "#ff5400" 
  color_text true
end

You can see all the options it takes by using sur config battery.

RE: [SOLVED] Need help with "Battery" sublet color - Added by tasty minerals about 5 years ago

yes, this last one works perfectly, thank you guys, you've been very helpful.

(1-8/8)