Difficult problem: Complex text file manipulation in bash script.

I don't know if this is a big issue or not, but I'm having difficulties. I apoligize for the upcoming essay :o.

I'm writing a script, similar to a paint program that edits images, but in the form of ANSI block characters. The program so far is working. I managed to save the image into a file, but its unformatted, so Here's where I can't find a solution:

How could I save output thats already on the screen into a file?

If that can't be done, how would I be able to interactively edit the text file through the script, like move up a line or left or right for example, and delete or add a character in the file while the script is running?

All help is appreciated. :smiley:

IF this is ASCII line art drawing, look into the script command. It creates a duplicate of the screen in a file, automatically.

But is there a way to capture a portion of the screen?

No, because a terminal isn't a video card -- its memory can't be directly accessed. The closest you get is 'print screen', which doesn't go where you want, and most terminals don't provide anyway.

Usually you'd keep a copy of the screen contents in your program and update it as you go along.

What about the other solution? Moving around the text file and manipulating text while the script is running?

What other solution? Script doesn't do that. It won't capture areas of the screen.

What it does is intercept all commands and text sent to the terminal as they happen. Since you're sending them in the first place, that doesn't sound too helpful.

Sorry I should've been more clear.

How could I design the script to interact with and manipulate a text file? I know cat, sed, and gawk can edit files, but they don't move around the text file. How could I move around the file and edit manipulate text?

Define what you mean by 'moving', then. I'm assuming you don't mean mv oldfile newfile .

Move around as in, moving around in a text document, adding for example the letter "a" on the 2nd line, 4th space. Interactively moving around the document and putting text in certain places with a bash script.

Hi.

Editor emacs has a picture mode:

and an artist mode:

and possibly others.

Because emacs edits files, it saves what you see on the screen.

It isn't along the lines of a script, but it looks like you are implementing this kind of thing in a script. Educational certainly, but perhaps not efficacious.

Otherwise, lots of tput cup commands and codes might help with cursor motion. See also ncurses, and language-specific ncurses interface packages, e.g.:

libcurses-perl - Curses interface for Perl

Best wishes ... cheers, drl

Thank you so much for your reply.

But unfortunately thats not what I was looking for. Emacs picture mode doesn't use ANSI block characters for pictures.

Let me try my best to give you a picture of what I'm trying to do. Once again I'm sorry for the essay thats coming up.

I programmed a rather large library of script functions to deal with input, displaying ANSI block graphics, playing sounds, and refining the terminal and so on. I also designed a file format for displaying ANSI block characters in a complete group onto the screen.

The library is completely working. Graphics display properly, I can save a graphics file, and load the image on to terminal. Sound properly plays sequences of notes, etc.

Now here is where my problem arises. I hate designing graphics files for my library. I have to manually type in each ANSI escape code to make an image, and its extremely hard to make an image. So I decided to write an application to edit the ANSI graphic file images.

The program works, I set up the interface and painting works properly. BUT! I need to find a way to save the blocks on the screen into a file. This is what I did so far: I set it so whenever you paint onto the screen, it saves to a file. That works, but the problem is, the file isn't properly formatted because if everything thats painted on the screen, is saved in a file not formatted, in a sequential order in a line.

So I'm asking if theres a way to navigate through the file with commands and add text, etc. so I can implement it in my paint program.

You probably going to be best off implementing these functions in you own program - Id imagine you would like to be able to select a rectangle and nudge up/down left/right, copy/paste and even perhaps rotate clockwise/anti-clockwise. The processing involved in writing a file and calling external commands like sed/awk to do this sort of thing is going to add too much lag to your interface.

I'd assume you have an array or the like for each X,Y character so it should just involve simple operations on this followed by a repaint screen function to get the job done.

I've already done that. I created the a temporary file to hold all paint routines. When the user wants to save the file, the temporary file is exported. Problem is formatting the file so it displays each line of block characters correctly, instead of in a straight line.

I also have X Y variable that is used to help redraw to interface.

I'm using Bash by the way.

Sorry I re-read my message and I wasn't really clear on what I was getting at, so I updated it, but that process took longer that I expected due to interuptions.

So. Any ideas? Is what I'm trying to do possible?