Skip to content

Instantly share code, notes, and snippets.

@ksss
Last active September 24, 2020 15:10
Show Gist options
  • Save ksss/332b4eac997575d0026ae598c16178c9 to your computer and use it in GitHub Desktop.
Save ksss/332b4eac997575d0026ae598c16178c9 to your computer and use it in GitHub Desktop.
create pokemon database
/* cat init.sql | sqlite3 pokemon */
drop table if exists status_list;
create table status_list(
jpname VARCHAR(255) primary key,
jpromaji VARCHAR(255) not null,
no integer not null,
h integer not null,
a integer not null,
b integer not null,
c integer not null,
d integer not null,
s integer not null,
sum integer not null
);
#! /usr/bin/env ruby
# curl https://yakkun.com/swsh/stats_list.htm | ruby pokemon.rb | sqlite3 pokemon
require 'nokogiri'
require 'romaji'
Encoding.default_external='euc-jp'
html = $stdin.read
doc = Nokogiri::HTML(html)
doc.css('.stupidtable > tbody > tr > td').each_slice(9) do |line|
no, jpname, h, a, b, c, d, s, sum = line.map { |td| td.children.first.text }
jpromaji = Romaji.kana2romaji(jpname)
values = [
no.to_i,
"\"#{jpname}\"",
"\"#{jpromaji}\"",
h.to_i,
a.to_i,
b.to_i,
c.to_i,
d.to_i,
s.to_i,
sum.to_i,
]
puts "insert into status_list (no, jpname, jpromaji, h, a, b, c, d, s, sum) values (#{values.join(',')}) on conflict(jpromaji) do nothing;"
end
#! /usr/bin/env ruby
require 'io/console'
require 'open3'
sqlite_argument = ARGV[0] || '-list'
class Cursor
def move_top
print "\e[0H"
end
def line_on(i)
print "\e[#{i}G"
end
def clear_line
line_on(0)
print "\e[2K"
end
def clear_after
print "\e[0J"
end
def clear_all
print "\e[2J"
end
end
cursor = Cursor.new
columns = "jpname,jpromaji,h,a,b,c,d,s"
query_template = "select #{columns} from status_list where jpromaji LIKE \"%%%s%%\" limit 10"
cursor.clear_all
buff = []
$stdin.raw do |stdin|
while true
cursor.move_top
cursor.clear_line
print "> #{buff.join}"
case c = stdin.getc
when "\r", "\n"
buff.clear
cursor.clear_after
next
when "\u007F"
# backspace
buff.pop
when "\u0003", "\u0004"
# CTRL+C or CTRL+D
exit 0
else
buff << c
end
puts
query = sprintf(query_template, buff.join)
## debug
# cursor.clear_line
# puts query
cursor.clear_line
puts columns
out, _status = Open3.capture2e("sqlite3 #{sqlite_argument} pokemon", stdin_data: query)
out.each_line do |line|
cursor.clear_line
puts line
end
cursor.clear_after
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment