Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created November 20, 2015 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/78b1d1fb503825128958 to your computer and use it in GitHub Desktop.
Save JoshCheek/78b1d1fb503825128958 to your computer and use it in GitHub Desktop.
How to use zlib
s = 'require"io/console";puts"\e[?25l\e[?1000h";at_exit{puts"\e[?25h\e[?1000l"};x=y=ax=ay=0;dy=dx=0.5;h,w,e,fg=*$>.winsize,!7,7;def r(n,v,i=(3*Math.tanh(v)).to_i+2)"\e[38;5;16m#{[196,208,184,38,21].map{|n|"\e[48;5;#{n}m"}.map.with_index{|c,j|c+(j==i&&n||?-)}*""}\e[40;37m %6.2f"%v;end;Thread.new{$stdin.raw{case$stdin.readpartial(20);when"\e[A";ay-=0.1;when"\e[B";ay+=0.1;when"\e[C";ax+=0.1;when"\e[D";ax-=0.1;when"\3";e=!e;when/\e\[M (.)(.)/;dx,dy,x,y=0,0,*[$1,$2].map{|n|n.force_encoding(Encoding::ASCII_8BIT).ord-32};when/^\d/;fg=$&;end until e}};(sleep 0.05;dx+=ax*0.1;dy+=ay*0.1;x+=dx/2;y+=dy/2;y,dy=y<1?[1,-dy*0.8]:y>h ?[h,-dy*0.8]:[y,dy];x,dx=x<1?[1,-dx*0.8]:x+1>w ?[w-2,-dx*0.8]:[x,dx];$><<"\e[40m\e[H\e[2J#{r ?x,dx} x-velocity\r\n#{r ?y,dy} y-velocity\r\n#{r ?x,ax} x-accel\r\n#{r ?y,ay} y-accel\e[#{y.to_i};#{x.to_i}H\e[4#{fg}m ")until e'
# ===== Default =====
require 'zlib'
Zlib::Deflate.deflate(s).length # => 546
s.length # => 843
# ===== Playing with options =====
compression = Zlib::BEST_COMPRESSION
# Zlib::DEFAULT_COMPRESSION
# Zlib::NO_COMPRESSION
# Zlib::BEST_SPEED
# Zlib::BEST_COMPRESSION
window_bits = Zlib::MAX_WBITS
# a number 7 - 15 chars
mem_level = Zlib::MAX_MEM_LEVEL
# Zlib::DEF_MEM_LEVEL
# Zlib::MAX_MEM_LEVEL
strategy = Zlib::DEFAULT_STRATEGY
# Zlib::DEFAULT_STRATEGY | For normal data
# Zlib::FILTERED | For data produced by a filter or predictor
# Zlib::FIXED | Prevents dynamic Huffman codes
# Zlib::HUFFMAN_ONLY | Prevents string matching
# Zlib::RLE | Designed for better compression of PNG image data
# ===== Well, I couldn't beat the default =====
deflate = Zlib::Deflate.new compression, window_bits, mem_level, strategy
deflated = deflate.deflate(s, Zlib::FINISH)
deflate.close
deflated.length # => 546
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment