Skip to content

Instantly share code, notes, and snippets.

@tompng
tompng / reline_debug_visualizer.rb
Last active February 9, 2024 19:10
Minimal terminal emulator that only supports Reline's escape sequences
require 'pty'
require 'io/console'
if ARGV.empty?
puts <<~EOS
Reline Visualizer (Minimal terminal emulator that only supports Reline's escape sequences)
Usage: ruby #{__FILE__} <command>'
ruby #{__FILE__} irb
ruby #{__FILE__} bash
ruby #{__FILE__} zsh
ruby #{__FILE__} ruby -I path/to/reline/lib -I path/to/irb/lib path/to/irb/exe/irb
@tompng
tompng / terminal_color_preview.rb
Last active January 28, 2024 11:34
Preview terminal background and foreground
raise 'COLORTERM required' unless ENV['COLORTERM']
# Color table from https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit
env_names = %w[VGA WinXP PowerShell6 VSCode Win10 Terminal.app PuTTY mIRC xterm Ubuntu Eclipse]
env_colors = <<COLORS.lines.map{|l|l.split(' ').map{_1.scan(/\d+/).join(';')}}.transpose
0,0,0 0,0,0 0,0,0 12,12,12 0,0,0 0,0,0 0,0,0 0,0,0 1,1,1 0,0,0
170,0,0 128,0,0 205,49,49 197,15,31 153,0,0 187,0,0 127,0,0 205,0,0 222,56,43 205,0,0
0,170,0 0,128,0 13,188,121 19,161,14 0,166,0 0,187,0 0,147,0 0,205,0 57,181,74 0,205,0
170,85,0 128,128,0 229,229,16 193,156,0 153,153,0 187,187,0 252,127,0 205,205,0 255,199,6 205,205,0
0,0,170 0,0,128 36,114,200 0,55,218 0,0,178 0,0,187 0,0,127 0,0,238 0,111,184 0,0,238
class << ($colored_stdout = STDOUT.dup)
# Flip this if you are using bright theme
BACKGROUND = [0, 0, 0]
FOREGROUND = [255, 255, 255]
RANDOMIZE_RANGE = -32..32
def rand_color(rgb, bg: false)
"#{bg ? 48 : 38};2;#{rgb.map { (_1 + rand(RANDOMIZE_RANGE)).clamp(0, 0xff) }.join(';')}"
end
@tompng
tompng / ruby_c_irb.rb
Created January 19, 2024 17:07
ruby interactive shell that uses `ruby -c`
class TemrinationChecker
def initialize
@prelaunched = nil
@code = ''
@current = new_session
end
def new_session
IO.popen(['ruby', '-c'], 'r+', err: [:child, :out])
end
  • 極端に長い行の貼り付け・編集などのパフォーマンスが悪い

    • 長い行数を貼り付けるよりも利用頻度高そう。jsonを貼り付けるとか。どこがボトルネックか測る
  • Unicode

    • split_by_widthはheightを返す必要もないし配列の偶数番めにnilを含める必要もない
    • get_prev_mbchar とか、unicode.rbでやるべきことか怪しいような
    • get_nth_prev_mbchar とかにしないとvi_argが大きい場合のパフォーマンスが悪い
    • get_mbchar_widthの高速化をどこまでやるか(あと2段階くらい変身を残してる)効果がどれくらいあるか測る
  • GeneralIO

class PseudoTerminalIO
def initialize(fiber)
@fiber = fiber
@buffer = []
end
def close
@buffer << nil
@closed = true
def dump(s)
dumped = s == eval("'#{s}'") && s !~ /[\C-a-\C-z]/ ? "'#{s}'" : s.dump.gsub(/\\x\d\d/){
"\\C-"+('a'.ord + _1[2..].to_i(16) - 1).chr
}
if dumped =~ /\u3099/
dumped.gsub!("\u3099", "\\u3099")
dumped = ?" + dumped[1...-1] + ?" if dumped[0] == ?'
end
dumped
end
@tompng
tompng / regexp_test.rb
Created December 27, 2023 12:05
regexp
class Generator
def generate_paren(level)
regexp, producer = multi(level - 1)
[['(', '(?:', '(?<a>'].sample + regexp + ')', producer]
end
ALPHABET = ('a'..'z').to_a
def generate_alphabet(_level)
c = ALPHABET.sample
require 'matrix'
module RegexpMatrixSolver
refine Array do
def ~
keys = map(&:first).flat_map(&:keys).uniq
matrix = []
binding = first.last
vec = map{_2}
each do |values, rhs|
matrix << keys.map { values[_1] || 0 }
def run
$VERBOSE=nil
tokens = %w[1 + * ** a in ( ) [ ] = . .. => <<]
cnt = 0
output = {}
(2..8).each do |n|
cnt = 0
tokens.repeated_permutation(n).each do |tokens|
code = tokens.join('') # or join(' ')
# Ruby's bug already fixed in 3.3