MoonScript - Agenda telefônica semifuncional em 101 linhas
Publicado por Ramon (última atualização em 02/12/2013)
[ Hits: 3.538 ]
O MoonScript é uma "linguagem" de programação, que faz cross-compile (ou seja, traduz seu código) para Lua (sim, a linguagem brasileira, usada no WoW). Porém, uma maneira de descrevê-la melhor é uma linguagem que faz algo como o CoffeeScript faz com o JavaScript.
Para testar a funcionalidade da linguagem, a testei fazendo uma mini-agenda-telefônica (quebrada, só adiciona contatos...) para ver se ela é "boa mesmo", e rendeu em surpreendentes 101 linhas um programa funcional.
Ah, só mais uma coisa. Olhe a TreeView. Quem já programou em GTK sem o Builder, e usou TreeViews, alguma vez ficará boquiaberto.
require "moon.all"
lgi = require "lgi"
import Gtk, GObject from lgi
require "lsqlite3"
-- General program logic
PhoneColumns = { NAME: 1, NUMBER: 2 }
database = sqlite3.open "database.db"
assert(database\exec("CREATE TABLE IF NOT EXISTS phones(name text, number text)") == sqlite3.OK)
-- Insert Window Logic
-- Yep, classes - neat!
class InsertWindow
new: (db, on_add_callback) =>
@name_input = Gtk.Entry!
@number_input = Gtk.Entry!
@database = db
@on_add_callback = on_add_callback
@window = Gtk.Window {
title: "Insert",
Gtk.Grid {
orientation: Gtk.Orientation.VERTICAL,
Gtk.Grid {
orientation: Gtk.Orientation.HORIZONTAL,
Gtk.Label {label: "Name: "},
@name_input
},
Gtk.Grid {
orientation: Gtk.Orientation.HORIZONTAL,
Gtk.Label {label: "Number: "},
@number_input
},
Gtk.Button {
label: "Insert",
on_pressed: ->
stmt = @database\prepare "INSERT INTO phones VALUES(?, ?)"
stmt\bind(1, @name_input.text)
stmt\bind(2, @number_input.text)
stmt\step!
stmt\reset!
@window\hide!
@on_add_callback!
}
}
}
clear: =>
@name_input.text = ""
@number_input.text = ""
-- Main Window Logic
fill_liststore_with_data = (database, store using nil) ->
store\clear!
for row in database\nrows("SELECT * FROM phones")
store\append {
[PhoneColumns.NAME]: row.name,
[PhoneColumns.NUMBER]: row.number
}
store = Gtk.ListStore.new {
[PhoneColumns.NAME]: GObject.Type.STRING,
[PhoneColumns.NUMBER]: GObject.Type.STRING
}
insert_window = InsertWindow(database, -> fill_liststore_with_data(database, store))
fill_liststore_with_data(database, store)
view = Gtk.TreeView {
model: store,
Gtk.TreeViewColumn {
title: "Name",
{ Gtk.CellRendererText({}), { text: PhoneColumns.NAME } }
},
Gtk.TreeViewColumn {
title: "Number",
{ Gtk.CellRendererText({}), { text: PhoneColumns.NUMBER } }
}
}
button = Gtk.Button {
label: "Insert contact",
on_pressed: ->
insert_window.window\show_all()
}
window = Gtk.Window {
title: "Test",
on_destroy: Gtk.main_quit,
Gtk.Grid {
orientation: Gtk.Orientation.VERTICAL,
view,
button
}
}
window\show_all!
Gtk.main!
Método de Gauss-Seidel em SCILAB
trocar permissão, dono e grupo de arquivos ou diretórios
Nenhum comentário foi encontrado.
Monitorando o Preço do Bitcoin ou sua Cripto Favorita em Tempo Real com um Widget Flutuante
IA Turbina o Desktop Linux enquanto distros renovam forças
Como extrair chaves TOTP 2FA a partir de QRCODE (Google Authenticator)
Ativando e usando "zoom" no ambiente Cinnamon
Vídeo Nostálgico de Instalação do Conectiva Linux 9
Como realizar um ataque de força bruta para desobrir senhas?
Thinkpads são bons mesmo ?! (2)
Estou tentando ser legalista, mas tá complicado! (8)
Queda no serviços da Cloudflare, alguns sites estão fora do ar. (1)









