Misterio77 / flavours

🎨💧 An easy to use base16 scheme manager that integrates with any workflow.
MIT License
501 stars 29 forks source link

Configurable sources (and source hierarchy) #62

Closed Softsun2 closed 1 year ago

Softsun2 commented 2 years ago

It appears a few people want support for configurable sources. As I mentioned in my previously closed pr I was thinking of doing so, I believe this is a more proper solution than my last pr but this is still my first time writing rust so I would review my additions if you're interested in this feature.

Configuration Hierarchy

The config.toml source lists take precedence over ~/.local/share/flavours/base16/sources.yaml and ~/.local/share/flavours/base16/sources.yaml takes precedence over the default source lists.

Configuration Schema

The config.toml now supports configuring source lists and extra sources.

/// Structure for configuration
#[derive(Deserialize, Debug)]
pub struct Config {
    pub shell: Option<String>,
    pub schemes: Option<String>,
    pub templates: Option<String>,
    pub extra_scheme: Option<Vec<ExtraSource>>,
    pub extra_template: Option<Vec<ExtraSource>>,
    pub item: Option<Vec<ConfigItem>>,
    pub items: Option<Vec<ConfigItem>>,
}

Extra Schemes and Templates

In the case where a user would like to configure schemes and templates that are not included in their active source lists they may provide extra schemes and templates that will be inserted into the appropriate list.yaml file. The Config struct fields extra_scheme and extra_template are vectors of the following extra source structure.

/// Structure for configuration extra sources
#[derive(Deserialize, Debug)]
pub struct ExtraSource{
    pub name: String,
    pub source: String,
}

Example

I've attached a simple config.toml that shows the new features with some snippets of the resulting files after running flavours update all.

# config.toml
schemes = "https://github.com/Softsun2/base16-schemes-source"
# templates = "my-templates"

[[extra_scheme]]
name = "tarko"
source = "https://github.com/Softsun2/base16-tarko-scheme"

Here I'm choosing to use a non-default scheme list, the default template list, and an extra scheme that's not included in the scheme list I've configured.

# ~/.local/share/flavours/base16/sources.yaml
schemes: https://github.com/Softsun2/base16-schemes-source
templates: https://github.com/chriskempson/base16-templates-source.git

You can see that the configured source list was written as expected.

# ~/.local/share/flavours/base16/sources/schemes/list.yaml
...
synth-midnight: https://github.com/michael-ball/base16-synth-midnight-scheme
tango: https://github.com/Schnouki/base16-tango-scheme
tarko: https://github.com/Softsun2/base16-tarko-scheme
tender: https://github.com/DanManN/base16-tender-scheme
twilight: https://github.com/hartbit/base16-twilight-scheme
...

This is a snippet of the schemes list yaml file. Just shows that the extra scheme (tarko) made it's way into the list of schemes.

Notes

I haven't really tested this much, I plan on using this fork of flavours, I'll report/fix bugs I run into. I have zero rust experience, so I would review my changes closely if this is a feature you would like to implement.

Misterio77 commented 1 year ago

I like it a lot! I was actually thinking of a way to allow users to include non-listed schemes or templates (without forking the sources), and I think you pretty much nailed it with a great design.

Code seems fine. I'm terribly sorry about the spaghetti code, it was my first real rust program. I should refactor it sometime (tm).

I will merge it as soon as I test it. Thanks a lot!

Misterio77 commented 1 year ago

Seems to work nicely!

Something I think might be useful is setting the URLs to an empty string could perhaps skip cloning and write an empty list (for people who prefer to not use any list). I will see if I can implement this in a follow up commit.

Thanks again!