Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional Theme Features #925

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
275 changes: 217 additions & 58 deletions themes/mod.nu
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
def "ansi_names" [] {
[
...(ansi --list | get name)
...(ansi --list | get 'short name' | range 133..338)
]
}

def "color_config" [] {
if ($env.config?.color_config? == null) {
error make -u {
msg: "$env.config.color_config is not defined"
}
}

$env.config?.color_config?
| transpose key value
}

# Preview the current nushell theme
export 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 all_ansi_names = (ansi_names)
let color_config = (color_config)

let color_table = ($color_config | each {|row|
if ($row.value | describe | str contains 'closure') {
# get the closure as a string
Expand Down Expand Up @@ -63,69 +81,108 @@ def preview_small [theme: string@"nu-complete list themes"] {
commandline edit --insert $"use themes/themes/($theme).nu; $env.config.color_config = (char lparen)($theme)(char rparen); preview_theme_small | table -e"
}

def get_type_keys [] {
where {|i| $i.key in [
binary
block
bool
cell-path
closure
custom
date
duration
filesize
float
glob
int
list
nothing
range
record
string
]}
}

def get_structure_keys [] {
where {|i| $i.key in [
foreground
cursor
empty
header
hints
leading_trailing_space_bg
row_index
search_result
separator
]}
}

def get_shape_keys [] {
where {|i| $i.key | str starts-with "shape_" }
}

def get_conditional_keys [] {
where {|i| ($i.value | describe) == "closure" }
}

# Preview the current nushell theme, small mode
export def "preview theme small" [] {
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, this will not work for hex colors
let source_code_replaced = ($all_ansi_names | reduce -f $source_code {|it, acc| $acc | str replace $"\\b($it)\\b" $"(ansi $it)($it)(ansi reset)"})
if $row.key == 'date' {
let date_show = ([[date];[((date now) - 30min)] [((date now) - 3hr)] [((date now) - 23hr)] [((date now) - 2day)] [((date now) - 5day)] [((date now) - 4wk)] [((date now) - 10wk)] [((date now) - 100wk)]])
[[key]; [$date_show]]
} else if $row.key == 'bool' {
let bool_show = ([[bool]; [true] [false]])
[[key]; [$bool_show]]
} else if $row.key == 'filesize' {
let filesize_show = ([[filesizes]; [0b] [500kb] [1mb]])
[[key]; [$filesize_show]]
} else {
[[key]; [$source_code_replaced]]
}
} else if ($row.key == 'background') {
[[key]; [$"($row.key) - ($row.value)"]]
} else if ($row.value | describe | str contains 'record') {
[[key]; [$"(ansi ($row.value))($row.key) - ($row.value)(ansi reset)"]]
} else if ($row.value | str starts-with '#') {
[[key]; [$"(ansi ($row.value))($row.key) - ($row.value)(ansi reset)"]]
} else {
[[key]; [$"(ansi ($row.value))($row.key) - ($row.value)(ansi reset)"]]
}
} | flatten)
let color_config = (color_config)

# This draws the table with two tables merged
# 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
let conditionals = ($color_config | get_conditional_keys)
let shapes = ($color_config | get_shape_keys)
let types = ($color_config | get_type_keys)
let structure = ($color_config | get_structure_keys)

# This draws the table with three tables merged
let row_count = (
$color_table
| length
| $in / 3
| math ceil
let conditionals_content = (
$conditionals
| each {|row| format closure --short $row})
| str join "\n"

let types_content = (
$types
| where $it.key not-in $conditionals.key
| each {|row| format basic --short $row }
| str join "\n"
)

let structure_content = (
$structure
| where $it.key not-in $conditionals.key
| each {|row|
match $row.key {
"foreground" => (format foreground_background --short $color_config)
_ => (format basic --short $row)
}
}
| str join "\n"
)

#return ($color_table | group 19)
let shapes_content = (
$shapes
| where $it.key not-in $conditionals.key
| each {|row| format basic --short $row }
| str join "\n"
)

let table1 = ($color_table | chunks $row_count | get 0 | rename "Column 1")
let table2 = ($color_table | chunks $row_count | get 1 | rename "Column 2")
let table3 = ($color_table | chunks $row_count | get 2 | rename "Column 3")
let structure_and_types_content = (
[
$types_content
$structure_content
]
| str join "\n\n\n\n\n"
)

$table1
| merge $table2
| merge $table3
| default '' "Column 3"
| table -e -i false
# Remove heading
[
{
"Shapes": $shapes_content
"Other Types and Structure": $structure_and_types_content
"Conditionally Defined": $conditionals_content
}
]
| table -i false
# Remove header
| str replace -r '^([^\n]+)(\n[^\n]+){2}' '$1'
| print $in
}

# Preview what your terminal theme looks like
Expand Down Expand Up @@ -185,4 +242,106 @@ export def "update terminal" [] {
| str replace --all "\n" ''
| print $in

}
}

def no-newline [] string->string {
$in | str replace -r '\n$' ''
}

def "format foreground_background" [
color_table
--short (-s)
] {
let color_record = ($color_table | transpose -dr)
let fg_bg = {
fg: $color_record.foreground?
bg: $color_record.background?
}
match $short {
false => {
key: "Foreground/Background"
value: $"($color_record.foreground?) on ($color_record.background?)"
}

true => $"(ansi $fg_bg)foreground/background - ($color_record.foreground?) on ($color_record.background?)(ansi reset)"
}
}

def "format basic" [
color_item: record
--short (-s)
] {
match $short {
false => {
key: $color_item.key
value: $"(ansi $color_item.value)($color_item.value)(ansi reset)"
}

true => $"(ansi $color_item.value)($color_item.key) - ($color_item.value)(ansi reset)"
}
}

def "format closure" [
color_item: record
--short (-s)
] {
let demo_value = (
match $color_item.key {
"bool" => [
{"bools": true}
{"bools": false}
]

"date" => [
{"dates": ((date now) - 30min)}
{"dates": ((date now) - 3hr)}
{"dates": ((date now) - 23hr)}
{"dates": ((date now) - 2day)}
{"dates": ((date now) - 5day)}
{"dates": ((date now) - 4wk)}
{"dates": ((date now) - 10wk)}
{"dates": ((date now) - 100wk)}
]

"filesize" => [
{"filesizes": 0b}
{"filesizes": 500kb}
{"filesizes": 1mb}
]

"string" => [
{"strings": "#FF0000"}
{"strings": "#00FF00"}
{"strings": "#0000FF"}
{"strings": "other text"}
]

_ => {
$"($color_item.key) - \(Depends on closure)"
}
}
)

match $short {
true => ($demo_value | table | no-newline)
false => {
key: $color_item.key
value: ($demo_value | table | no-newline)
}
}
}

# Not used
def "format record" [
color_rec: record
--short (-s)
] {
match $short {
true => {
$color_rec
| to nuon
| str replace -a '"' ''
| $"(ansi ($color_rec))($in)(ansi reset)"
}
}
}
Loading
Loading