Personaje
#1
Big Grin 
This is a simple program that works like "Cowsay" Flatpak app. It associates a quotation with a silly ASCII picture of an animal or person or something else. It draws a balloon around the quotation. Maybe I should have added the option for "thought" which is fluffier cloud...

This requires at least two files:
  • personaje.txt - contains the ASCII art. Each "personality" should be separated by a single line which has only three dashes, no whitespace around it, only newline should follow it.
  • personajq.txt - contains the quotations, one per line.

A file could be asked for in interactive mode:
  • personaj1.txt - has the quotation that you prefer to give the personality which is not found in "personajq.txt". I wrote this program originally in Freebasic, and I'm not sure if "_CLIPBOARD$" function works on Linux. Otherwise for Windows the change to that function could be certainly done.

Also in interactive mode it's possible to load a text file of your choice to display the personality on the terminal.

This program does no special formatting for the personality, only for the balloon and caption inside. Its output is into the terminal to make it easier to copy and paste into a text editor to foul it up...

Run this program without parameters and it comes up with a random quotation and a random personality from the two files required for it. Otherwise type "help" after the program name to see what's in it for interactive mode. Smile

I'm only including the source code. I leave it to your imagination to go looking for ASCII art and things to say...

Code: (Select All)
$CONSOLE:ONLY
OPTION _EXPLICIT
DIM AS INTEGER p, q, pl, ql, ff, m, n, i, rm, m1, m2
DIM AS STRING pfile, qfile, a, b, bl, ca, crlf
DIM ch AS _UNSIGNED _BYTE
REDIM qline(1 TO 1) AS STRING
REDIM pline(1 TO 1) AS STRING

$IF WIN THEN
crlf = CHR$(13) + CHR$(10)
$ELSEIF LINUX THEN
crlf = CHR$(10)
$ELSE
crlf = CHR$(13)
$END IF

RANDOMIZE TIMER

q = 1
p = 1
ca = COMMAND$(1)
IF ca = "" THEN
    qfile = "personajq.txt"
    pfile = "personaje.txt"

    IF NOT _FILEEXISTS(pfile) THEN
        PRINT "File NOT found: "; pfile
        SYSTEM
    END IF
    IF NOT _FILEEXISTS(qfile) THEN
        PRINT "File NOT found: "; qfile
        SYSTEM
    END IF

    ql = 10
    pl = 10
    REDIM qline(1 TO ql) AS STRING
    REDIM pline(1 TO pl) AS STRING

    b = ""
    ff = FREEFILE
    OPEN pfile FOR INPUT AS ff
    DO UNTIL EOF(ff)
        LINE INPUT #ff, a
        IF a = "---" THEN
            pline(p) = b
            b = ""
            p = p + 1
            IF p > pl THEN
                pl = pl + 10
                REDIM _PRESERVE pline(1 TO pl) AS STRING
            END IF
        ELSE
            'for Windows concatenate "chr(13) + chr(10)" instead of just the latter
            b = b + delundersinside$(a) + crlf
        END IF
    LOOP
    CLOSE ff
    IF b = "" THEN
        p = p - 1
    ELSE
        b = b + delundersinside$(a) + crlf
    END IF

    ff = FREEFILE
    OPEN qfile FOR INPUT AS ff
    DO UNTIL EOF(ff)
        LINE INPUT #ff, a
        IF a <> "" THEN
            qline(q) = a
            q = q + 1
            IF q > ql THEN
                ql = ql + 10
                REDIM _PRESERVE qline(1 TO ql) AS STRING
            END IF
        END IF
    LOOP
    CLOSE ff
ELSE
    ca = LCASE$(ca)
    IF ca = "help" THEN
        PRINT quotesquiggle$("Accepted parameters are: ~say~, ~pers~, ~both~ (without double-quotes)")
        SYSTEM
    END IF
    IF ca = "say" OR ca = "both" THEN
        PRINT "Write what the personality has to say"
        PRINT quotesquiggle$("or ~c~ (without double-quote) to get it from")
        PRINT "(current-dir)/personaj1.txt:"
        LINE INPUT b
        IF b = "" THEN SYSTEM
        IF b = "c" THEN
            qfile = "personaj1.txt"
            b = ""
            ff = FREEFILE
            OPEN qfile FOR INPUT AS ff
            IF NOT EOF(ff) THEN LINE INPUT #ff, b
            CLOSE ff
        END IF
        qline(1) = b
    END IF
    IF ca = "pers" OR ca = "both" THEN
        PRINT "Enter the filename (in current dir) which contains the personality:"
        LINE INPUT pfile
        IF pfile = "" THEN END
        IF NOT _FILEEXISTS(pfile) THEN
            PRINT "Without a personality I cannot work!"
            SYSTEM
        END IF
        b = ""
        ff = FREEFILE
        OPEN pfile FOR INPUT AS ff
        DO UNTIL EOF(ff)
            LINE INPUT #ff, a
            b = b + a + crlf
        LOOP
        CLOSE ff
        pline(1) = b
    END IF
END IF

IF q = 1 THEN n = 1 ELSE n = INT(RND * q + 1)
a = qline(n)
b = ""
bl = ""
rm = -1
m = 1
FOR i = 1 TO LEN(a)
    m = m + 1
    ch = ASC(a, i)
    IF ch = 32 AND m > 50 THEN
        IF m > rm THEN rm = m
        bl = ""
        m = 1
    ELSE
        bl = bl + CHR$(ch)
    END IF
NEXT
IF rm = -1 THEN
    rm = m
ELSEIF m > rm THEN
    rm = m
END IF

bl = ""
m = 1
FOR i = 1 TO LEN(a)
    m = m + 1
    ch = ASC(a, i)
    IF ch = 32 AND m > 50 THEN
        b = b + "|" + bl + SPACE$(rm - LEN(bl)) + "|" + crlf
        bl = ""
        m = 1
    ELSE
        bl = bl + CHR$(ch)
    END IF
NEXT
IF bl <> "" THEN
    b = b + "|" + bl + SPACE$(rm - LEN(bl)) + "|" + crlf
END IF
m1 = rm - (rm \ 2) - 1
m2 = rm - m1 - 2
b = " " + STRING$(rm, 45) + crlf + b + " " + STRING$(m1, 45) + "||" + STRING$(m2, 45) + crlf + SPACE$(m1 + 1) + "||"
PRINT b

IF p = 1 THEN n = 1 ELSE n = INT(RND * p + 1)
PRINT pline(n)
SYSTEM


FUNCTION quotesquiggle$ (sa AS STRING)
    STATIC st AS STRING
    st = sa
    ReplaceString2 st, "~", CHR$(34), 0
    quotesquiggle$ = st
END FUNCTION

SUB ReplaceString2 (tx AS STRING, sfind AS STRING, repl AS STRING, numtimes AS _UNSIGNED LONG)
    DIM AS STRING s, t
    DIM AS _UNSIGNED LONG ls, count, u
    DIM goahead AS _BYTE
    IF (tx = "") OR (sfind = "") OR (sfind = repl) OR (LEN(sfind) > LEN(tx)) THEN EXIT SUB
    s = UCASE$(sfind): t = UCASE$(tx)
    ls = LEN(s)
    count = 0
    goahead = 1
    DO
        u = INSTR(t, s)
        IF u > 0 THEN
            tx = LEFT$(tx, u - 1) + repl + MID$(tx, u + ls)
            t = UCASE$(tx)
            IF numtimes > 0 THEN count = count + 1: IF count >= numtimes THEN goahead = 0
        ELSE
            goahead = 0
        END IF
    LOOP WHILE goahead
END SUB

FUNCTION delundersinside$ (sa AS STRING)
    STATIC st AS STRING, i AS LONG, ch AS _UNSIGNED _BYTE, fl AS _UNSIGNED _BYTE
    st = SPACE$(LEN(sa))
    fl = 0
    FOR i = 1 TO LEN(st)
        ch = asc(sa, i)
        IF ch = 95 AND fl = 1 THEN
            'mid$(st, i, 1) = " "
            _CONTINUE
        ELSEIF ch <> 95 AND fl = 0 THEN
            fl = 1
        END IF
        MID$(st, i, 1) = CHR$(ch)
    NEXT
    delundersinside$ = RTRIM$(st)
END FUNCTION

EDIT: Made sure it could work on "any" OS. Didn't process properly the "---" as last line of "personaje.txt", fixed. Didn't format the last line of balloon properly, fixed.

EDIT #2: Added a function, for display of the "personality" that turns the underscores into spaces, the annoying ones that interfere with image view.
Reply


Messages In This Thread
Personaje - by mnrvovrfc - 01-20-2023, 04:24 PM
RE: Personaje - by bplus - 01-20-2023, 05:44 PM
RE: Personaje - by mnrvovrfc - 01-20-2023, 08:36 PM
RE: Personaje - by bplus - 01-20-2023, 09:41 PM
RE: Personaje - by mnrvovrfc - 01-20-2023, 09:56 PM
RE: Personaje - by bplus - 01-20-2023, 10:54 PM
RE: Personaje - by mnrvovrfc - 01-21-2023, 12:27 PM
RE: Personaje - by mnrvovrfc - 01-21-2023, 02:27 PM
RE: Personaje - by mnrvovrfc - 01-22-2023, 07:55 PM
RE: Personaje - by mnrvovrfc - 01-22-2023, 10:01 PM
RE: Personaje - by SMcNeill - 01-23-2023, 07:21 PM
RE: Personaje - by mnrvovrfc - 01-23-2023, 09:06 PM
RE: Personaje - by bplus - 01-24-2023, 01:33 AM
RE: Personaje - by bplus - 01-24-2023, 02:42 AM
RE: Personaje - by bplus - 01-24-2023, 03:28 AM
RE: Personaje - by bplus - 01-25-2023, 01:20 PM
RE: Personaje - by bplus - 01-25-2023, 01:31 PM
RE: Personaje - by mnrvovrfc - 01-25-2023, 02:32 PM
RE: Personaje - by bplus - 01-25-2023, 03:24 PM
RE: Personaje - by mnrvovrfc - 01-25-2023, 04:26 PM
RE: Personaje - by bplus - 01-25-2023, 04:34 PM
RE: Personaje - by mnrvovrfc - 01-25-2023, 08:04 PM
RE: Personaje - by bplus - 01-26-2023, 05:31 PM
RE: Personaje - by mnrvovrfc - 01-29-2023, 03:33 AM
RE: Personaje - by mnrvovrfc - 01-30-2023, 09:50 AM



Users browsing this thread: 13 Guest(s)