Repository navigation
Closed
Labels
status:needs-triageAn issue that hasn't had any proper look
Milestone
Description
Describe the bug
I'm trying to write a script that will:
- load a theme
- display the theme colors
- automatically take a screenshot and save it
- clear the screen
- rinse and repeat
What I've noticed is that colors for headers (and maybe other things) do not change colors when requested via a theme change.
preview-theme.nu script
def preview_theme [] {
let ansi_names = (ansi --list | get name)
let all_ansi_names = $ansi_names ++ (ansi --list | get 'short name' | range 133..388)
let color_config = ($env.config.color_config | transpose key value)
let color_table = ($color_config | each {|row|
if ($row.value | describe | str contains 'closure') {
# get the closure as a string
let source_code = (view source ($env.config.color_config | get $row.key) | str replace -a "'" '')
# replace named colors with ansi codes
let source_code_replaced = ($all_ansi_names | reduce -f $source_code {|it, acc| $acc | str replace $"\\b($it)\\b" $"(ansi $it)($it)(ansi reset)"})
# todo, how do we color the closure hex colors?
[[key value]; [$row.key $source_code_replaced]]
} else if ($row.value | describe | str contains 'record') {
[[key value]; [$row.key $"(ansi ($row.value))($row.value)(ansi reset)"]]
} else if ($row.value | str starts-with '#') {
[[key value]; [$row.key $"(ansi ($row.value))($row.value)(ansi reset)"]]
} else {
[[key value]; [$row.key $"(ansi ($row.value))($row.value)(ansi reset)"]]
}
} | flatten)
let row_count = ($color_table | length)
let row_count_half = (($color_table | length) / 2 | math floor)
let table1 = ($color_table | range 0..$row_count_half | rename datatypes dtvals)
let table2 = ($color_table | range $row_count_half..$row_count | rename shapes shpvals)
echo $table1 | merge $table2
}preview-generate-script.nu (a script to generate a script)
let themes = (ls *.nu | first 10)
$themes | each {|theme|
let name = $theme.name
let basename = ($theme.name | path parse | get stem)
echo "clear\n" | save -f -a script.nu
echo $"use ($name)\n" | save -f -a script.nu
echo $"$env.config.color_config = (char lp)($basename)(char rp)\n" | save -f -a script.nu
# echo "source preview-current.nu\n" | save -f -a script.nu
echo "source preview-theme.nu\n" | save -f -a script.nu
echo "sleep 100ms\n" | save -f -a script.nu
echo $"print (char sq)($basename)(char sq)\n" | save -f -a script.nu
# echo "print (main)\n" | save -f -a script.nu
echo "print (preview_theme)\n" | save -f -a script.nu
echo "sleep 250ms\n" | save -f -a script.nu
echo $'c:\apps\MiniCap\MiniCap.exe -captureactivewin -bordershadow -save c:\temp\theme_($name | path parse | get stem)_$date$.png -closeapp -exit(char nl)' | save -f -a script.nu
}preview-screenshot-script.nu
clear
use 3024-day.nu
$env.config.color_config = (3024-day)
source preview-theme.nu
sleep 100ms
print '3024-day'
print (preview_theme)
sleep 250ms
c:\apps\MiniCap\MiniCap.exe -captureactivewin -bordershadow -save c:\temp\theme_3024-day_$date$.png -closeapp -exit
clear
use 3024-night.nu
$env.config.color_config = (3024-night)
source preview-theme.nu
sleep 100ms
print '3024-night'
print (preview_theme)
sleep 250ms
c:\apps\MiniCap\MiniCap.exe -captureactivewin -bordershadow -save c:\temp\theme_3024-night_$date$.png -closeapp -exit
clear
use 3024.nu
$env.config.color_config = (3024)
source preview-theme.nu
sleep 100ms
print '3024'
print (preview_theme)
sleep 250ms
c:\apps\MiniCap\MiniCap.exe -captureactivewin -bordershadow -save c:\temp\theme_3024_$date$.png -closeapp -exit
clear
use abyss.nu
$env.config.color_config = (abyss)
source preview-theme.nu
sleep 100ms
print 'abyss'
print (preview_theme)
sleep 250ms
c:\apps\MiniCap\MiniCap.exe -captureactivewin -bordershadow -save c:\temp\theme_abyss_$date$.png -closeapp -exit
clear
use aci.nu
$env.config.color_config = (aci)
source preview-theme.nu
sleep 100ms
print 'aci'
print (preview_theme)
sleep 250ms
c:\apps\MiniCap\MiniCap.exe -captureactivewin -bordershadow -save c:\temp\theme_aci_$date$.png -closeapp -exit
clear
use aco.nu
$env.config.color_config = (aco)
source preview-theme.nu
sleep 100ms
print 'aco'
print (preview_theme)
sleep 250ms
c:\apps\MiniCap\MiniCap.exe -captureactivewin -bordershadow -save c:\temp\theme_aco_$date$.png -closeapp -exit
clear
use adventuretime.nu
$env.config.color_config = (adventuretime)
source preview-theme.nu
sleep 100ms
print 'adventuretime'
print (preview_theme)
sleep 250ms
c:\apps\MiniCap\MiniCap.exe -captureactivewin -bordershadow -save c:\temp\theme_adventuretime_$date$.png -closeapp -exit
clear
use afterglow.nu
$env.config.color_config = (afterglow)
source preview-theme.nu
sleep 100ms
print 'afterglow'
print (preview_theme)
sleep 250ms
c:\apps\MiniCap\MiniCap.exe -captureactivewin -bordershadow -save c:\temp\theme_afterglow_$date$.png -closeapp -exit
clear
use alien-blood.nu
$env.config.color_config = (alien-blood)
source preview-theme.nu
sleep 100ms
print 'alien-blood'
print (preview_theme)
sleep 250ms
c:\apps\MiniCap\MiniCap.exe -captureactivewin -bordershadow -save c:\temp\theme_alien-blood_$date$.png -closeapp -exit
clear
use alucard.nu
$env.config.color_config = (alucard)
source preview-theme.nu
sleep 100ms
print 'alucard'
print (preview_theme)
sleep 250ms
c:\apps\MiniCap\MiniCap.exe -captureactivewin -bordershadow -save c:\temp\theme_alucard_$date$.png -closeapp -exitHow to reproduce
The screenshot script only works on windows right now.
- clone nu_scripts repo
- cd to nu_scripts/themes/themes
- copy the scripts above into this directory
To preview your current theme:
- source preview-theme.nu
- preview_theme
To generate the screenshots from the current directory (this only does the first 10)
- preview-screenshot-script.nu
Examples of what it's currently generating (yes, it's not working correctly)
3024
3024-day
3024-night
abyss
aci
aco
adventuretime
afterglow
alien-blood
alucard
Notice the column headers and row headers do not change colors. I'm not sure why. I'm guessing a bug in our code somewhere.
Expected behavior
All theme colors to be shown correctly.
Screenshots
No response
Configuration
| key | value |
|---|---|
| version | 0.80.1 |
| branch | main |
| commit_hash | 057de06 |
| build_os | windows-x86_64 |
| build_target | x86_64-pc-windows-msvc |
| rust_version | rustc 1.67.1 (d5a82bbd2 2023-02-07) |
| rust_channel | 1.67.1-x86_64-pc-windows-msvc |
| cargo_version | cargo 1.67.1 (8ecd4f20a 2023-01-10) |
| build_time | 2023-05-17 11:19:17 -05:00 |
| build_rust_channel | release |
| features | default, zip |
| installed_plugins | from eml, from ics, from ini, from parquet, from vcf, gstat, hist, inc, json path, plot, query, query json, query web, query xml, regex, xyplot |
Additional context
No response
Metadata
Metadata
Assignees
Labels
status:needs-triageAn issue that hasn't had any proper look










Activity
fdncred commentedon May 23, 2023
/cc @zhiburt Any idea why these table column headers and row indexes aren't changing colors with this script? If you look at the
row_indexandheaderin the screenshots you'll see that most of them are different colors but the tables do not match. Oh, i just noticed thatseparatorisn't being applied either. It's like the table_theme part of the coloring is being ignored.zhiburt commentedon May 23, 2023
Hi @fdncred
You've put quite an effort into the scripts;
Seems to be OK?
I don't know maybe something in your scripts?
Could you use this instead and verify? (you must enter it twice to see effect)
PS: I did not figured out how to make it a function, seems like
userequires a constant?fdncred commentedon May 24, 2023
@zhiburt I've got updates to the script but it doesn't affect this problem. You're right, that if you run them one at a time in the repl, it seems to work, although sometimes you have to run it twice.
However, if you run the script above
preview-screenshot-script.nu(just comment out the c:\apps\MiniCap\MiniCap.exe part), which switches themes in a script, you should see the problem.This is why i have a script to create a the preview-screenshot-script.nu, because it requires a constant.
zhiburt commentedon May 24, 2023
So I guess I see what you mean.
I think it's a shorter example. (though it has a different cause I guess).
def ctxIssue [] { $env.config.color_config.header = blue; ls } ctxIssueSeems like
merge_delta/merge_envmust be done for blocks, calls? (nu_engine/src/eval.rs).I've approached it a number of times but yet not succeed.
As it's a bit tricky cause being called recursively.
I might be wrong though.
zhiburt commentedon May 24, 2023
Yes seems like I am correct;
At least what worked for me;
For functions you need to remove a callee stack (or merge it later to the caller one).
And then update the engine before print is called (now it updates it only after).
nushell/crates/nu-cli/src/util.rs
Lines 241 to 245 in 74724de
nushell/crates/nu-engine/src/eval.rs
Line 59 in 74724de
I do not provide a PR as I am not sure about the changes (and I haven't cracked a file one, (?has some endless loop because of a engine.clone()?))
It very likely it can be done differently.
fdncred commentedon May 24, 2023
@zhiburt good research. thanks. do you think it's the reason for this too?
if you do this
and then run ctxIssue2 TWICE the second time the changes are reflected.
fdncred commentedon May 24, 2023
@kubouch Do you have time to look at what @zhiburt is saying here and help us understand how to fix it?
zhiburt commentedon May 24, 2023
Sort of.
I believe this patch will make it work from 1 attempt.
(save it to a file and run
git am bugfix.patch).fdncred commentedon May 24, 2023
@zhiburt This patch doesn't seem to fix it when running in a script for me. The header, row_index, and separators are all colored whatever they were with the last theme. Since my default theme has cyan headers, yellow row_index, and gold separators, so do all themes ran from the script. although you can look at the tables and see what they're supposed to be.




If you comment out the C:\apps\minicap\minicap.exe line in the preview-screenshot-script.nu, and put it in the nu_scripts/themes/themes folder, you should be able to reproduce this but without the screenshots.
Preview Theme Script
preview-theme.nu.txt
Preview Generate Script
preview-generate-script.nu.txt
Preview Screenshot Script
preview-screenshot-script.nu.txt
kubouch commentedon May 25, 2023
So the issue is that
does not show
lsoutput with blue column names, right?I think the issue is that inside
crates/nu-command/src/viewers/table.rsthe config seems to be fetched byengine_state.get_config()which ignores any changes to the environment made in the current parse-eval cycle. These are are only in the Stack until the entire evaluation is over and they're merged into the EngineState, but then it's too late.This means that basically any call to
engine_state.get_config()should be changed toengine_state.get_config() + combine with $env.config in the Stack. The only time you'd use plainengine_state.get_config()is when you don't have a runtime Stack, i.e., in the parser. We might have such helper already, if not, then it would be probably a good idea to make something likeStack::get_config(&self, engine_state: &EngineState). You can see a similar idea in theStack::get_env_var(&self, engine_state: &EngineState): We also pass the engine state and look both there and in the stack for the environment variable.zhiburt commentedon May 25, 2023
Hi @kubouch
Personally I'd argue it'd be a bad decision to do it in a leaf functions (down the stack trace).
But in any way it still wont work cause you have a specific stack for function execution which is not merged afterwards (see above messages).
PS: I am not sure how modules work in this regard; but seems to be identical?
You can test it yourself.
fdncred commentedon May 25, 2023
This patch (Subject: [PATCH] patch as noted by @kubouch) doesn't solve the problem for me.
for this command
Merge stack before printing (#9304)
fdncred commentedon May 30, 2023
closed by #9304