Another word wrap program

Mac OSX specific forum
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Another word wrap program

Post by WilliamL »

I have been an on-and-off listener to Hearts of Space and I wanted to take the text of the program and have it 'word wrapped' to the width of the rest of the text. I wanted to end up with individual sentences, of the desired length, to paste back into my TextEdit document (copied out of the bottom Editor field-or I could put it on the clipboard).

I pretty much just got the program to work so it isn't polished and I only run it in editor mode. As soon as I got it to work I lost interest. I'll get back to it after next weeks program (I just know it won't work because of some odd text). You can play with it with the text to wrap in the comments at the top and what the output looks like (at least on my output) below that.

;<<<<<<<<take below and paste into textedit and copy the result to clipboard(without ;'s)>>>>>>>>>>>
;Electro-acoustic waves for early summer
;Ahhh, the sweetness of early summer, the balm of warm breezes, the pollen, the allergies. It's a good time for electro-acoustic music with gentle rhythms, soft harmonies and seductive melodies. In a word, something "mellotronic."
;
;Now, historians of electronic instruments will tell you that the Mellotron was a tape-based, polyphonic, sample-playback keyboard, developed in England in the early 1960s. The instrument achieved classic status For its key role in popular albums by the Moody Blues And the Beatles, And brought violin, cello, brass, flute And choir sounds To innumerable albums from the Progressive Rock era.
;
;These days, every laptop can be a sampler, the original Mellotron sound is a menu choice, And the orchestration of electronic music is open To all. On this transmission of Hearts of Space, we revisit the romantic/contemplative side of ambient electronica, on a program called MELLOTRONIC.

;<<<<<<<<< with any luck you will end up with this(without ;'s)>>>>>>>>>>>
; Electro-acoustic waves For early summer
; Ahhh, the sweetness of early summer, the balm of warm breezes, the pollen, the allergies. It's a good time for electro-
; acoustic music With gentle rhythms, soft harmonies And seductive melodies. In a word, something "mellotronic."
;
; Now, historians of electronic instruments will tell you that the Mellotron was a tape-based, polyphonic, sample-
; playback keyboard, developed in England in the early 1960s. The instrument achieved classic status For its key role in
; popular albums by the Moody Blues And the Beatles, And brought violin, cello, brass, flute And choir sounds To
; innumerable albums from the Progressive Rock era.
;
; These days, every laptop can be a sampler, the original Mellotron sound is a menu choice, And the orchestration of
; electronic music is open To all. On this transmission of Hearts of Space, we revisit the romantic/contemplative side
; of ambient electronica, on a program called MELLOTRONIC.

; take clipboard and paste into top box, hit 'wrap', copy from bottom box

Code: Select all

;simple word wrap app.  Only tested in Editor mode.
EnableExplicit

Global Dim final$(40)
Global fcnt
Define cnt
#Window=1

Procedure.s WrapText(txt$,width)
    ; one entire string from Editor gadget
    Define pcnt,ptxt$
    fcnt=1 : pcnt=1 : ptxt$=""
    Repeat
        If pcnt>width  Or Mid(txt$,pcnt,1)=Chr(10)
            If Mid(txt$,pcnt,1)=Chr(10)
                pcnt+1
                txt$=Right(txt$,Len(txt$)-pcnt+1)
            Else
                ;ptxt$ already defined
                txt$=Right(txt$,Len(txt$)-pcnt+1)
                ;backup to blank
                While Right(ptxt$,1)<>" " And Right(ptxt$,1)<>"-" And Len(ptxt$)>0
                    txt$=Right(ptxt$,1)+txt$ ; put end of ptxt$ back on txt$
                    ptxt$=Left(ptxt$,Len(ptxt$)-1) ; take off end of ptxt$
                Wend
            EndIf
            final$(fcnt)=RTrim(ptxt$)
            fcnt+1
            pcnt=1
            ptxt$=""
        Else
            ptxt$+Mid(txt$,pcnt,1)
            pcnt+1
        EndIf
        Debug Str(fcnt)+" "+Str(Len(txt$))
    Until Len(txt$)<width
    If Len(txt$)>0
        final$(fcnt)=RTrim(txt$)
    EndIf
EndProcedure

Procedure DrawWnd()
    Define cnt,txt$
    If OpenWindow(#Window,0,0,1000,800,"Paste clipboard")
        EditorGadget(1, 10,  10, WindowWidth(#Window)-20,100)
        EditorGadget(2, 10, 120, WindowWidth(#Window)-20,320)
        EditorGadget(3, 10, 450, WindowWidth(#Window)-20,320)
        SpinGadget(5,WindowWidth(#Window)-200,WindowHeight(#Window)-24,80,20,30,120)
        SetGadgetState(5,118)
        ButtonGadget(4,WindowWidth(#Window)-100,WindowHeight(#Window)-24,80,20,"Wrap")
        If GetClipboardText()
            SetGadgetText(1,GetClipboardText())
        EndIf
    EndIf
EndProcedure
DrawWnd()

Repeat
    Select WaitWindowEvent()
    Case #PB_Event_Gadget
        Select EventGadget()
        Case 5 ; spingadget
            Select EventType()
            Case #PB_EventType_Up
                SetGadgetState(5,GetGadgetState(5))
            Case #PB_EventType_Down
                SetGadgetState(5,GetGadgetState(5))
            EndSelect
        Case 4
            wraptext(GetGadgetText(1),GetGadgetState(5)+1)
            ClearGadgetItems(2) : ClearGadgetItems(3)
            cnt=0
            Repeat
                cnt+1
                AddGadgetItem(2,-1,Str(cnt)+" "+final$(cnt)+"("+Str(Len(final$(cnt)))+")")
                AddGadgetItem(3,-1,final$(cnt))
            Until cnt=fcnt
        EndSelect
    Case #PB_Event_CloseWindow
        End
    EndSelect
ForEver
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1