Skip to content

Instantly share code, notes, and snippets.

@tompng
tompng / complex_pow.rb
Last active November 9, 2024 05:48
complex pow in 1024bit
def complex_fixed_point_1024bits_pow(x, n, bits: 1024)
ans = base = 1 << bits
x2 = Complex.rect(*x.rect.map{(_1.to_r * base).round})
mult = ->(a, b) { Complex.rect(*(a * b).rect.map { _1 / base }) }
n.digits(2).each do |a|
ans = mult[ans, x2] if a == 1
x2 = mult[x2, x2]
end
ans.fdiv base
end
@tompng
tompng / term_value.md
Last active October 18, 2024 17:54
List of Environment variable TERM

TERM value that has different kcuu1|kcud1|kcuf1|kcub1|khome|kend|kpp|knp|kdch1 value from the one Reline registers when terminfo is not available

TERM count values
aaa-60 8 \e[A \e[B \e[C \e[D \e[H - - - \e[P
aixterm 2 \e[A \e[B \e[C \e[D \e[H \e[146q \e[150q \e[154q \e[P
cons25 17 \e[A \e[B \e[C \e[D \e[H \e[F \e[I \e[G \x7F
d210-dg 2 \x17 \x1A \x18 \x19 \b - - -
mach 19 \e[A \e[B \e[C \e[D \e[H \e[Y \e[V \e[U \e[9
qansi 35 \e[A \e[B \e[C \e[D \e[H \e[Y \e[V \e[U \e[P
require 'prism'
require 'ripper'
chars = %w[1 % = a b == * . ( ) , % ? ** & [ ] { } | ; : => ~ ! ^ || && '' .. ... < > + $a @a] + [' ', "\n", ' if ', ' and ',' rescue ', ' in ']
p seed: seed = rand(10000)
srand seed
$VERBOSE=nil
(3..10).each do |n|
[chars.size**n, 100000].min.times do |i|
code = n.times.map{chars.sample}.join
p [n, i] if i % 1000 == 0
@tompng
tompng / stickynote_shadow.html
Created September 12, 2024 08:32
stickynote shadow
<canvas>
<script>
ctx = document.querySelector('canvas').getContext('2d')
ctx.clearRect(0, 0, 1000, 1000)
const blur = 12
ctx.shadowBlur = blur
ctx.shadowColor = 'black'
ctx.shadowOffsetX = blur
ctx.shadowOffsetY = blur
const x = 20
@tompng
tompng / tree.rb
Last active September 11, 2024 10:44
Tree rendering
require 'chunky_png'
case ARGV[0]
when 'red'
FLOOR_COLOR = [0xff, 0x80, 0x80]
LEAF_COLOR = 0xff0044
TRUNK_COLOR = 0xff8888
RAY_COLOR = [2, 1, 1]
LIGHT_THETA = 0.1
when 'green'
@tompng
tompng / measure_width.rb
Created August 20, 2024 16:44
terminal emulator measure result
codepoints = File.read('EastAsianWidth.txt').scan(/^[0-9A-F]{4,6}/).map { _1.to_i(16) }
widths = codepoints.map do |cp|
c = cp.chr('utf-8') rescue (next nil)
print "\e[H\e[K#{c}\e[6n"
STDOUT.flush
/\e\[\d+;(\d+)R/ =~ STDIN.raw { STDIN.readpartial(1000) }
$1.to_i - 1
end
codepoints.zip(widths).reject {
!_2 || Reline::Unicode.get_mbchar_width(''<<_1) == _2 || ('a'<<_1).grapheme_clusters.size == 1
<body>
<script>
const width = 1600
const height = 900
function f(x) {
return 0.48+(-Math.cos(12*x+3*Math.sin(6*x-4)/4-1)*(1.1-x)+(3*x-(2*x-1)**2)*0.5)/20
}
function g(x, a) {
return (
Math.sin(12.4*x-a)+Math.sin(14.3*x+a)+
require 'io/console'
require 'pty'
command = ARGV.join(' ')
if command.empty?
puts 'Error: Command not specified.'
puts "Usage: `ruby #{File.basename __FILE__} options -- command`"
exit
end
srand 0
6.times do |index|
fg = "##{3.times.map{"%02x" % rand(0..100)}.join}"
bg = "##{3.times.map{"%02x" % rand(150..255)}.join}"
face = :"face#{index}"
Reline::Face.config(face) do
_1.define(:default, foreground: fg, background: bg)
end
pointClass = Data.define :x, :y
prev = nil
@tompng
tompng / reline_editor.rb
Created August 5, 2024 09:35
Text editor using reline
require 'irb'
require 'reline'
$status = nil
Reline::Face.config(:editor_status) do |face|
face.define :default, foreground: :white, background: :blue
end
Reline.add_dialog_proc :editor_status, ->(*) do
if (message = $status)
$status = nil