 : wcprintf ( string window -- )
     ptr>> [ (wcwrite) ] [ "\n" swap (wcwrite) ]
     [ (wcrefresh) ] tri ;
-: curses-print-refresh ( string -- ) current-window get wcprintf ;
+: cprintf ( string -- ) current-window get wcprintf ;
 
 : wcwritef ( string window -- )
     ptr>> [ (wcwrite) ] [ (wcrefresh) ] bi ;

: with-curses ( window quot -- )
    curses-ok? [ unsupported-curses-terminal ] unless
    [
        [
            ffi:initscr curses-pointer-error
            >>ptr dup apply-options
        ] dip
        ffi:erase curses-error
        init-colors
        [
            [ ffi:endwin curses-error ] [ ] cleanup
        ] curry with-window
    ] with-destructors ; inline

TUPLE: curses-terminal < disposable

: (wgetch) ( window -- key )
    ffi:wgetch [ curses-error ] keep ; inline

: (wattroff) ( attribute window-ptr -- )
    swap ffi:wattroff curses-error ; inline

: (wattron) ( attribute window-ptr -- )
    swap ffi:wattron curses-error ; inline

PRIVATE>

: wcrefresh ( window -- ) ptr>> (wcrefresh) ;
: crefresh ( -- ) current-window get wcrefresh ;

: wcnl ( window -- ) [ "\n" ] dip ptr>> (wcwrite) ;
: cnl ( -- ) current-window get wcnl ;

: wcwrite ( string window -- ) ptr>> (wcwrite) ;
: cwrite ( string -- ) current-window get wcwrite ;

: wcprint ( string window -- )
    ptr>> [ (wcwrite) ] [ "\n" swap (wcwrite) ] bi ;
