|
Forum Index : Microcontroller and PC projects : Line editor to edit lines/pages in Basic
| Author | Message | ||||
| rave Newbie Joined: 24/02/2018 Location: United StatesPosts: 28 |
Hi all, I've added a LINEEDIT.BAS program with a LineEdit function to the MMBasic repo: http://fruitoftheshed.com/MMBasic.Line-editor-for-interactive-editing-of-text-lines-pages-LINEEDIT-BAS.ashx?NoRedirect=1 &NS=MMBasic I've included some examples on how to use the LineEdit function to interactively edit lines and pages of text stored in string arrays in your MMBasic 4.5 program. For example to interactively edit a string: a$ = "text to edit" Do: k = LineEdit(a$): Loop Until k = 13 ' until ENTER To edit 10 lines stored in an array: ' 10 lines of length 40 Dim a$(10) length 40 Cls Print "Tiny edit | ESC to quit" For row = 1 To 10 Print @(0,12*row) a$(row);Tab(11);"|" Next row row = 1 ' first row to edit cur = 0 ' cursor at begin of the line max = 40 ' max size of a$() win = 10 ' edit in window of 10 chars Do Print @(0,12*row); ' place cursor where we want to edit the line k = LineEdit(a$(row), cur, -1, 40, 10) ' -1: get next key press If k = 128 Then If row > 1 Then row = row - 1 ' UP key: move up ElseIf k = 13 Or k = 129 Then If row < 10 Then row = row + 1 ' DOWN key: move down EndIf Loop Until k = 27 ' until ESC You can even use the editor to edit while the screen is updating, for example continuously displaying the current time: ' 10 lines of length 120, edited within current screen size limits Dim a$(10) length 120 Cls Print " | Editor | ESC to quit" For row = 1 To 10 Print @(0,12*row) a$(row); Next row row = 1 ' first row to edit cur = 0 ' cursor at begin of the line max = 40 ' max size of a$() win = 10 ' edit in window of 10 chars ' show cursor Font 1,,1: Print @(0,12*row) Mid$(a$(row)+" ", 1, 1);Chr$(8);: Font 1,,0 Do Print @(0,0) Time$; ' continuously show the current time k = Asc(Inkey$) If k Then Print @(0,12*row); k = LineEdit(a$(row), cur, k, 120, MM.HRes/6) ' pass in key to edit line If k = 128 Then ' UP key: move up If row > 1 Then row = row - 1 Font 1,,1: Print @(0,12*row) Mid$(a$(row)+" ", 1, 1);Chr$(8);: Font 1,,0 cur = 0 ElseIf k = 13 Or k = 129 Then ' DOWN key: move down If row < 10 Then row = row + 1 Font 1,,1: Print @(0,12*row) Mid$(a$(row)+" ", 1, 1);Chr$(8);: Font 1,,0 cur = 0 EndIf EndIf Loop Until k = 27 ' until ESC Enjoy! - Rob |
||||
| twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1671 |
Hi Rob, thanks for your contribution! Your code remainds me of my LineEdit function and my MASKEdit$ EDIT: Both functions tested with MAXIMITES (MMBasic 4.5)! '************************************************* '* '* MASKEdit$ V.95 '* mod of LineEdit() '* MMBasic 4.5/Maximite '* '* line editor for small strings (e.g. file names '* time$, date$ ...) '* MASKEdit$("12:45:56",1,"##.##.##") '* MASKEdit$(" TEST.BAS",1," . ") '* Special mask characters: '* " " any ascii character '* "#" any number(0-9) '* "." skip char will be ignored/skipped '* "@" numbers & letters (not yet implemented!) '* Used functions: FindLeft,FindRight,IsNum '* Limit: '* The string must not exceed the width of screen '*------------------------------------------------ Function MaskEdit$ edText$, cursorp, edMask$ Local hpos, vpos, p, ep, keyvalue, enterPressed Local text$,ltext$,mtext$,rtext$,C$,M$,Mask$ Local TRUE, FALSE TRUE=1 FALSE=0 hpos=MM.HPos:vpos=MM.VPos ' save print position text$=edText$ ' use local copy of text$ string MaskEdit$=edText$ ' backup edText$ for Esc enterPressed=FALSE ' Flag to EXIT the loop escPressed=FALSE ' a Global var ep=Len(text$) ' end pointer C$="." ' prohibited/skipped char p=cursorp:If Not p Then p=1 ' cursor pointer Mask$=edMask$ ' local copy If ep<>Len(Mask$) Then Mask$=Space$(ep) Pause 200 ' es gibt einen Zeitfaktor fuer keyboard input buffer ~140ms ltext$=Mid$(text$,1,p-1) mtext$=Mid$(text$,p,1) rtext$=Mid$(text$,p+1,ep-p) Print @(hpos,vpos)ltext$; Font#1,,1 Print mtext$; Font#1 Print rtext$; Do Do:keyvalue=KeyDown:Pause 50:Loop While keyvalue=0 If keyvalue=130 And p>1 Then p=FindLeft(Mask$,C$,p) 'p=p-1 'left arrow EndIf If keyvalue=131 And p<ep Then 'p=p+1 'right arrow p=FindRight(Mask$,C$,p) EndIf If keyvalue=13 Then enterPressed=TRUE 'Enter EndIf If keyvalue=27 Then 'Esc Print @(hpos,vpos)MaskEdit$; escPressed=TRUE EndIf If keyvalue=134 Then 'Home p=1 If Mid$(text$,p,1)=C$ Then p=FindLeft(Mask$,C$,p) EndIf EndIf If keyvalue=135 Then 'End p=ep If Mid$(text$,p,1)=C$ Then p=FindRight(Mask$,C$,p) EndIf EndIf If keyvalue>=32 And keyvalue<127 Then 'all ASCII chars M$= Mid$(Mask$,p,1) If m$=" " Or (m$="#" And IsNum(Chr$(keyvalue))) Then ' num mask, only numbers If p<ep Then ltext$=ltext$+Chr$(keyvalue) If Mid$(Mask$,p+1,1)=C$ Then ltext$=ltext$+Mid$(Text$,p+1,1) p=FindRight(Mask$,C$,p) mtext$=Mid$(text$,p,1) rtext$=Mid$(text$,p+1) Else ltext$=Left$(text$,p-1) mtext$=Chr$(keyvalue) rtext$="" EndIf EndIf ElseIf keyvalue=132 Then 'insert, when dot is on the right _and_ If mask$=" . " And p<9 Then ' mask$=" . " mtext$=" " ltext$=Mid$(text$,2,p-1) EndIf ElseIf keyvalue=127 Then 'Del If mask$=" . " And p<9 Then ' mask$=" . " If p>1 Then mtext$=Mid$(text$,p-1,1) ltext$=" "+Mid$(text$,1,p-2) Else mtext$=" " EndIf EndIf ElseIf keyvalue=8 And p>1 Then 'Bsp, delete left space p=FindLeft(Mask$,C$,p) mtext$=" " ltext$=Mid$(text$,1,p-1) rtext$=Mid$(text$,p+1) Else If p>=ep Then p=ep ltext$=Mid$(text$,1,p-1) 'move cursor / over write mtext$=Mid$(text$,p,1) rtext$=Mid$(text$,p+1,ep-p) EndIf Print @(hpos,vpos)ltext$; Font#1,,1 Print mtext$; Font#1 Print rtext$; text$=Left$(ltext$+mtext$+rtext$,ep) Pause 125 ' some wait states to slow down key input Loop While Not enterPressed And Not escPressed If Not escPressed Then MaskEdit$=ltext$+mtext$+rtext$ Print @(hpos,vpos)MaskEdit$; EndIf End Function '***************************************************** Kind regards Michael causality ≠ correlation ≠ coincidence |
||||
| rave Newbie Joined: 24/02/2018 Location: United StatesPosts: 28 |
Nice! I like the idea of masking. Note that you can do the same (and more ) with LineEdit by passing the key press to LineEdit with the 'key' parameter, which you can inspect and mask before calling LineEdit in the loop: ' edit string of digits a$ = "1234" Font 1,,1: Print Mid$(a$, 1, 1);: Font 1,,0: Print Mid$(a$, 2);Chr$(13); cur = 0 ' LineEdit cursor to the begin of the line Do k = Asc(Inkey$) If k Then If k < 32 Or k > 126 Or Instr("0123456789", Chr$(k)) Then k = LineEdit(a$, cur, k) EndIf EndIf Loop Until k = 13 Or k = 27 ' until ENTER or ESC You could even make the masking dependent on the location of the string edited (which is tracked by the 'cur' parameter). - Rob |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |