#!/usr/bin/env splrun_norl /* * STFL - The Structured Terminal Forms Language/Library * Copyright (C) 2006 Clifford Wolf * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301 USA * * example.spl: A little STFL SPL example */ load "sql"; load "sql_utils"; load "stfl"; var db = sql_connect("sqlite", "example.db"); { var count = sql_value(db, "SELECT count(*) FROM people"); var message = "$count addresses in database"; stfl_run(stfl_create("{vbox tie:c {table{label .border:lrtb " "text:${stfl::message}}}}"), 3000); } function search() { var f = stfl_create(<:> : table : @input#style_normal:bg=blue,fg=black : @input#style_focus:bg=blue,fg=white,attr=bold : label : .expand:0 : .border:lt : .spacer:r : text:"Name:" : input[in_name] : .expand:h : .border:rt : .colspan:2 : text[in_name_val]:"" : tablebr : label : .expand:0 : .border:lb : .spacer:r : text:"E-Mail:" : input[in_email] : .expand:h : .border:b : text[in_email_val]:"" : label : .expand:0 : .border:lrtb : text:"Press ENTER to search" : tablebr : list[result] : pos_name[result_rowid]: : style_focus:bg=blue,fg=white,attr=bold : style_selected:bg=blue,fg=black : .colspan:3 : .border:lrtb ); function perform_search() { var in_name = stfl_get(f, "in_name_val"); var in_email = stfl_get(f, "in_email_val"); var tuples = sql(db, <:> : SELECT rowid, * FROM people WHERE 1 : AND name LIKE ${sql::"%$in_name%"} : AND email LIKE ${sql::"%$in_email%"} : LIMIT 50 ); stfl_modify(f, "result", "replace_inner", <:> : list : listitem[rowid_${t.rowid}] : text:${stfl::t['name']}" <"${stfl::t['email']}">" ); } while (1) { switch { var event = stfl_run(f, 0); case event ~== "ENTER" and stfl_get_focus(f) =~ /^in_/: perform_search(); case event ~== "ENTER" and stfl_get_focus(f) ~== "result": if (stfl_get(f, "result_rowid") =~ /rowid_(\d+)/) edit($1); perform_search(); case event ~== "ESC": stfl_reset(); return; } } } function edit(rowid) { var tuple = sql_tuple(db, "SELECT * FROM people WHERE rowid = $rowid"); var fields = [ name: "Name", email: "E-Mail", web: "WWW Address", description: "Description", addr: "Address" ]; var f = stfl_create(<:> : table : @input#style_normal:bg=blue,fg=black : @input#style_focus:bg=blue,fg=white,attr=bold : @input#.expand:h : @input#.border:rtb : @label#.expand:0 : @label#.border:ltb : @label#.spacer:r : label : text:${stfl::fields[i]}: : input[in_$i] : text[in_${i}_val]:${stfl::tuple[i]} : tablebr : vbox : .colspan:2 : .border:lrtb : label .tie:r : text:"Press ENTER to save and ESC to cancel" ); while (1) { switch { var event = stfl_run(f, 0); case event ~== "ENTER": sql(db, <:> : UPDATE people SET : $i = ${sql::stfl_get(f, "in_${i}_val")} : ${ $] ? "" : ","} : WHERE rowid = $rowid ); return; case event ~== "ESC": return; } } } search();