Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 22:33 23 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : [Picomite] Lineedit$

Author Message
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1236
Posted: 08:48am 14 Nov 2024
Copy link to clipboard 
Print this post

I updated a 10 year old function. Maybe useful for javavis file manager?

'* Demo code:
Option explicit
Dim a$
CLS
Print @(12,12);
a$= LineEdit$("The Backshed")
Print
Print a$,Len(a$)
End

'*******************************************************
'*
'*           LineEdit$ V.95
'*
'*      MMBasic 6.0/PicoMite 2 (HDMI)
'*
'*      by twofingers 09-2014 at TBS
'*          (modified 10-2024)
'* Line editor for small strings (e.g. file names)
'* Tested with PicoMite in Mode 1. Not for the console!
'*--------------------------------------------------------------
'* Limit:
'* The string must not exceed the width of screen
'*
'* Supported keys:
'* left arrow, right arrow
'* Enter
'* ESC
'* Home
'* End
'* Del
'* Ins
'* Backspace
'* all ASCII chars (char 32-126)
'*
'*------------------------------------------------
'*  possible improvments:
'*  a fixed minimum string size
'*  color modes
'*******************************************************
' This code may be freely distributed and
' changed. Provided AS IS without any warranty.
' Use it at your own risk.
'*******************************************************


Function LineEdit$(edText$)
Dim integer Invers
Local hpos, vpos
Local p, ep 'current print position, end position = last character pos.
Local keyValue, enterPressed
Local atext$,ltext$,mtext$,rtext$,shift$
Local insertMode

Const TRUE=1, FALSE=0
Const LEFTKEY =130
Const RIGHTKEY=131
Const ENTERKEY=13
Const ESCKEY  =27
Const BACKKEY =8
Const HOMEKEY =134
Const ENDKEY  =135
Const DELKEY  =127
Const INSKEY  =132
Const FH      =MM.Info(FONTHEIGHT)
Const FW      =MM.Info(FONTWIDTH)

 hpos=MM.Info(HPOS):vpos=MM.Info(VPOS) ' save print position
 aText$=edText$            ' use local copy of edtext$ parameter
 LineEdit$=edText$         ' backup edText$ for Esc
 p=Len(atext$)             ' cursor pointer
 ep=p                      ' end pointer
 shift$=" "                ' out shift buffer
 enterPressed=FALSE        ' Flag to EXIT the loop
 insertMode=FALSE

 Print @(hpos,vpos)Left$(atext$,p-1);
 t.invers
 Print Right$(atext$,1);
 t.normal

 Do
   Do :keyValue=Asc(Inkey$):Loop While keyValue=0
   If keyvalue=LEFTKEY And p>1 Then
       p=p-1  'left arrow
   EndIf
   If keyvalue=RIGHTKEY And p<ep Then
        p=p+1   'right arrow
   EndIf
   If keyvalue=ENTERKEY Then
      enterPressed=TRUE                  'Enter
   EndIf
   If keyvalue=ESCKEY Then
      Print @(hpos,vpos)LineEdit$;
      Exit Function                      'ESC
   EndIf
   If keyvalue=HOMEKEY Then p=1          'Home
   If keyvalue=ENDKEY Then p=ep          'End
   If keyvalue=INSKEY Then
      insertMode=Not insertMode          'Insert toggle
   EndIf
   If keyvalue>=32 And keyvalue<127 Then 'all ASCII chars
     If insertMode And Len(shift$) <255 Then
       If p<ep Then
         p=p+1
         ltext$=ltext$+Chr$(keyvalue)
         mtext$=""
         rtext$=Mid$(atext$,p-1,ep-p+1)
       Else
         ltext$=Left$(atext$,p-1)+Chr$(keyvalue)
         mtext$=""
         rtext$=""
       EndIf
       shift$=Right$(atext$,1)+shift$
     Else
       If p<ep Then
         p=p+1
         ltext$=ltext$+Chr$(keyvalue)
         mtext$=Mid$(atext$,p,1)
         rtext$=Mid$(atext$,p+1)
       Else
         ltext$=Left$(atext$,p-1)
         mtext$=Chr$(keyvalue)
         rtext$=""
       EndIf
     EndIf
   ElseIf keyvalue=BACKKEY And p>1 Then        'Backspace
     If insertMode Then
       p=p-1
       ltext$=Mid$(atext$,1,p-1)
       mtext$=""
       rtext$=Mid$(atext$,p+1)+Left$(shift$,1)
       shift$=Mid$(shift$,2)
     Else
       p=p-1
       ltext$=Mid$(atext$,1,p-1)
       mtext$=Mid$(atext$,p+1,1)
       rtext$=Mid$(atext$,p+2)+Left$(shift$,1)
       shift$=Mid$(shift$,2)
     EndIf
   ElseIf keyvalue=DELKEY Then              'Del
       ltext$=Mid$(atext$,1,p-1)
       mtext$=Mid$(atext$,p+1,1)
       rtext$=Mid$(atext$,p+2)+Left$(shift$,1)
       shift$=Mid$(shift$,2)
   Else
     If insertMode Then
       ltext$=Mid$(atext$,1,p-1)          'move cursor / insert mode
       mtext$=""
       rtext$=Mid$(atext$,p,ep-p+1)
     Else
       ltext$=Mid$(atext$,1,p-1)          'move cursor / over write
       mtext$=Mid$(atext$,p,1)
       rtext$=Mid$(atext$,p+1,ep-p)
     EndIf
   EndIf

   Print @(hpos,vpos)ltext$;
   If Not insertMode Then t.invers
   Print mtext$;
   t.normal
   Print rtext$;

   If insertMode Then                   ' show the insert cursor
     Line hpos+p*FW-FW,vpos+FH/2,hpos+p*FW-FW,vpos+FH-1,2
   EndIf

   aText$=Left$(ltext$+mtext$+rtext$,ep)
   If shift$="" Then shift$=" "
 Loop While Not enterPressed
 LineEdit$=ltext$+mtext$+rtext$
 Print @(hpos,vpos)LineEdit$;
End Function
'*****************************************************

Sub t.invers
 Colour RGB(BLACK),RGB(WHITE)
 Invers=1
End Sub

Sub t.normal
 Colour RGB(WHITE),RGB(BLACK)
 invers=0
End Sub

Edited 2024-11-14 18:52 by twofingers
causality ≠ correlation ≠ coincidence
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2075
Posted: 09:27am 14 Nov 2024
Copy link to clipboard 
Print this post

similar code for the console under MMBasic + easy VT100 talking with windowing

VT Console Pack. Screen Positioning, Sane Key Scanning And a String Editing Routine
+
String editing on-screen for VT compatible Terminals

Takes a string to edit, where to do it and max length.

e.g.

a$=StrEdit$("Fred",10,10,60)

It came from a database program I wrote (and published) for the Amiga back in 1989, which came from some QB5 code I did in '87 (!) refreshed for later years and MMBasic.

Console editing can be quite "messy" at low baud rates and whereas the above works at any, the minimum tolerant, really is 9600... which still exhibits a lot of the cursor movement - for that genuine '80s experience.  
Edited 2024-11-14 19:45 by CaptainBoing
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1236
Posted: 10:21am 14 Nov 2024
Copy link to clipboard 
Print this post

Thanks Captain! I didn't know that yet!

I looked at my code again and I think I should have used select/case. However, the Maximite I originally wrote this for didn't have select/case (IMHO).
Best regards
Michael
causality ≠ correlation ≠ coincidence
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 857
Posted: 10:40am 14 Nov 2024
Copy link to clipboard 
Print this post

Great stuff, guys. Many thanks  
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2075
Posted: 01:28pm 14 Nov 2024
Copy link to clipboard 
Print this post

  twofingers said  Thanks Captain! I didn't know that yet!

I looked at my code again and I think I should have used select/case. However, the Maximite I originally wrote this for didn't have select/case (IMHO).
Best regards
Michael


I often do something I really like to do - pick up a piece of code from years ago and just hone it, speed it up, re-do bits of it to take advantage of new thinking... try this, try that all to see if I can get it smaller and faster. I think it just scratches my Z80 bone
 
GerryL
Newbie

Joined: 24/01/2019
Location: Australia
Posts: 33
Posted: 11:55pm 14 Nov 2024
Copy link to clipboard 
Print this post

  Quote  I think it just scratches my Z80 bone


Writing code gets the dopamine levels up, some languages more than others.  I have always found assembly language to be the best dopamine generator.  The problem with assembly is it may be fun to write and efficient for the processor but not the most productive language to use to knock up something complex in a hurry.  I used the Forth language from the mid-1980’s thru to about 2005 and found that it was just as good as assembly in getting a reward kick when the program works. A Forth program is something you can keep on re-visiting to optimise, the downside is, as some wag mentioned, it’s ‘a write only language’ cos reading and deciphering complex stack operations can be painful.  I ended up porting pForth onto the STM32H7 and are now re-writing my weather station in Forth to debug the port, and more importantly ensuring my dopamine levels are up in retirement.

Gerry
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2075
Posted: 09:36am 15 Nov 2024
Copy link to clipboard 
Print this post

nice! I think Forth didn't really get the exposure it deserves... anything that generates m/c was a grail back decades ago, probably because, as you point out, it can be impenetrable and the Jupiter Ace (a forth version of the ZX80 - an attempt to get Forth out to the masses) didn't do well in the face of friendly Basic. There were odd compilers that came and went but only did sub-sets and given time, along came QB which was a native compiler... very rich and unbelievably quick for the day.

One of my all-time favourite things to do is hand-compile... get a bit of code working just right using a high-level language (usually Basic) and then take each line and re-write it in assembler. I was looking through some source I did 40 years ago and it had tell-tale "L270", "L350" labels for jumps/subs
Edited 2024-11-15 22:58 by CaptainBoing
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024