How do I have text go to the start when exceeding the width of `textarea`?

Yeah, basically what the question and image entails. Is there a way to have writing go all the way back there when someone types more than the width of the textarea?
No description
50 Replies
rezabet
rezabet3mo ago
So I suppose you could imagine I want something like this:
No description
rezabet
rezabet3mo ago
(Also, yeah, I need that text on the top left to be there)
ἔρως
ἔρως3mo ago
wait, you want the text from inside the textarea to go outside the textarea?
rezabet
rezabet3mo ago
Yeah Is that possible? Or atleast "morph" the textarea to look like that
ἔρως
ἔρως3mo ago
why do you even want to do that? that goes against all logic and reasoning why do you want the text to arbitrarily escape out of the box?
rezabet
rezabet3mo ago
Because on the terminal, that's how it works and I'm trying to create something similar to that
ἔρως
ἔρως3mo ago
can you show me which terminal does that?
SvenSplynter
SvenSplynter3mo ago
I think what you're looking for, is a fixed placeholder text inside your text area. Then you 'emulate' a terminal look.
SvenSplynter
SvenSplynter3mo ago
No description
ἔρως
ἔρως3mo ago
i think we need more context, because if what he's saying is right, it makes no sense if what you are saying is right (aka: the prompt), then it is harder than it looks
Jochem
Jochem3mo ago
if you're building a terminal, you're better off rewriting the typing logic using javascript, and that's a nightmare
ἔρως
ἔρως3mo ago
if you want to emulate a terminal, i strongly recommend you to use xterm.js
SvenSplynter
SvenSplynter3mo ago
Never said it was easy. I'm just trying to translate the original request. Haha. Because I agree with you. If that's not what he wants to do, it doesn't make any sense at all
ἔρως
ἔρως3mo ago
i know, but the question is worded in such a weird way
SvenSplynter
SvenSplynter3mo ago
Absolutely
ἔρως
ἔρως3mo ago
lets see what he says because implementing a terminal-like thing is way way harder than it looks, so, using something that already exists and is battle tested is a lot better i used xterm.js before, and it works amazing
SvenSplynter
SvenSplynter3mo ago
Hypothetically speaking, if the prompt doesn't need to come back every time, is it possible to have a 'sticky' first line placeholder text there? Position absolute to place the text, but not sure how you could have padding only on the first line.
ἔρως
ἔρως3mo ago
no, its a text area everything moves you need to write your own logic it is leagues and bounds easier to use xterm.js all you do is .write and dump a ton of ansii escapes done! you can also read input from it, and react to it you also gain support for colors, positioning, cursor clicks and more it is the terminal emulator used by many applications, including vscode
SvenSplynter
SvenSplynter3mo ago
Right. But I just meant... if all they want is an initial 'prompt' that stays in place and the text flows around it, then maybe all of that is overkill
ἔρως
ἔρως3mo ago
it is overkill, but you know what's even more overkill? having to write your own logic
SvenSplynter
SvenSplynter3mo ago
Haha, good point
ἔρως
ἔρως3mo ago
imagine this: there's the prompt and you typed 0 characters you press backspace you have to handle that, to prevent deleting the promot prompt now, you press enter after typing 0 characters you need a new prompt imagine this: i select part of the text and try to delete it, with prompts and all you have to actively fight against me using the textarea what the chrome and firefox console do is to provide an input textarea and an output area which is a less overkill and less impressive way to do it, but much much easier you can then do whatever you want on the output area: including spinning pink fluffy unicorns dancing on rainbows
SvenSplynter
SvenSplynter3mo ago
Stack Overflow
How do I add permanent text before user enters a text in html texta...
I want to add a permanent text in html textarea before user enters a text like in cmd where path is written before you write any code. How can I achieve this?
SvenSplynter
SvenSplynter3mo ago
The first 2 snippets are doing it So you can do it with a position absolute, but then not a padding like I said, but a text-indent. Which makes sense of course.
ἔρως
ἔρως3mo ago
thats not repeatable and doesnt behave like a terminal however, i will admit: i didnt knew that the pseudoelements worked with textareas
SvenSplynter
SvenSplynter3mo ago
Like I said... if all they want is a fixed first text and then have the rest of the text flow around it... terminal behavior would be overkill So this could be a solution But we've done a lot of assumptions based on the initial request. Hahaha
ἔρως
ἔρως3mo ago
yes, for 1 single "line" i think a mixture of my solution and the ::before is the best this one
SvenSplynter
SvenSplynter3mo ago
Now, to go back to the original request... if they want the 2 different colors like in the screenshot... I'm out of options. Haha
ἔρως
ἔρως3mo ago
my solution works with that
SvenSplynter
SvenSplynter3mo ago
I'm not sure what you mean with this output area. But I'd like to know more about it
ἔρως
ἔρως3mo ago
press f12 and then go to the console tab in a browser window
SvenSplynter
SvenSplynter3mo ago
Right, I know about the console.
ἔρως
ἔρως3mo ago
the way it works is as i said: input at the bottom, output at the top
rezabet
rezabet3mo ago
Oh yeah, that's exactly what I'm trying to simulate Also, sorry for the late reply guys
ἔρως
ἔρως3mo ago
better late than never
SvenSplynter
SvenSplynter3mo ago
No worries, we were having fun guessing what you're looking for. Haha
rezabet
rezabet3mo ago
See
No description
ἔρως
ἔρως3mo ago
yeah, just do what i said save your sanity
rezabet
rezabet3mo ago
Ah okay Yeah, I do value that lol
ἔρως
ἔρως3mo ago
then just copy what chrome and firefox do you can use the technique he sent so the text wraps around in the input but, if you want to emulate a full terminal (ssh over socket, for example), then xterm.js is your only option
ChooKing
ChooKing3mo ago
The terminal text is not escaping its container. The starting point of this text is pushed to the right due to the existence of the prompt. Subsequent lines of the same command or output don't have the prompt until the next command. If you are emulating the terminal, just place a prompt in there. Regular text flow takes care of the rest. This is not a CSS issue at all.
rezabet
rezabet3mo ago
You mean I put the prompt in the textarea and start after the prompt? What if the user decides to delete the prompt?
ChooKing
ChooKing3mo ago
That would be a Javascript issue to block the deletion if the prompt is actually in the textarea. It would probably be better to keep the prompt in a separate element that absolutely positioned and indent the textarea to prevent any text from going in the space that the prompt occupies.
SvenSplynter
SvenSplynter3mo ago
Did you check the SO link that I sent? I think it explains pretty well how you can do it with just a pseudo-element and text-indent, if all you want is this initial prompt. Of course, if you want the prompt to come back after the user presses enter, like in a terminal, it's a whole different story.
ChooKing
ChooKing3mo ago
https://codepen.io/chooking/pen/GRLvRrE This is the general idea. The implementation gets a bit more complex when there are more new prompts, since you would need to position their height correctly. Also, setting the proper indent woud be easier using a fixed width font, which I did not do in this example. One trick you could use is a separate textarea for each new prompt. Hide the borders and outline to make them all look like one entity. That would make placing the prompt relative to the textarea simple. The challenge of calculating the height for the next prompt is you don't know in advance how many lines of text the command and/or output will take, so knowing font size and line height is not sufficient.
rezabet
rezabet3mo ago
The challenge of calculating the height for the next prompt is you don't know in advance how many lines of text the command and/or output will take, so knowing font size and line height is not sufficient.
That is true so perhaps the position: absolute method isn't the way to go? I'll check try out your other method though!
it's a whole different story
Oh damn, is that bad or good?
ChooKing
ChooKing3mo ago
You can use position:absolute if you spawn a new pair of prompt and textarea along with their wrapper for each command. The next wrapper will appear right below the previous, and the prompt would be absolutely positioned relative to the wrapper position. But making this work requires tricks for automatic height on the textarea. One option is to use a div with the contenteditable attribute instead of using a textarea. This will also allow you to actually place colored text in the div, and you would just use JS to avoid erasing prompt. Upon further reflection, I think this is the simplest solution.
Aoi
Aoi3mo ago
If you are already dealing with js, you can use a para with float: left; on prompt text https://codepen.io/Nandesh-S/pen/MWRvYba
ChooKing
ChooKing3mo ago
I created a demo that does everything except for preventing deletion of the prompt. I decided to use inline-block and place the prompt inside the container. https://codepen.io/chooking/pen/MWRvwEQ
rezabet
rezabet3mo ago
oo okay, thanks a lot!