aristocratos / btop

A monitor of resources
Apache License 2.0
19.91k stars 617 forks source link

[REQUEST]Automatic switch theme from dark to light based on Gnome theme #928

Open lamyergeier opened 1 week ago

lamyergeier commented 1 week ago

Is your feature request related to a problem? Please describe. I would like to switch from solarized light to solarized dark depending on if on Gnome it is dark theme enabled or not.

Describe the solution you'd like Automatic switch theme from dark to light based on Gnome theme, or atleast when it is starting it should be mindful of the Gnome dark theme settings

Additional

Please give CLI option to set theme, so that I can atleast automate through a bash function

imwints commented 1 week ago

The first problem is that there is no mapping of [Light <-> Dark] themes. There are a couple of themes that only have one variant. So btop doesn't know (and probably shouldn't care) if you're theme is dark or light. The other thing is that this would tie a CLI application to a desktop, and I'm not sure if this is good idea.

Please give CLI option to set theme, so that I can atleast automate through a bash function

This seems reasonable.

As a workaround, this wrapper script edits the config based on the theme type you provide it. You still need to talk to gnome and parse it's mode and pass either dark or light to the script.

#!/usr/bin/env python3

import os
import subprocess
import sys
from pathlib import Path

# Mode is the first command line argument
# As an alternative, set mode based on gnome's theme type here
mode = sys.argv[1]

# Path to btop's config file in the user's home
config = (
    Path(os.path.expanduser("~"))
    .joinpath(".config")
    .joinpath("btop")
    .joinpath("btop.conf")
)

# Read the config, replacing the `color_theme` statement based on the `mode`
with config.open() as file:
    lines = [
        line if "color_theme" not in line else f'color_theme = "solarized_{mode}"\n'
        for line in file
    ]

# Write the new config
with config.open("w") as file:
    for line in lines:
        file.write(line)

# Launch btop
subprocess.run(["btop"], check=False)
lamyergeier commented 1 week ago

@imwints Could you provide a CLI Option --theme?

So users can start btop as follows

btop --theme "Solarized dark"