gyscos / cursive

A Text User Interface library for the Rust programming language
MIT License
4.26k stars 243 forks source link

[FEATURE] Menubar to render StyledStrings #781

Closed svfat closed 4 months ago

svfat commented 4 months ago

Is your feature request related to a problem? Please describe. I'm frustrated because the menubar doesn't render colours from StyledString. Basically, I would like to have ability to highlight keyboard shortcuts (e.g. "F" in "File")

Describe the solution you'd like I would like the menubar to render the colours specified in the StyledString. For example, when passing a StyledString with specific colours, those colours should appear on the menubar.

Describe alternatives you've considered I considered using plain strings instead, but this doesn't allow for the use of colours or styles, which limits the visual customisation options.

Additional context Here is a minimal example demonstrating the issue:

use cursive;
use cursive::utils::markup::StyledString;
use cursive::theme::BaseColor;
use cursive::theme::Color;
use cursive::theme::Style;
use cursive::theme::Effect;

fn main() {
    let mut siv = cursive::default();
    let mut styled = StyledString::plain("Isn't ");
    styled.append(StyledString::styled("that ", Color::Dark(BaseColor::Red)));
    styled.append(StyledString::styled(
        "cool?",
        Style::from(Color::Light(BaseColor::Blue)).combine(Effect::Bold),
    ));
    siv.menubar()
        .add_leaf(styled, |s| s.quit());
    siv.set_autohide_menu(false);
    siv.add_global_callback('q', |s| s.quit());
    siv.run();
}

screenshot

gyscos commented 4 months ago

Hi, and thanks for the report!

Indeed, it would be very nice to be able to use StyledStrings there! Especially to use bold or underscores to highlight shortcuts.

Right now the menu doesn't support these, mostly because it was written before styled strings were a thing. Shouldn't be too hard to add that!

svfat commented 4 months ago

I can take this on if you could point me in the right direction. I'm not yet familiar with the sources

gyscos commented 4 months ago

Sure!

There's two parts to the menubar:

So to fix this last point, I would recommend making 2 changes:

(Note for later: we should really try to make more functions const to help here! Right now we're often using the Into trait so that limits what we can const-ify, but hopefully it'll get better eventually...)

svfat commented 4 months ago

Wow, thanks for the thorough instructions. The fix seems to work, I will test it and prepare a proper PR soon

cool
svfat commented 4 months ago

merged https://github.com/gyscos/cursive/pull/784