Skip to content

Instantly share code, notes, and snippets.

@katafrakt
Last active December 13, 2017 09:53
Show Gist options
  • Save katafrakt/e56b5e5b4e19b04b71518a6aac90a227 to your computer and use it in GitHub Desktop.
Save katafrakt/e56b5e5b4e19b04b71518a6aac90a227 to your computer and use it in GitHub Desktop.
This is probably my first real Ruby code ever - it prints specified number of busines cards on a page, using PostScript, RTF or TeX and was an assignment for my university classes. Date on forgotten Dropbox account is March 2009, but I'm pretty sure it's from autumn 2007.
class Vcard
attr_reader :imie, :nazwisko, :firma, :telefon
def show
if not @imie
puts "Dane niezaładowane"
else
puts @imie + " " + @nazwisko
puts @firma
puts @telefon
end
end
def new_data
print "Podaj imię: "
@imie = gets.chomp
print "Podaj nazwisko: "
@nazwisko = gets.chomp
print "Podaj nazwę firmy: "
@firma = gets.chomp
print "Podaj telefon: "
@telefon = gets.chomp
end
def edit_data
puts "Wciśnij ENTER, by pozostawić obecną wartość lub wpisz nową."
print "Imię (" + @imie + "): "
tmp = gets.chomp
@imie = tmp if not tmp == ""
print "Nazwisko (" + @nazwisko + "): "
tmp = gets.chomp
@nazwisko = tmp if not tmp == ""
print "Firma (" + @firma + "): "
tmp = gets.chomp
@firma = tmp if not tmp == ""
print "Telefon (" + @firma + "): "
tmp = gets.chomp
@telefon = tmp if not tmp == ""
end
end
class Printer
attr_reader :queue, :total_quant
def initialize
@total_quant = 0
@queue = Array.new
end
def add(vc,quant)
@queue << [vc,quant]
@total_quant += quant
end
def undo # cofa ostatnie dodanie do kolejki drukowania
@queue.pop
end
def set(type,filename)
@type = type
@fname = filename
end
def print
self.print_ps if @type == "ps"
self.print_rtf if @type == "rtf"
self.print_tex if @type == "tex"
end
def print_ps
out = File.open(@fname,"w")
out.puts "%!PS-Adobe-3.0 EPSF-30.
%%BoundingBox:0 0 595.28 841.89
/mm {2.8346 mul} bind def
.6 setgray
5 setlinewidth"
@actual_height = 240
even = @total_quant/2
even.times do
out.puts "10 mm " + @actual_height.to_s + " mm moveto
90 mm 0 rlineto
0 50 mm rlineto
-90 mm 0 rlineto
closepath
110 mm " + @actual_height.to_s + " mm moveto
90 mm 0 rlineto
0 50 mm rlineto
-90 mm 0 rlineto
closepath"
@actual_height -= 55
end
out.puts "10 mm " + @actual_height.to_s + " mm moveto
90 mm 0 rlineto
0 50 mm rlineto
-90 mm 0 rlineto
closepath" if @total_quant % 2 == 1
out.puts "stroke
0 setgray"
@actual_height = 270
@actual_width = 55
@queue.each do |vcard|
vcard[1].times do
out.puts "/Verdana findfont
20 scalefont
setfont
" + @actual_width.to_s + " mm " + @actual_height.to_s + " mm moveto
(" + vcard[0].imie + " " + vcard[0].nazwisko + ") dup stringwidth pop 2 div neg 0 rmoveto show
/Times-Bold findfont
15 scalefont
setfont
newpath
" + (@actual_width-10).to_s + " mm " + (@actual_height-8).to_s + " mm moveto
(" + vcard[0].firma + ") show
/Times-Roman findfont
15 scalefont
setfont
newpath
" + (@actual_width-10).to_s + " mm " + (@actual_height-14).to_s + " mm moveto
(Tel. " + vcard[0].telefon + ") show"
if @actual_width == 55
@actual_width = 155
else
@actual_width = 55
@actual_height -= 55
end
end
end
out.puts "showpage
%%EOF"
puts "Zapisano plik " + @fname
fork { exec "evince " + @fname }
out.close
end
def print_rtf
out = File.open(@fname,"w")
out.puts "{\\rtf1\\ansi\\ansicpg1250\\deff0
{\\fonttbl
{\\f0\\roman Verdana;}
{\\f1\\roman Times;}
}
{\\colortbl
\\red0\\green0\\blue0;
\\red0\\green0\\blue255;
}"
even = @total_quant/2
table = Array.new
@queue.each do |vcard|
vcard[1].times {table << vcard[0]}
end
@num = 0
even.times do
out.puts "\\trowd\\trrh2500
\\clbrdrt\\brdrw35\\brdrs\\brdrcf2
\\clbrdrl\\brdrw35\\brdrs\\brdrcf2
\\clbrdrb\\brdrw35\\brdrs\\brdrcf2
\\clbrdrr\\brdrw35\\brdrs\\brdrcf2
\\clvertalc
\\cellx4700
\\cellx4900
\\clbrdrt\\brdrw35\\brdrs\\brdrcf2
\\clbrdrl\\brdrw35\\brdrs\\brdrcf2
\\clbrdrb\\brdrw35\\brdrs\\brdrcf2
\\clbrdrr\\brdrw35\\brdrs\\brdrcf2
\\clvertalc
\\cellx9600
\\pard\\intbl\\qc
\\fs40\\f0\\cf0\\b " + table[@num].imie + " " + table[@num].nazwisko + "\\b0\\line
\\fs30\\f1\\cf1 " + table[@num].firma + "\\cf0\\line
Tel. " + table[@num].telefon + "
\\line
\\cell
\\cell
\\pard\\intbl\\qc
\\fs40\\f0\\cf0\\b " + table[@num+1].imie + " " + table[@num+1].nazwisko + "\\b0\\line
\\fs30\\f1\\cf1 " + table[@num+1].firma + "\\cf0\\line
Tel. " + table[@num+1].telefon + "
\\line
\\cell
\\row
\\trowd\\trrh30
\\cellx9600
\\cell
\\row"
@num += 2
end
out.puts "\\trowd\\trrh2500
\\clbrdrt\\brdrw35\\brdrs\\brdrcf2
\\clbrdrl\\brdrw35\\brdrs\\brdrcf2
\\clbrdrb\\brdrw35\\brdrs\\brdrcf2
\\clbrdrr\\brdrw35\\brdrs\\brdrcf2
\\clvertalc
\\cellx4700
\\pard\\intbl\\qc
\\fs40\\f0\\cf0\\b " + table[@num].imie + " " + table[@num].nazwisko + "\\b0\\line
\\fs30\\f1\\cf1 " + table[@num].firma + "\\cf0\\line
Tel. " + table[@num].telefon + "
\\line
\\cell
\\row" if @total_quant % 2 == 1
out.puts "}"
puts "Zapisano plik " + @fname
out.close
fork {exec "soffice " + @fname}
end
def print_tex
out = File.open(@fname,"w")
out.puts "%& --translate-file=il2-pl
\\documentclass[a4paper,10pt]{mwart}
\\usepackage{polski}
\\begin{document}
\\begin{tabular}{|p{6cm}|p{6cm}|}\\hline"
even = @total_quant/2
table = Array.new
@queue.each do |vcard|
vcard[1].times {table << vcard[0]}
end
@num = 0
even.times do
out.puts "\\begin{center}
\\textbf{ \\\\ " + table[@num].imie + " " + table[@num].nazwisko + "}\\\\
\\textsl{" + table[@num].firma + "}\\\\
Tel. " + table[@num].telefon + "\\\\
\\end{center}
& \\begin{center}
\\textbf{" + table[@num+1].imie + " " + table[@num+1].nazwisko + "}\\\\
\\textsl{" + table[@num+1].firma + "}\\\\
Tel. " + table[@num+1].telefon + "
\\end{center}\\\\
\\hline"
@num += 2
end
out.puts "\\begin{center}
\\textbf{ \\\\ " + table[@num].imie + " " + table[@num].nazwisko + "}\\\\
\\textsl{" + table[@num].firma + "}\\\\
Tel. " + table[@num].telefon + "\\\\
\\end{center}
& \\\\
\\hline" if @total_quant%2 == 1
out.puts "\\end{tabular}
\\end{document}"
puts "Zapisano plik " + @fname
out.close
fork {exec "latex " + @fname + " && evince " + @fname.gsub(/\..+/,"") + ".dvi"}
end
end
class Interface
def initialize
@printer = Printer.new
@vcards = Array.new
end
def load_lang(lang)
lfile = File.open(lang + ".lng","r")
@main_menu = lfile.gets
@print_menu = lfile.gets
@lang_menu = lfile.gets
@obj_menu = lfile.gets
@give_fname = lfile.gets
@wrong_option = lfile.gets
end
def main_menu
puts "\n\nMenu główne:"
puts "1. Kolejka drukowania"
puts "2. Wizytówki"
puts "0. Wyjdź"
answer = gets.chomp
return false if answer == "0"
self.menu_printing if answer == "1"
self.menu_vcards if answer == "2"
return true
end
def menu_printing
loop do
puts "\n\nKolejka drukowania:"
if @printer.total_quant == 0
puts "Kolejka jest aktualnie pusta!"
else
@printer.queue.each do |item|
puts item[0].imie + " " + item[0].nazwisko + ": " + item[1].to_s + " razy"
end
puts "Razem " + @printer.total_quant.to_s + " wizytówek na stronie"
end
puts "\n1. Dodaj wizytówkę do kolejki"
puts "2. Usuń ostatnią wizytówkę z kolejki"
puts "3. Drukuj"
puts "0: Wyjdź do menu głównego"
answer = gets.chomp
break if answer == "0"
if answer == "1"
if @printer.total_quant == 10
puts "Nie możesz dodać więcej wizytówek, najpierw trzeba coś usunąć"
else
self.list_vcards
print "Podaj numer wizytówki, którą chcesz dodać: "
num = gets.chomp
if num.to_i != 0
if @vcards[num.to_i-1]
max = 10 - @printer.total_quant
print "Ile wizytówek wydrukować? (max. " + max.to_s + "): "
quant = gets.chomp
if not quant == "0"
quant = quant.to_i
if quant != 0
if quant > max
puts "Za dużo..."
quant = max
end
puts "Dodaję " + quant.to_s + " wizytówek"
@printer.add(@vcards[num.to_i-1],quant)
else
puts "Błędna wartość."
end
end
else
puts "Nie ma wizytówki o tym numerze."
end
else
puts "Nieprawidłowa wartość."
end
end
end
@printer.undo if answer == "2"
self.print_menu if answer == "3"
end
end
def print_menu
puts "Wybierz format:"
puts "1. PostScript"
puts "2. Rich Text File (RTF)"
puts "3. LaTeX"
puts "inne: Wyjdź do menu głównego"
answer = gets.chomp
case answer
when "1": type = "ps"
when "2": type = "rtf"
when "3": type = "tex"
else return false
end
print "Podaj nazwę pliku docelowego (z rozszerzeniem): "
fname = gets.chomp
if File.exists?(fname)
print "Plik o tej nazwie istnieje. Nadpisać? (T/N) "
answer = gets.chomp
if not answer == "T"
puts "Nie zapisuję więc niczego..."
return false
end
end
@printer.set(type,fname)
puts "Drukowanie rozpoczęte."
@printer.print
end
def menu_vcards
loop do
puts "\n\nWizytówki:"
puts "1. Obejrzyj wizytówki"
puts "2. Edytuj wizytówkę"
puts "3. Dodaj wizytówkę"
puts "4. Usuń ostatnią wizytówkę"
puts "0. Wróć do menu głównego"
answer = gets.chomp
break if answer == "0"
if answer == "1"
if @vcards.empty?
puts "Nie ma czego oglądać - lista jest pusta"
else
@vcards.each do |vcard|
vcard.show
puts "" # oddzielenie
end
end
end
if answer == "2"
self.list_vcards
print "Którą z nich chcesz edytować? "
num = gets.chomp
if num.to_i != 0
if @vcards[num.to_i-1]
@vcards[num.to_i-1].edit_data
else
puts "Nie ma wizytówki o tym numerze."
end
else
puts "Nieprawidłowa wartość."
end
end
if answer == "3"
@vcards << Vcard.new
@vcards.last.new_data
end
if answer == "4"
puts "Czy na pewno chcesz usunąć wizytówkę:"
@vcards.last.show
print "(T/N) "
num = gets.chomp # bo po co definiować dodatkowe zmienne? ;)
if num == "T"
@vcards.pop
else
puts "Nie to nie..."
end
end
end
end
def list_vcards
@num = 1
if @vcards.empty?
puts "Brak wizytówek."
else
@vcards.each do |vcard|
puts @num.to_s + ". " + vcard.imie + " " + vcard.nazwisko
@num += 1
end
end
end
end
iface = Interface.new
while iface.main_menu
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment