1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-02 07:07:46 +00:00

feat: added coloring/hl.nu, a highlighting module (#299)

This commit is contained in:
Neur1n 2022-09-27 19:07:13 +08:00 committed by GitHub
parent 7a7051e9a7
commit 646dec4406
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

56
coloring/hl.nu Normal file
View file

@ -0,0 +1,56 @@
export def combine [txt: string, fg: record, bg: record] {
{
txt: $txt,
fg: $fg.fg,
bg: $bg.fg,
bli: ($fg.bli || $bg.bli),
bol: ($fg.bol || $bg.bol),
dim: ($fg.dim || $bg.dim),
hid: ($fg.hid || $bg.hid),
ita: ($fg.ita || $bg.ita),
rev: ($fg.rev || $bg.rev),
stk: ($fg.stk || $bg.stk),
und: ($fg.und || $bg.und)
}
}
export def create [txt: string,
fg = "n", bg = "n",
bli = false, bol = false, dim = false, hid = false,
ita = false, rev = false, stk = false, und = false] {
{
txt: $txt,
fg: $fg,
bg: $bg,
bli: $bli,
bol: $bol,
dim: $dim,
hid: $hid,
ita: $ita,
rev: $rev,
stk: $stk,
und: $und
}
}
export def render [obj: record] {
let attr = ""
let attr = $"($attr)(if $obj.bli {'l'})"
let attr = $"($attr)(if $obj.bol {'b'})"
let attr = $"($attr)(if $obj.dim {'d'})"
let attr = $"($attr)(if $obj.hid {'h'})"
let attr = $"($attr)(if $obj.ita {'i'})"
let attr = $"($attr)(if $obj.rev {'r'})"
let attr = $"($attr)(if $obj.stk {'s'})"
let attr = $"($attr)(if $obj.und {'u'})"
let color = {fg: $obj.fg, bg: $obj.bg, attr: $attr}
$"(ansi $color)($obj.txt)(ansi reset)"
}
export def reverse [obj: record] {
let r = ($obj | update fg $obj.bg)
let r = ($r | update bg $obj.fg)
$r
}