Skip to content

Port before_v0.60/coloring folder (issue #221) #836

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

Merged
merged 18 commits into from
May 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions before_v0.60/coloring/24bit-1.nu

This file was deleted.

21 changes: 0 additions & 21 deletions before_v0.60/coloring/256-color.sh

This file was deleted.

77 changes: 0 additions & 77 deletions before_v0.60/coloring/256_color_testpattern.nu

This file was deleted.

96 changes: 0 additions & 96 deletions before_v0.60/coloring/256_color_testpattern.sh

This file was deleted.

5 changes: 0 additions & 5 deletions before_v0.60/coloring/README.md

This file was deleted.

22 changes: 0 additions & 22 deletions before_v0.60/coloring/color_and_formatting.sh

This file was deleted.

84 changes: 0 additions & 84 deletions before_v0.60/coloring/color_table.nu

This file was deleted.

35 changes: 0 additions & 35 deletions before_v0.60/coloring/color_tables.nu

This file was deleted.

12 changes: 0 additions & 12 deletions before_v0.60/coloring/color_tables.sh

This file was deleted.

25 changes: 0 additions & 25 deletions before_v0.60/coloring/colors.sh

This file was deleted.

41 changes: 0 additions & 41 deletions before_v0.60/coloring/gradient.nu

This file was deleted.

27 changes: 0 additions & 27 deletions before_v0.60/coloring/gradient.ps1

This file was deleted.

24 changes: 0 additions & 24 deletions before_v0.60/coloring/gradient.py

This file was deleted.

86 changes: 0 additions & 86 deletions before_v0.60/coloring/gradient_40x160.ps1

This file was deleted.

16 changes: 0 additions & 16 deletions before_v0.60/coloring/nu_index_fg.nu

This file was deleted.

30 changes: 0 additions & 30 deletions before_v0.60/coloring/ref_table.nu

This file was deleted.

24 changes: 0 additions & 24 deletions before_v0.60/coloring/table.sh

This file was deleted.

104 changes: 102 additions & 2 deletions modules/coloring/256_color_testpattern.nu
Original file line number Diff line number Diff line change
@@ -52,7 +52,7 @@ def print_run [start:int, amount:int] {

def print_blocks [start:int, end:int, block_cols:int, block_rows:int, blocks_per_line:int] {
let block_length = ($block_cols * $block_rows)
let end = (($end - $start) / (($blocks_per_line) * $block_length))
let end = (($end - $start) / (($blocks_per_line) * $block_length)) | math round
0..<$end | each { |i|
0..<$block_rows | each { |row|
0..<$blocks_per_line | each { |block|
@@ -66,4 +66,104 @@ print (print_run 0 16) # The first 16 colours are spread over the whole spectrum
print "" # Single line
print (print_blocks 16 123 6 6 3) # 6x6x6 colour cube between 16 and 123 inclusive
print (print_blocks 124 231 6 6 3) # 6x6x6 colour cube between 124 and 231 inclusive
print (print_blocks 232 255 12 2 1) # Not 50, but 24 Shades of Grey
print (print_blocks 232 255 12 2 1) # Not 50, but 24 Shades of Grey


# bash:

# #!/bin/bash
#
# # Tom Hale, 2016. MIT Licence.
# # Print out 256 colours, with each number printed in its corresponding colour
# # See http://askubuntu.com/questions/821157/print-a-256-color-test-pattern-in-the-terminal/821163#821163
#
# set -eu # Fail on errors or undeclared variables
#
# printable_colours=256
#
# # Return a colour that contrasts with the given colour
# # Bash only does integer division, so keep it integral
# function contrast_colour {
# local r g b luminance
# colour="$1"
#
# if (( colour < 16 )); then # Initial 16 ANSI colours
# (( colour == 0 )) && printf "15" || printf "0"
# return
# fi
#
# # Greyscale # rgb_R = rgb_G = rgb_B = (number - 232) * 10 + 8
# if (( colour > 231 )); then # Greyscale ramp
# (( colour < 244 )) && printf "15" || printf "0"
# return
# fi
#
# # All other colours:
# # 6x6x6 colour cube = 16 + 36*R + 6*G + B # Where RGB are [0..5]
# # See http://stackoverflow.com/a/27165165/5353461
#
# # r=$(( (colour-16) / 36 ))
# g=$(( ((colour-16) % 36) / 6 ))
# # b=$(( (colour-16) % 6 ))
#
# # If luminance is bright, print number in black, white otherwise.
# # Green contributes 587/1000 to human perceived luminance - ITU R-REC-BT.601
# (( g > 2)) && printf "0" || printf "15"
# return
#
# # Uncomment the below for more precise luminance calculations
#
# # # Calculate percieved brightness
# # # See https://www.w3.org/TR/AERT#color-contrast
# # # and http://www.itu.int/rec/R-REC-BT.601
# # # Luminance is in range 0..5000 as each value is 0..5
# # luminance=$(( (r * 299) + (g * 587) + (b * 114) ))
# # (( $luminance > 2500 )) && printf "0" || printf "15"
# }
#
# # Print a coloured block with the number of that colour
# function print_colour {
# local colour="$1" contrast
# contrast=$(contrast_colour "$1")
# printf "\e[48;5;%sm" "$colour" # Start block of colour
# printf "\e[38;5;%sm%3d" "$contrast" "$colour" # In contrast, print number
# printf "\e[0m " # Reset colour
# }
#
# # Starting at $1, print a run of $2 colours
# function print_run {
# local i
# for (( i = "$1"; i < "$1" + "$2" && i < printable_colours; i++ )) do
# print_colour "$i"
# done
# printf " "
# }
#
# # Print blocks of colours
# function print_blocks {
# local start="$1" i
# local end="$2" # inclusive
# local block_cols="$3"
# local block_rows="$4"
# local blocks_per_line="$5"
# local block_length=$((block_cols * block_rows))
#
# # Print sets of blocks
# for (( i = start; i <= end; i += (blocks_per_line-1) * block_length )) do
# printf "\n" # Space before each set of blocks
# # For each block row
# for (( row = 0; row < block_rows; row++ )) do
# # Print block columns for all blocks on the line
# for (( block = 0; block < blocks_per_line; block++ )) do
# print_run $(( i + (block * block_length) )) "$block_cols"
# done
# (( i += block_cols )) # Prepare to print the next row
# printf "\n"
# done
# done
# }
#
# print_run 0 16 # The first 16 colours are spread over the whole spectrum
# printf "\n"
# print_blocks 16 231 6 6 3 # 6x6x6 colour cube between 16 and 231 inclusive
# print_blocks 232 255 12 2 1 # Not 50, but 24 Shades of Grey
94 changes: 94 additions & 0 deletions modules/coloring/color_table.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
def make_header [hi] {
if $hi == true {
let ansi100m = ('100m' | fill -a l -w 11 -c ' ')
let ansi101m = ('101m' | fill -a l -w 9 -c ' ')
let ansi102m = ('102m' | fill -a l -w 9 -c ' ')
let ansi103m = ('103m' | fill -a l -w 9 -c ' ')
let ansi104m = ('104m' | fill -a l -w 9 -c ' ')
let ansi105m = ('105m' | fill -a l -w 9 -c ' ')
let ansi106m = ('106m' | fill -a l -w 9 -c ' ')
let ansi107m = ('107m' | fill -a l -w 9 -c ' ')
$"(char newline)($ansi100m)($ansi101m)($ansi102m)($ansi103m)($ansi104m)($ansi105m)($ansi106m)($ansi107m)(char newline)"

} else {
let ansi40m = ('40m' | fill -a l -w 10 -c ' ')
let ansi41m = ('41m' | fill -a l -w 8 -c ' ')
let ansi42m = ('42m' | fill -a l -w 8 -c ' ')
let ansi43m = ('43m' | fill -a l -w 8 -c ' ')
let ansi44m = ('44m' | fill -a l -w 8 -c ' ')
let ansi45m = ('45m' | fill -a l -w 8 -c ' ')
let ansi46m = ('46m' | fill -a l -w 8 -c ' ')
let ansi47m = ('47m' | fill -a l -w 8 -c ' ')
$"(char newline)($ansi40m)($ansi41m)($ansi42m)($ansi43m)($ansi44m)($ansi45m)($ansi46m)($ansi47m)(char newline)"
}
}

# mk_header and make_header do the same thing in different ways
# make_header is far easier to read and understand
# mk_header is more convoluted but less repetitive

def mk_header [color_range:range] {
let min_rng = (echo $color_range | math min)
let hi_start_pad = 11
let hi_regular_pad = 9
let lo_start_pad = 10
let lo_regular_pad = 8
echo $color_range | each { |color|
let ansi_color = $"($color)m"
if $color == $min_rng {
if $min_rng == 100 {
($ansi_color | fill -a l -w $hi_start_pad -c ' ')

} else {
($ansi_color | fill -a l -w $lo_start_pad -c ' ')

}

} else {
if $min_rng >= 100 {
($ansi_color | fill -a l -w $hi_regular_pad -c ' ')

} else {
($ansi_color | fill -a l -w $lo_regular_pad -c ' ')

}

}
} | str join
echo (char newline)
}

def color_row_range [num:int bg_rg:range] {
let reset = (ansi reset)
let row_header = $"($num)m ($reset)"
let row_data = (echo $bg_rg | each { |back|
let row_name = $"($num);($back)m"
let ansi_color = (ansi -e $row_name)
$"($ansi_color) ($row_name) ($reset)"
} | append (char newline) | str join)
$"($row_header)($row_data)"
}

def create_color_tables [fg_range:range bg_range:range] {
echo $fg_range | each { |fg|
color_row_range $fg $bg_range
} | str join
}

def color_table [] {
[
# make_header $false
(mk_header 40..47)
(create_color_tables 30..37 40..47)

# put a line between tables
(char newline)

#make_header $true
(mk_header 100..107)
(create_color_tables 90..97 100..107)
] | str join
}

color_table

23 changes: 23 additions & 0 deletions modules/coloring/color_tables.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Bash script
# for x in {0..8}; do
# for i in {30..37}; do
# for a in {40..47}; do
# echo -ne "\e[$x;$i;$a""m\\\e[$x;$i;$a""m\e[0;37;40m "
# done
# echo
# done
# echo
# done
# echo ""

0..8 | each {|x|
let row = (30..37 | each {|i|
let row = (40..47 | each {|a|
let color = $"($x);($i);($a)"
$"(ansi -e $color)m\\e[($color)(ansi -e '0;37;40')m "
} | str join)
$"($row)(char newline)"
} | str join)
$"($row)(char newline)"
} | str join

82 changes: 82 additions & 0 deletions modules/coloring/gradient.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# this script will print a blue gradient on the screen

# We can get the terminal width and height now with term size
# but we like to use the script as a benchmark, so let's keep
# it a constant size for now
let height = 40 # really need to get the terminal height here
let width = 160 # really need to get the terminal width here
let stamp = 'Nu'

def iter_inc [incr mult iter] {
$incr + $mult * $iter
}

seq 0 $height | each {
let row_data = (seq 0 $width | each { |col|
let fgcolor = (iter_inc 2 2 $col)
if $fgcolor > 200 and $fgcolor < 210 {
$"(ansi -e '48;2;0;0;')($fgcolor)m($stamp)(ansi -e '0m')"
} else {
$"(ansi -e '48;2;0;0;')($fgcolor)m(char sp)(ansi -e '0m')"
}
} | str join)
$"($row_data)(char newline)"
} | str join


# python:
#
# #!/usr/bin/env python
# from colors import *
#
# def iter_inc(incr, mult, it):
# return incr + mult * it
#
# height = 40
# width = 160
# stamp = "py"
#
# for line in range(0, height):
# row_data = ""
#
# for col in range(0, width):
# fgcolor = iter_inc(2, 2, col)
# if fgcolor > 200 and fgcolor < 210:
# row_data = row_data + color(stamp, bg='rgb(0, 0, %d)' % fgcolor)
# else:
# fg = fgcolor % 256
# row_data = row_data + color(' ', bg='rgb(0, 0, %d)' % fg)
#
# print(row_data)


# powershell:

# function Set-Cursor {
# [CmdletBinding()]
# param ([int] $x, [int] $y)
# $Host.UI.RawUI.CursorPosition = @{x = $x; y = $y }
# }
#
# function Get-Character {
# [CmdletBinding()]
# param ([int]$index)
# $mystring = ' Trevor Sullivan'
# return $index -ge ($mystring.Length) ? ' ' : $mystring[$index]
# }
#
# function main {
#
# for ($y = 0; $y -le ($host.ui.RawUI.BufferSize.Height - 1); $y++) {
# $Color = 25
# Set-Cursor -x $PSItem -y $y
# 0..($Host.UI.RawUI.BufferSize.Width - 1) | ForEach-Object {
# Write-Host -Object ("`e[48;2;0;0;$Color`m{0}" -f (Get-Character -Index $PSItem)) -NoNewline
# $Color += 2
# }
# }
# Start-Sleep -Seconds 5
# }
#
# main

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
def show_index_colors [] {
let prefix = "48;5;"
echo 1..256 | each { |idx|
let cr = ($"($idx) % 16" | math eval)
1..256 | each { |idx|
let color = $"(ansi -e $prefix)($idx)m"
let padded_number = ($"($idx)" | str lpad -l 3 -c '0')
let padded_number = ($"($idx)" | fill -a l -w 3 -c '0')
let cr = ($idx mod 16)
if $cr == 0 {
$"($color)($padded_number) (ansi -e 0m)(char newline)"
} {
} else {
$"($color)($padded_number) (ansi -e 0m)"
}
} | str collect
} | str join
}

show_index_colors
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
# this script uses foreground ansi index colors to print
# a table of 16 rows by 16 columns where each item is a
# a table of 16 rows by 16 colums where each item is a
# different color
def show_index_colors [] {
let prefix = "38;5;"
echo 1..256 | each { |idx|
let cr = ($"($idx) % 16" | math eval)
let cr = (($idx) mod 16)
let color = ($"(ansi -e $prefix)($idx)m")
let padded_number = ($"($idx)" | str lpad -l 3 -c '0')
let padded_number = ($"($idx)" | fill -a l -w 3 -c '0')

if $cr == 0 {
$"($color)($padded_number) (char newline)"
} {
} else {
$"($color)($padded_number) "
}
} | str collect
} | str join
}

show_index_colors

# one-liner version that just prints
# it all on one line which wraps in
# your terminal
def one_liner [] {
0..255 | each {|fg| [(ansi -e '38;5;') ($fg | into string) 'm' ($fg | into string) ' ']} | flatten | str join
}

#echo 1..256 | each { |idx| echo [(ansi -e '38;5;') (build-string $idx) 'm' (build-string $idx) ' ']} | str collect

show_index_colors
Original file line number Diff line number Diff line change
@@ -7,23 +7,23 @@
# print u"\u001b[0m"

# Foreground Colors
echo 0..16 | each { |col|
print (0..16 | each { |col|
let row = (echo 0..16 | each { |row|
let code = ($col * 16 + $row)
if $code < 256 {
$"(ansi -e '38;5;')($code | into string)m($code | into string | str lpad -l 4 -c ' ')(ansi reset)"
} {} # Do nothing in the else
} | str collect)
$"(ansi -e '38;5;')($code | into string)m($code | into string | fill -a l -w 4 -c ' ')(ansi reset)"
}
} | str join)
$"($row)(char newline)"
} | str collect
} | str join)

# Background Colors
echo 0..16 | each { |col|
print (0..16 | each { |col|
let row = (echo 0..16 | each { |row|
let code = ($col * 16 + $row)
if $code < 256 {
$"(ansi -e '48;5;')($code | into string)m($code | into string | str lpad -l 4 -c ' ')(ansi reset)"
} {} # do nothing in the else
} | str collect)
$"(ansi -e '48;5;')($code | into string)m($code | into string | fill -a l -w 4 -c ' ')(ansi reset)"
}
} | str join)
$"($row)(char newline)"
} | str collect
} | str join)
51 changes: 51 additions & 0 deletions modules/coloring/ref_table.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This script will print a table 8 rows by 36 columns
# of background colors using ansi index coloring

# #!/bin/bash
# echo -en "\n + "
# for i in {0..35}; do
# printf "%2b " $i
# done
# printf "\n\n %3b " 0
# for i in {0..15}; do
# echo -en "\033[48;5;${i}m \033[m "
# done
# #for i in 16 52 88 124 160 196 232; do
# for i in {0..6}; do
# let "i = i*36 +16"
# printf "\n\n %3b " $i
# for j in {0..35}; do
# let "val = i+j"
# echo -en "\033[48;5;${val}m \033[m "
# done
# done
# echo -e "\n"


# This prints the column headers
let nl = (char newline)
let plus = $"($nl) + "
let cols = (seq 0 35 | each { |col| $"($col)" | fill -a l -c ' ' -w 3 } | str join)
print $"($plus)($cols)"

let ansi_bg = (ansi -e '48;5;')
let ansi_reset = (ansi reset)
print $"($nl)($nl)"

# This prints the row headers
let row_header = ' 0 '
let row_data = (seq 0 15 | each { |row|
$"($ansi_bg)($row)m ($ansi_reset)"
} | str join)
print $"($row_header)($row_data)($nl)($nl)"

# This is the meat of the script that prints the little squares of color
seq 0 6 | each { |row_idx|
let r = ($row_idx * 36 + 16)
let row_header = (echo $r | into string -d 0 | fill -a l -c ' ' -w 4)
let row_data = (seq 0 35 | each { |row|
let val = (($r + $row) | into string -d 0)
$"($ansi_bg)($val)m (ansi -e 'm') "
} | str join)
$"($row_header) ($row_data)($nl)($nl)"
} | str join
28 changes: 19 additions & 9 deletions before_v0.60/coloring/sample.nu → modules/coloring/sample.nu
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Background Colors
echo 40..47 100..107 49 | each { |clbg|
[40..47 100..107 49] | each { flatten } | flatten | each { |clbg|
# Foreground Colors
echo 30..37 90..97 39 | each { |clfg|
[30..37 90..97 39] | each { flatten } | flatten | each { |clfg|
# 0 Normal
# 1 Bold or increased intensity
# 2 Faint or decreased intensity
@@ -12,15 +12,25 @@ echo 40..47 100..107 49 | each { |clbg|
# 7 Reverse Video
# 8 Conceal (not widely supported)
# 9 Strike-through
let row = (echo 0..9 | each { |attr|
let row = (0..9 | each { |attr|
let ansi_str = $"($attr);($clbg);($clfg)m"
$"(ansi -e $ansi_str) ($ansi_str) (ansi reset)"
} | str collect)
$"($row)(char newline)" | autoview
} | str collect
} | str collect
} | str join)
$"($row)(char newline)"
} | str join
} | str join

# Bash Script

# #!/bin/bash
#
# # This program is free software. It comes without any warranty, to
# # the extent permitted by applicable law. You can redistribute it
# # and/or modify it under the terms of the Do What The Fuck You Want
# # To Public License, Version 2, as published by Sam Hocevar. See
# # http://sam.zoy.org/wtfpl/COPYING for more details.
#
# #Background
# for clbg in {40..47} {100..107} 49 ; do
# #Foreground
# for clfg in {30..37} {90..97} 39 ; do
@@ -32,5 +42,5 @@ echo 40..47 100..107 49 | each { |clbg|
# echo #Newline
# done
# done

# exit 0
#
# exit 0
File renamed without changes.