FMotalleb / nu_plugin_image

MIT License
11 stars 4 forks source link

Export image with current shell's theme #2

Open fdncred opened 9 months ago

fdncred commented 9 months ago

This is from to png image This is from ls in the terminal image

You'll notice other problems between these images too. to png looks good but it's not quite right. It's really noticeable on the header and footer labels as well as the row indexes.

FMotalleb commented 9 months ago

will fix colors after patching the ansi parser

FMotalleb commented 9 months ago

added a new feature and flag --theme (-t) with two options (currently), vscode(default) and xterm maybe ill add custom configuration

FMotalleb commented 9 months ago

samples:

Vscode: vs

Xterm: xterm

FMotalleb commented 9 months ago

The improved functionality now includes compatibility with a variety of themes such as “vscode,” “xterm,” “ubuntu,” “eclipse,” “mirc,” “putty,” “winxp,” “terminal,” “win10,” and “win_power-shell.” Additionally, there’s flexibility to personalize each color within any chosen theme. This is achieved by using the custom-theme- prefix followed by the color name (for example, fg, bg, white, etc.). The custom-defined colors will take precedence, replacing the default colors of the selected theme.

fdncred commented 9 months ago

I'm not really sure what the point of a theme is. I just want it to look like my screen. I'll try it out in a little while.

FMotalleb commented 9 months ago

I get your point; however, since the plugins API doesn’t grant me the capability to get your existing shell theme, this method allows you to customize the output to your preference.

fdncred commented 9 months ago

I understand now. I do think there's a way to query the terminal and get colors. I've seen it somewhere but maybe not all terminals support it.

fdncred commented 9 months ago

I wrote this little thing tonight. It doesn't work perfectly because someone broke the input command but if you run this custom command and hit enter 16 times, and your terminal supports this query, you'll get an output like this below. At least this is how it works on Wezterm on MacOS.

def get-terminal-colors [] {
    let color_00 = (input $"(ansi -o '4;0;?')(ansi st)" --bytes-until-any "\e\\" -s) 
    let color_01 = (input $"(ansi -o '4;1;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_02 = (input $"(ansi -o '4;2;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_03 = (input $"(ansi -o '4;3;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_04 = (input $"(ansi -o '4;4;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_05 = (input $"(ansi -o '4;5;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_06 = (input $"(ansi -o '4;6;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_07 = (input $"(ansi -o '4;7;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_08 = (input $"(ansi -o '4;8;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_09 = (input $"(ansi -o '4;9;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_10 = (input $"(ansi -o '4;10;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_11 = (input $"(ansi -o '4;11;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_12 = (input $"(ansi -o '4;12;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_13 = (input $"(ansi -o '4;13;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_14 = (input $"(ansi -o '4;14;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_15 = (input $"(ansi -o '4;15;?')(ansi st)" --bytes-until-any "\e\\" -s)
    # goes through 4;255;?
    [
        $color_00
        $color_01
        $color_02
        $color_03
        $color_04
        $color_05
        $color_06
        $color_07
        $color_08
        $color_09
        $color_10
        $color_11
        $color_12
        $color_13
        $color_14
        $color_15
    ] | each {|col|
        let rgb = $col | split row : | get 1 | split row /
        let red = ($rgb | get 0 | str substring 0..2)
        let green = ($rgb | get 1 | str substring 0..2)
        let blue = ($rgb | get 2 | str substring 0..2)

        {
            red: $red
            green: $green
            blue: $blue
            hex: $"#($red)($green)($blue)"
            rgb: $"RGB(char lp)($red | into int -r 16), ($green | into int -r 16), ($blue | into int -r 16)(char rp)"
        }
    }
}

image

FMotalleb commented 9 months ago

I'll try to make it work but I don't think i can because of how stdin/out are controlled in nu-plugins

fdncred commented 9 months ago

I tweaked this conversion a little bit so it's a little more accurate with converting 16bits to 8bits.

def get-terminal-colors [] {
    let color_00 = (input $"(ansi -o '4;0;?')(ansi st)" --bytes-until-any "\e\\" -s) 
    let color_01 = (input $"(ansi -o '4;1;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_02 = (input $"(ansi -o '4;2;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_03 = (input $"(ansi -o '4;3;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_04 = (input $"(ansi -o '4;4;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_05 = (input $"(ansi -o '4;5;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_06 = (input $"(ansi -o '4;6;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_07 = (input $"(ansi -o '4;7;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_08 = (input $"(ansi -o '4;8;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_09 = (input $"(ansi -o '4;9;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_10 = (input $"(ansi -o '4;10;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_11 = (input $"(ansi -o '4;11;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_12 = (input $"(ansi -o '4;12;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_13 = (input $"(ansi -o '4;13;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_14 = (input $"(ansi -o '4;14;?')(ansi st)" --bytes-until-any "\e\\" -s)
    let color_15 = (input $"(ansi -o '4;15;?')(ansi st)" --bytes-until-any "\e\\" -s)
    # goes through 4;255;?
    [
        $color_00 $color_01 $color_02 $color_03
        $color_04 $color_05 $color_06 $color_07
        $color_08 $color_09 $color_10 $color_11
        $color_12 $color_13 $color_14 $color_15
    ] | each {|col|
        let rgb = $col | split row : | get 1 | split row /

        # 16bit rgb to 8bit =  0xe7e7 | bits and 0x00ff
        let red = ($"0x($rgb | get 0)" | into int | bits and 0x00ff)
        let green = ($"0x($rgb | get 1)" | into int | bits and 0x00ff)
        let blue = ($"0x($rgb | get 2)" | into int | bits and 0x00ff)
        let red_hx = ($red | fmt).lowerhex | str substring 2..
        let green_hx = ($green | fmt).lowerhex | str substring 2..
        let blue_hx = ($blue | fmt).lowerhex | str substring 2..

        {
            red: $red_hx
            green: $green_hx
            blue: $blue_hx
            hex: $"#($red_hx)($green_hx)($blue_hx)"
            rgb: $"RGB(char lp)($red), ($green), ($blue)(char rp)"
        }
    }
}