Main Menu

Text boxes?

Started by SquaredNES, May 21, 2024, 03:53 PM

Previous topic - Next topic

SquaredNES

I'm trying to work on a program to read markdown format and need a way to display block quotes.
basically, i want to know how to indent text

Missooni

#1
Quote from: SquaredNES on May 21, 2024, 03:53 PMI'm trying to work on a program to read markdown format and need a way to display block quotes.
basically, i want to know how to indent text

There's a couple of ways to indent text, here's one of them:
How to Indent Text
For this example I'll make two files, text.txt and writeIndented.lua

text.txt
Code: Text
  1. > This is text in a file, contained in a markdown blockquote.
  2. -and this text is not in a blockquote!

writeIndented.lua
Code: Lua
  1. -- Get the terminal width so that we can wrap text in blockquotes.
  2. local termX, termY = term.getSize()
  3. -- Make an empty table for all of the lines from the text file to be stored.
  4. local lines = {}
  5.  
  6. -- Reads the text file and stores all lines in our table.
  7. for line in io.lines("text.txt") do
  8.   table.insert(lines, line)
  9. end
  10.  
  11. -- Reads each of the lines and performs an action depending on the syntax.
  12. for i, str in pairs(lines) do
  13.   if str:find(">") and str:sub(1,1) == ">" then
  14. -- The 'pos' variable is created so that it can parse text that is written >like this and > like this.
  15.   local pos = 2
  16.   if str:find("> ") then pos = 3 end
  17.   str = str:sub(pos)
  18. -- If the string is longer than the terminal width it will cut it down and re-add entries to the table.
  19.   if #str > termX - 3 then
  20.   table.insert(lines, i+1, ">"..str:sub(termX - 3))
  21.   str = str:sub(0,(termX - 3))
  22.   end
  23. -- Adds a unicode symbol and two spaces in front of formatted text.
  24.   str = "\149  "..str
  25.   end
  26.   print(str)
  27. end
Output:
https://i.imgur.com/OiTVt0I.png
" We are like the spider. We weave our life and then move along in it.
We are like the dreamer, who dreams and then lives in the dream. "
- Aitareya Upanishad

QuickMuffin8782

Quote from: Missooni on Sep 10, 2024, 08:44 PM-formatting snip-
Quote from: SquaredNES on May 21, 2024, 03:53 PM-formatting -snip
Surprisingly, my remix of JackMackWindows' PrimeUI, I am adding Markdown, so this is definitely helpful code.
"The Blue Blur" - Working on Net Star OS, a new frontier for Red Star OS 3.0
linktr.ee/quickmuffin8782