HUZZAH! CURSES WORKS;git push

This commit is contained in:
Greg Gauthier 2025-04-13 17:04:19 +01:00
parent f7474e911b
commit 931f384af2
3 changed files with 196 additions and 0 deletions

89
src/empdisplay.a68 Normal file
View File

@ -0,0 +1,89 @@
PROC main = INT: BEGIN
# Postgres Setup #
FILE db;
STRING result;
STRING conninfo = "dbname=testdb user=gmgauthier password=thx1138gg host=localhost port=5432";
INT status = pq connect db(db, conninfo, result);
IF status /= 0 THEN print(("Connect failed: ", result, newline)); stop FI;
print(("We've initialized the database connection",newline));
# Query Employees #
INT qstatus = pq exec(db, "SELECT employee, employee_id, start_date, salary FROM employees ORDER BY employee_id");
IF qstatus /= 0 THEN
print(("Query failed: ", qstatus, newline));
INT fin1 = pq finish(db);
print(fin1);
stop
FI;
print(("We've loaded the data from the database", newline));
# ncurses Setup #
print(("We're starting curses...",newline));
curses start; # VOID, no return to check #
print(("We've gotten past curses start",newline));
# Border and Title (y=2, x=10, width=40, height=12) #
PROC draw char = (INT y, x, CHAR c) VOID: (
curses move(y, x); curses putchar(c)
);
PROC draw str = (INT y, x, STRING s) VOID: (
FOR i FROM 1 TO UPB s DO
draw char(y, x + i - 1, s[i])
OD
);
print(("We've gotten past the two draw proc declarations",newline));
# Top/Bottom Border #
FOR x FROM 10 TO 49 DO
draw char(2, x, "-"); draw char(13, x, "-")
OD;
# Sides #
FOR y FROM 3 TO 12 DO
draw char(y, 10, "|"); draw char(y, 49, "|")
OD;
# Corners #
draw char(2, 10, "+"); draw char(2, 49, "+");
draw char(13, 10, "+"); draw char(13, 49, "+");
# Title #
draw str(3, 12, "Employee Data");
# Headers (y=5) #
draw str(5, 12, "Name ID Start Salary");
draw str(6, 12, "------ --- ------ ------");
print(("We're at the beginning of the data row loop", newline));
# Data (y=7+) #
INT roz = pq ntuples(db);
print((roz, newline));
FOR i FROM 1 TO roz DO
INT y = 6 + i;
IF pq get value(db, i, 1) = 0 THEN # employee #
draw str(y, 12, result)
FI;
IF pq get value(db, i, 2) = 0 THEN # employee_id #
draw str(y, 19, result)
FI;
IF pq get value(db, i, 3) = 0 THEN # start_date #
draw str(y, 23, result)
FI;
IF pq get value(db, i, 4) = 0 THEN # salary #
draw str(y, 30, result)
FI
OD;
print(("We're at the end of the data row loop",newline));
# Refresh and Wait #
curses refresh;
CHAR ch = curses getchar;
print((ch,newline));
# Cleanup #
curses end;
print(("We've terminated curses",newline));
INT fin2 = pq finish(db);
print(("We've reached program cleanup",newline));
fin2 # success #
END;
main

101
src/empdisplay2.a68 Normal file
View File

@ -0,0 +1,101 @@
PROC main = INT: BEGIN
# Postgres Setup #
FILE db;
STRING result;
STRING conninfo = "dbname=testdb user=gmgauthier password=thx1138gg host=localhost port=5432";
INT status = pq connect db(db, conninfo, result);
IF status /= 0 THEN print(("Connect failed: ", result, newline)); stop FI;
print(("We've initialized the database connection", newline));
# Query Employees #
INT qstatus = pq exec(db, "SELECT employee, employee_id, start_date, salary FROM employees ORDER BY employee_id");
IF qstatus /= 0 THEN
print(("Query failed: ", qstatus, newline));
INT fin1 = pq finish(db);
print((fin1, newline));
stop
FI;
print(("We've loaded the data from the database", newline));
# ncurses Setup #
print(("We're starting curses...", newline));
curses start; # VOID, no return to check #
INT lines = curses lines; INT cols = curses columns; # Check screen init #
IF lines = 0 OR cols = 0 THEN
curses end;
print(("ncurses failed to initialize: lines=", lines, " cols=", cols, newline));
INT fin1 = pq finish(db);
print((fin1, newline)); # suppresses the warning #
stop
FI;
# Clear screen to avoid artifacts #
curses clear;
# Border and Title (y=2, x=10, width=40, height=12) #
PROC draw char = (INT y, x, CHAR c) VOID: (
curses move(y, x); curses putchar(c)
);
PROC draw str = (INT y, x, STRING s) VOID: (
FOR i FROM 1 TO UPB s DO
draw char(y, x + i - 1, s[i])
OD
);
# Top/Bottom Border #
FOR x FROM 10 TO 49 DO
draw char(2, x, "-"); draw char(13, x, "-")
OD;
# Sides #
FOR y FROM 3 TO 12 DO
draw char(y, 10, "|"); draw char(y, 49, "|")
OD;
# Corners #
draw char(2, 10, "+"); draw char(2, 49, "+");
draw char(13, 10, "+"); draw char(13, 49, "+");
# Title #
draw str(3, 12, "Employee Data");
# Headers (y=5) #
draw str(5, 12, "Name ID Start Salary");
draw str(6, 12, "------ --- ------ ------");
# Data (y=7+) #
INT roz = pq ntuples(db);
FOR i FROM 1 TO roz DO
INT y = 6 + i;
IF pq get value(db, i, 1) = 0 THEN # employee #
draw str(y, 12, result)
FI;
IF pq get value(db, i, 2) = 0 THEN # employee_id #
draw str(y, 19, result)
FI;
IF pq get value(db, i, 3) = 0 THEN # start_date #
draw str(y, 23, result)
FI;
IF pq get value(db, i, 4) = 0 THEN # salary #
draw str(y, 30, result)
FI
OD;
# Refresh and Wait for 'q' to Quit #
curses refresh;
draw str(15, 10, "Press 'q' to quit");
curses refresh;
CHAR ch;
DO
ch := curses getchar
UNTIL ch = "q"
OD;
# Cleanup #
curses end;
print(("We've terminated curses", newline));
INT fin2 = pq finish(db);
print(("We've reached program cleanup", newline));
fin2 # success, return pq finish status #
END;
main

6
src/employees.csv Normal file
View File

@ -0,0 +1,6 @@
employee,employee_id,start_date,salary
jim,1,2010,123456
joe,2,2011,99876
jack,3,2012,75246
john,4,2013,66123
jeff,5,2014,50501
1 employee employee_id start_date salary
2 jim 1 2010 123456
3 joe 2 2011 99876
4 jack 3 2012 75246
5 john 4 2013 66123
6 jeff 5 2014 50501