load dimensions dynamically from config

This commit is contained in:
~d6 2023-02-11 19:59:36 -05:00
parent 6ce966ce1d
commit 01b86bdabd
2 changed files with 74 additions and 19 deletions

29
term.py
View File

@ -7,6 +7,30 @@ import sys
import struct import struct
import termios import termios
# return dimensions (cols x rows)
def load_dimensions():
# default to 80x24
dims = 80, 24
if not os.path.exists('.theme'):
return dims
try:
with open('.theme', 'rb') as f:
data = f.read(8)
if len(data) < 8:
return dims
vals = struct.unpack('HHHBB', data)
x, y = int(vals[3]), int(vals[4])
if x < 80 or y < 24 or x > 200 or y > 100:
print('invalid width/height: %d/%d' % (x, y))
sys.exit(1)
dims = x, y
except Exception(e):
print('error in .theme file: %s' % e)
sys.exit(1)
return dims
def main(): def main():
# check usage # check usage
@ -17,6 +41,9 @@ def main():
print(' ROM: the rom file to launch') print(' ROM: the rom file to launch')
sys.exit(1) sys.exit(1)
# either detect dimensions from theme or use 80x24
cols, rows = load_dimensions()
# path to rom to run # path to rom to run
rom = args[0] rom = args[0]
@ -30,8 +57,6 @@ def main():
os.execvpe('bash', ['bash'], env) os.execvpe('bash', ['bash'], env)
else: else:
# set the terminal size # set the terminal size
###cols, rows = 79, 40
cols, rows = 80, 24
size = struct.pack("HHHH", rows, cols, 8, 12) size = struct.pack("HHHH", rows, cols, 8, 12)
fcntl.ioctl(fd, termios.TIOCSWINSZ, size) fcntl.ioctl(fd, termios.TIOCSWINSZ, size)

View File

@ -10,7 +10,6 @@
( b. on insert ) ( b. on insert )
( c. etc. ) ( c. etc. )
( 6. add more ansi control seqs ) ( 6. add more ansi control seqs )
( 7. stop hard coding terminal size in both term.py and term.tal )
( 8. key repeat - not possible in general though ) ( 8. key repeat - not possible in general though )
( 9. support shift+arrow and alt+arrow ) ( 9. support shift+arrow and alt+arrow )
( 10. crawl has screen-clearing issues ) ( 10. crawl has screen-clearing issues )
@ -130,13 +129,8 @@
( 80 cols x 24 rows + 1 col for padding ) ( 80 cols x 24 rows + 1 col for padding )
#0018 .rows STZ2 #0018 .rows STZ2
#0051 .cols STZ2 #0051 .cols STZ2
( #0024 .rows STZ2
( set col-bytes, frequently needed ) #005b .cols STZ2 )
.cols LDZ2 DUP2 ADD2 .col-bytes STZ2
( set max row/col )
.rows LDZ2 #0001 SUB2 .max-y STZ2
.cols LDZ2 #0001 SUB2 .max-x STZ2
( set initial cursor ) ( set initial cursor )
#0000 .cur-x STZ2 #0000 .cur-x STZ2
@ -148,16 +142,15 @@
( confirm no buttons pressed yet ) ( confirm no buttons pressed yet )
#00 .lastkey STZ #00 .lastkey STZ
( set screen height/width based on rows/cols )
.cols LDZ2 #30 SFT2 ( width ) #0010 ADD2 .Screen/w DEO2
.rows LDZ2 #000c MUL2 ( height ) #0010 ADD2 .Screen/h DEO2
( set colors ) ( set colors )
#07bf .System/r DEO2 #07bf .System/r DEO2
#07bf .System/g DEO2 #07bf .System/g DEO2
#07bf .System/b DEO2 #07bf .System/b DEO2
( load-theme-old )
load-theme load-theme
update-dimensions
( set starting tint: reverse=0, bg=0, fg=2 ) ( set starting tint: reverse=0, bg=0, fg=2 )
#02 .attr STZ #02 .attr STZ
update-tint update-tint
@ -194,9 +187,37 @@
#01 .File1/append DEO #01 .File1/append DEO
BRK BRK
@update-dimensions ( -> )
( set col-bytes, frequently needed )
.cols LDZ2 DUP2 ADD2 .col-bytes STZ2
( set max row/col )
.rows LDZ2 #0001 SUB2 .max-y STZ2
.cols LDZ2 #0001 SUB2 .max-x STZ2
( set screen height/width based on rows/cols )
.cols LDZ2 #30 SFT2 ( width ) #0010 ADD2 .Screen/w DEO2
.rows LDZ2 #000c MUL2 ( height ) #0010 ADD2 .Screen/h DEO2
JMP2r
@shell "bash 00 "-i 00 00 @shell "bash 00 "-i 00 00
@load-theme ( -> ) @load-theme ( -> )
;&path .File1/name DEO2
#0006 .File1/len DEO2
;&r .File1/r DEO2
.File1/ok DEI2 ORA #01 JCN JMP2r
,&r LDR2 .System/r DEO2
,&g LDR2 .System/g DEO2
,&b LDR2 .System/b DEO2
#0002 .File1/len DEO2
;&x .File1/r DEO2
.File1/ok DEI2 ORA #01 JCN JMP2r
#00 ,&x LDR INC2 ( hack ) .cols STZ2
#00 ,&y LDR .rows STZ2
JMP2r [ &path ".theme $1 &r $2 &g $2 &b $2 &x $1 &y $1 ]
@load-theme-old ( -> )
;&path .File1/name DEO2 ;&path .File1/name DEO2
#0002 .File1/len DEO2 #0002 .File1/len DEO2
;&r .File1/r DEO2 ;&r .File1/r DEO2
@ -712,16 +733,13 @@
@exec1 ( addr* -> ) @exec1 ( addr* -> )
STH2 #0001 read-arg-1 STH2r JSR2 BRK STH2 #0001 read-arg-1 STH2r JSR2 BRK
( FIXME: hardcoded terminal size )
@exec-status @exec-status
POP #0000 read-arg-1 #0006 NEQ2 ,&done POP #0000 read-arg-1 #0006 NEQ2 ,&done
#1b .Console/w DEO #1b .Console/w DEO
LIT "[ .Console/w DEO LIT "[ .Console/w DEO
LIT "2 .Console/w DEO .cur-y LDZ2 INC2 emit-dec2
LIT "4 .Console/w DEO
LIT "; .Console/w DEO LIT "; .Console/w DEO
LIT "8 .Console/w DEO .cur-x LDZ2 INC2 emit-dec2
LIT "0 .Console/w DEO
LIT "R .Console/w DEO LIT "R .Console/w DEO
&done BRK &done BRK
@ -1110,6 +1128,18 @@
INCr STHkr ?&loop INCr STHkr ?&loop
POPr POP2 POP2 JMP2r POPr POP2 POP2 JMP2r
( emit a short as a decimal )
@emit-dec2 ( n* -> )
LITr 00 ( n [0] )
&read ( n [k] )
#000a DIV2k STH2k MUL2 SUB2 STH2r INCr ( n%10 n/10 [k+1] )
DUP2 ORA ,&read JCN
POP2 ( top element was 0000 )
&write ( n0 n1 ... nk [k+1] )
NIP #30 ADD .Console/w DEO LITr 01 SUBr ( n0 ... n{k-1} [k] )
STHkr ,&write JCN
POPr JMP2r
@debug-log "debug_term.log 00 @debug-log "debug_term.log 00
@scratch $40 &pos $2 @scratch $40 &pos $2