Textutils API

From ComputerCraft
Jump to: navigation, search

Textutils.complete

FunctionFunctiontextutils.formatTime
Formats time (in ticks) in a human-readable format such as "6:30 PM". If twentyFourHour is set to true, the AM/PM prefix is omitted, and the time is presented in a 24-hour format such as "18:30".
Syntax textutils.formatTime()
Returns string
Part of CC:Tweaked (source)
API textutils
ExampleExampleWith 12-hour format
Prints the Minecraft world time in a 12-hour human-readable format.
Code
print(textutils.formatTime(os.time()))
Output Depends on the world time, for example "6:30 PM".
ExampleExampleWith 24-hour format
Prints the Minecraft world time in a 24-hour human-readable format.
Code
print(textutils.formatTime(os.time(), true))
Output Depends on the world time, for example "18:30".

Textutils.pagedPrint Textutils.pagedTabulate

FunctionFunctiontextutils.serialise
Converts value to a string representing it, which may be converted back to a value of the original type using textutils.unserialise.
Syntax textutils.serialise()
Returns string
Part of CC:Tweaked (source)
API textutils
ExampleExampleSerialising a table
Prints a string representation of the given table.
Code
print(textutils.serialise({ 1, 2, foo = "bar", baz = { 1, 2, 3 } })
Output
{
  1,
  2,
  baz = {
    1,
    2,
    3,
  },
  foo = "bar"
}
ExampleExampleSaving a table to a file
Converts the given table to a string representing it, stores it in a file, then reads it back.
Code
local t = { 1, 2, 3 }
print(t[1])
local f = fs.open("foo", "w")
f.write(textutils.serialise(t))
f.close()

local f = fs.open("foo", "r")
local t2 = textutils.unserialise(f.readAll())
f.close()
print(t2[1])
Output 1 1

Textutils.serialiseJSON

FunctionFunctiontextutils.slowPrint
Slowly prints text, character-by-character, at the current cursor position in the current term. Accepts an optional rate parameter, in characters per second (default 20). This function is similar to textutils.slowWrite, however it automatically moves to the next line after writing.
Syntax textutils.slowPrint()
Returns nil
Part of CC:Tweaked (source)
API textutils
See also textutils.slowWrite
ExampleExampleWith default rate
Slowly prints "Hello, world!" to the current term.
Code
textutils.slowPrint("Hello, world!")
ExampleExampleWith custom rate
Slowly prints "Hello, world!" to the current term, at 5 characters per second.
Code
textutils.slowPrint("Hello, world!", 5)


FunctionFunctiontextutils.slowWrite
Slowly writes text, character-by-character, at the current cursor position in the current term. Accepts an optional rate parameter, in characters per second (default 20). This function does not automatically move to the next line after writing.
Syntax textutils.slowWrite()
Returns nil
Part of CC:Tweaked (source)
API textutils
See also textutils.slowPrint
ExampleExampleWith default rate
Slowly writes "Hello, world!" to the current term.
Code
textutils.slowWrite("Hello, world!")
ExampleExampleWith custom rate
Slowly writes "Hello, world!" to the current term, at 5 characters per second.
Code
textutils.slowWrite("Hello, world!", 5)

Textutils.tabulate

FunctionFunctiontextutils.unserialise
Converts a serialised string to a reassembled Lua value - the opposite of textutils.serialise. Returns nil if the unserialisation failed.
textutils.unserialise is implemented using load (source). It is loaded with an empty environment, therefore strings passed to textutils.unserialise cannot cause any harm to the computer. However, they can cause an infinite loop and cause the computer to crash for failing to yield!
Syntax textutils.unserialise()
Returns any
Part of CC:Tweaked (source)
API textutils
ExampleExampleDeserialising a serialised table
Prints a value of the given serialised table.
Code
local serialised = "{ foo = \"bar\", fruit = \"apple\", colour = \"yellow\" }"
print(textutils.unserialise(serialised).fruit)
Output
apple

Textutils.urlEncode