[edit]Forgot to mention in the subject that this is for DBP[/edit]
This is a basic, general purpose prompt/console. Note that this has no specific functionality such as command parsing or anything.
Customize the console for your needs:
You can easily customize the text color and background color.
You can also edit the left/top/right/bottom edges of the window of the prompt - good if you only want it to be in a certain part of the screen.
Easily turn off input echo to hide passwords or whatever.
Easily limit the characters a user can input (such as allowing only alpha-numeric, etc).
Global variables are hidden in a type of one global variable to make it easier to add to an existing program.
Cursor movement:
You can go through previous entries using up/down keys.
As you're typing at it, you can move the cursor around on the line you are currently entering using the left/right arrow keys - handy for editing typos in the middle of the line, and editing previous entries before re-entering them.
The cursor is a blinking underscore but it is very easy to make a vertical line or whatever you want by editing the show and hide cursor functions.
Wrapping and scrolling:
When you're entering a line, if it is wider than the console window text will naturally wrap around to the next line.
When you get to the bottom of the console everything in it will automatically scroll up one line at a time as needed without affecting the rest of the screen.
Uses:
This can be the start of a command prompt simulation.
This can be used in hacking games that use a prompt.
This can be used for getting input from the user and displaying output without affecting anything else on the screen.
Anyone interested in a command prompt simulator can go here:
http://thegamecreators.com/?m=codebase_view&i=be65dd609b2a1ddb2c6526c68e3e097c to find my linux command prompt simulator (submitted by my old and mostly unused handle). In fact much of the code for this prompt is from the linux command prompt simulator. The linux command prompt simulator has many standard commands, including cat, cd, clear, date, echo, id, ls, mkdir, mv, cp, rm, rmdir, sleep, su, touch, whoami and several commands accept common command-line options.
set display mode 800,600,32
set window on:set window title "Prompt"
sync on:sync rate 0
cls
global char_width
char_width=text width("X")
global char_height
char_height=text height("X")
type prompt_type
left
top
right
bottom
maxrow
maxcol
textcolor
screencolor
echo
row
col
startrow
startcol
endtype
global prompt as prompt_type
dim commandline_history$(1)
rem window of the command prompt:
prompt.left=8
prompt.top=screen height()-10-char_height*15
prompt.right=screen width()-8
prompt.bottom=screen height()-8
rem Usually the echo should be 1 unless the user is entering a password or something.
prompt.echo=1
rem background prompt.color:
prompt.screencolor=rgb(255,255,255)
rem text prompt.color:
prompt.textcolor=rgb(0,0,0)
prompt.maxcol=int((prompt.right-prompt.left)/(char_width))
prompt.maxrow=int((prompt.bottom-prompt.top)/(char_height))
dim screen$(prompt.maxcol,prompt.maxrow)
_println("Welcome to the prompt!\nYou can use the arrow keys to move the cursor left/right. The up/down keys will move you through previous entries.\nHave fun!")
do
_print("> ") rem this is the prompt sign. It can easily be changed to just about anything.
input$=_prompt()
ink -1,0
if len(input$)>0 then print "You have entered:",input$
sync
loop
function is_printable(e$)
rem you can also use this to filter out characters you don't want the user to be able to input.
rem If you don't want a user to enter a specific character, remove it from this string:
chars$="`1234567890-=\~!@#$%^&*()_+|qwertyuiop[]QWERTYUIOP{}asdfghjkl;'ASDFGHJKL:zxcvbnm,./ZXCVBNM<>? "+chr$(34) rem char 34 is "
for n=1 to len(chars$)
if mid$(chars$,n)=e$ then exitfunction 1
next n
endfunction 0
function _print(s$)
for n=1 to len(s$)
if mid$(s$,n)<>"\"
screen$(prompt.col,prompt.row)=mid$(s$,n)
inc prompt.col
else
inc n
if mid$(s$,n)="n"
prompt.col=0
inc prompt.row
_sync()
else
if mid$(s$,n)="\"
screen$(prompt.col,prompt.row)="\"
inc prompt.col
else
dec n
screen$(prompt.col,prompt.row)=mid$(s$,n)
inc prompt.col
endif
endif
endif
if prompt.col>=prompt.maxcol
inc prompt.row:prompt.col=0
endif
if prompt.row>=prompt.maxrow
_shift(1) rem scroll the entire prompt to make room for a new line
endif
next n
endfunction
function _sync()
rem This function will refresh the entire prompt.
rem This function is slow so it is not used much - local refreshes are used when typing to ensure quick response time.
ink prompt.screencolor,0
box prompt.left,prompt.top,prompt.right,prompt.bottom
ink prompt.textcolor,0
left=0
for r=0 to prompt.maxrow
line$=""
left=0
for c=0 to prompt.maxcol
if c=prompt.col and r=prompt.row
show_cursor()
ink prompt.screencolor,0
text prompt.left+c*char_width,prompt.top+r*char_height,screen$(c,r)
ink prompt.textcolor,0
endif
line$=line$+screen$(c,r)
next
text prompt.left+left,prompt.top+r*char_height,line$
next r
sync
endfunction
function move_cursor_right()
ink prompt.screencolor,0
box prompt.left+prompt.col*char_width,prompt.top+prompt.row*char_height,prompt.left+(prompt.col+1)*char_width,prompt.top+(prompt.row+1)*char_height
ink prompt.textcolor,0
text prompt.left+prompt.col*char_width,prompt.top+prompt.row*char_height,screen$(prompt.col,prompt.row)
prompt.col=prompt.col+1
if prompt.col>=prompt.maxcol
if prompt.row<prompt.maxrow
prompt.col=0
prompt.row=prompt.row+1
else
prompt.col=prompt.maxcol
endif
endif
endfunction
function move_cursor_left()
ink prompt.screencolor,0
box prompt.left+prompt.col*char_width,prompt.top+prompt.row*char_height,prompt.left+(prompt.col+1)*char_width,prompt.top+(prompt.row+1)*char_height
ink prompt.textcolor,0
text prompt.left+prompt.col*char_width,prompt.top+prompt.row*char_height,screen$(prompt.col,prompt.row)
prompt.col=prompt.col-1
if prompt.col<0
if prompt.row>0
prompt.col=prompt.maxcol-1
prompt.row=prompt.row-1
else
prompt.col=0
endif
endif
endfunction
function _print_literal(s$)
for n=1 to len(s$)
screen$(prompt.col,prompt.row)=mid$(s$,n)
ink prompt.screencolor,0
box prompt.left+prompt.col*char_width,prompt.top+prompt.row*char_height,prompt.left+(prompt.col+1)*char_width,prompt.top+(prompt.row+1)*char_height
ink prompt.textcolor,0
text prompt.left+prompt.col*char_width,prompt.top+prompt.row*char_height,screen$(prompt.col,prompt.row)
inc prompt.col
if prompt.col>=prompt.maxcol
inc prompt.row
prompt.col=0
endif
if prompt.row>=prompt.maxrow
_shift(1)
endif
next n
endfunction
function _println(s$)
_print(s$+" \n")
endfunction
function _shift(amt)
for c=0 to prompt.maxcol
for r=0 to prompt.maxrow-amt
screen$(c,r)=screen$(c,r+amt)
next r
for r=prompt.maxrow-amt+1 to prompt.maxrow
screen$(c,r)=" "
next r
next c
_sync()
prompt.row=prompt.row-amt
prompt.startrow=prompt.startrow-amt
endfunction
function _prompt()
prompt.startcol=prompt.col
prompt.startrow=prompt.row
_sync()
t=timer()
while returnkey()=1 and timer()<t+180
endwhile
input$=""
tmp_save$=""
backspace=0
delete=0
history_num=0
last_key_press_time=0
last_key_pressed=0
clear entry buffer
while returnkey()=0
e$=entry$()
if prompt.echo=1
rem this blinks the cursor:
if last_blink+400<timer():on=1-on:last_blink=timer():endif
if on=1:show_cursor():else:hide_cursor():endif
if keystate(200)=0 then key200=0
if keystate(200)=1 and key200=2 and timer()>last_key_press_time+60
key200=2
last_key_press_time=timer()
inc history_num
endif
if keystate(200)=1 and key200=1 and timer()>last_key_press_time+400
key200=2
last_key_press_time=timer()
inc history_num
endif
if keystate(200)=1 and key200=0
key200=1
last_key_press_time=timer()
last_key_pressed=200
inc history_num
endif
if keystate(208)=0 then key208=0
if keystate(208)=1 and key208=2 and timer()>last_key_press_time+60
key208=2
last_key_press_time=timer()
dec history_num
endif
if keystate(208)=1 and key208=1 and timer()>last_key_press_time+400
key208=2
last_key_press_time=timer()
dec history_num
endif
if keystate(208)=1 and key208=0
key208=1
last_key_press_time=timer()
last_key_pressed=208
dec history_num
endif
if keystate(203)=0 then key203=0
if keystate(203)=1 and key203=2 and timer()>last_key_press_time+60
key203=2
last_key_press_time=timer()
if prompt.col+(prompt.row-prompt.startrow)*prompt.maxcol>prompt.startcol
move_cursor_left()
endif
endif
if keystate(203)=1 and key203=1 and timer()>last_key_press_time+400
key203=2
last_key_press_time=timer()
if prompt.col+(prompt.row-prompt.startrow)*prompt.maxcol>prompt.startcol
move_cursor_left()
endif
endif
if keystate(203)=1 and key203=0
key203=1
last_key_press_time=timer()
last_key_pressed=203
if prompt.col+(prompt.row-prompt.startrow)*prompt.maxcol>prompt.startcol
move_cursor_left()
endif
endif
if keystate(205)=0 then key205=0
if keystate(205)=1 and key205=2 and timer()>last_key_press_time+60
key205=2
last_key_press_time=timer()
if prompt.col+(prompt.row-prompt.startrow)*prompt.maxcol<prompt.startcol+len(input$)
move_cursor_right()
endif
endif
if keystate(205)=1 and key205=1 and timer()>last_key_press_time+400
key205=2
last_key_press_time=timer()
if prompt.col+(prompt.row-prompt.startrow)*prompt.maxcol<prompt.startcol+len(input$)
move_cursor_right()
endif
endif
if keystate(205)=1 and key205=0
key205=1
last_key_press_time=timer()
last_key_pressed=205
if prompt.col+(prompt.row-prompt.startrow)*prompt.maxcol<prompt.startcol+len(input$)
move_cursor_right()
endif
endif
endif
if history_num<0:history_num=0:endif
if history_num>array count(commandline_history$(0)):history_num=array count(commandline_history$(0)):endif
if history_num<>old_history_num
commandline_history$(old_history_num)=input$
input$=commandline_history$(history_num)
t$=""
for n=1 to len(commandline_history$(old_history_num))+1
t$=t$+" "
next n
oldcol=prompt.col
oldrow=prompt.row
prompt.col=prompt.startcol
prompt.row=prompt.startrow
_print_literal(t$)
oldcol=prompt.col
oldrow=prompt.row
prompt.col=prompt.startcol
prompt.row=prompt.startrow
_print_literal(input$)
endif
old_history_num=history_num
if len(e$)>0
clear entry buffer
if is_printable(e$)=1
input$=left$(input$,prompt.col-prompt.startcol+(prompt.row-prompt.startrow)*(prompt.maxcol))+e$+right$(input$,len(input$)-(prompt.col-prompt.startcol)-(prompt.row-prompt.startrow)*(prompt.maxcol))
if prompt.echo=1
oldcol=prompt.col
oldrow=prompt.row
prompt.col=prompt.startcol
prompt.row=prompt.startrow
s=prompt.startrow
_print_literal(input$)
prompt.col=oldcol
prompt.row=oldrow+prompt.startrow-s
ink prompt.screencolor,0
box prompt.left+prompt.col*char_width,prompt.top+prompt.row*char_height,prompt.left+(prompt.col+1)*char_width,prompt.top+(prompt.row+1)*char_height
ink prompt.textcolor,0
text prompt.left+prompt.col*char_width,prompt.top+prompt.row*char_height,e$
screen$(prompt.col,prompt.row)=e$
move_cursor_right()
endif
endif
endif
if keystate(14)=0:backspacepresstime=0:backspace=0:endif
if keystate(211)=0:deletepresstime=0:delete=0:endif
if (keystate(14)=1 and backspace=2 and timer()>backspacepresstime+60 and len(input$)>0) or (keystate(14)=1 and backspace=1 and timer()>backspacepresstime+400 and len(input$)>0) or (keystate(14)=1 and backspace=0 and len(input$)>0)
if backspace=0 then backspace=1 else backspace=2
backspacepresstime=timer()
input$=left$(input$,prompt.col-prompt.startcol-1+(prompt.row-prompt.startrow)*(prompt.maxcol))+right$(input$,len(input$)-(prompt.col-prompt.startcol)-(prompt.row-prompt.startrow)*(prompt.maxcol))
if prompt.echo=1
ink prompt.screencolor,0
box prompt.left+prompt.col*char_width,prompt.top+prompt.row*char_height,prompt.left+(prompt.col+1)*char_width,prompt.top+(prompt.row+1)*char_height
move_cursor_left()
ink prompt.screencolor,0
box prompt.left+prompt.col*char_width,prompt.top+prompt.row*char_height,prompt.left+(prompt.col+1)*char_width,prompt.top+(prompt.row+1)*char_height
oldcol=prompt.col
oldrow=prompt.row
prompt.col=prompt.startcol
prompt.row=prompt.startrow
s=prompt.startrow
_print_literal(input$+" ")
prompt.col=oldcol
prompt.row=oldrow+prompt.startrow-s
endif
endif
if (keystate(211)=1 and delete=2 and timer()>deletepresstime+60 and len(input$)>0) or (keystate(211)=1 and delete=1 and timer()>deletepresstime+400 and len(input$)>0) or (keystate(211)=1 and delete=0 and len(input$)>0)
if delete=0 then delete=1 else delete=2
deletepresstime=timer()
input$=left$(input$,prompt.col-prompt.startcol+(prompt.row-prompt.startrow)*(prompt.maxcol))+right$(input$,len(input$)-(prompt.col-prompt.startcol)-1-(prompt.row-prompt.startrow)*(prompt.maxcol))
if prompt.echo=1
oldcol=prompt.col
oldrow=prompt.row
prompt.col=prompt.startcol
prompt.row=prompt.startrow
s=prompt.startrow
_print_literal(input$+" ")
prompt.col=oldcol
prompt.row=oldrow+prompt.startrow-s
endif
endif
sync
endwhile
_print("\n")
clear entry buffer
if input$<>""
array insert at top commandline_history$(0)
commandline_history$(1)=input$
endif
endfunction input$
function show_cursor()
ink prompt.textcolor,0
text prompt.left+prompt.col*char_width,prompt.top+(prompt.row)*char_height-1,"_"
text prompt.left+prompt.col*char_width,prompt.top+(prompt.row)*char_height,"_"
endfunction
function hide_cursor()
ink prompt.screencolor,0
text prompt.left+prompt.col*char_width,prompt.top+(prompt.row)*char_height-1,"_"
text prompt.left+prompt.col*char_width,prompt.top+(prompt.row)*char_height,"_"
ink prompt.textcolor,0
text prompt.left+prompt.col*char_width,prompt.top+(prompt.row)*char_height,screen$(prompt.col, prompt.row)
endfunction
I made this for my logo parser program but I hope this helps whoever else that needs a prompt!