Mô đun:OutputBuffer

Bách khoa toàn thư mở Wikipedia
Tài liệu mô đun[xem] [sửa] [lịch sử] [làm mới]

This module provides an easy and efficient way to generate lengthy strings.

Cách sử dụng[sửa mã nguồn]

First, load the module.

local newBuffer = require('Mô đun:OutputBuffer')

Then, create the buffer and the functions that act on it.

local getBuffer, print, printf = newBuffer()

getBuffer[sửa mã nguồn]

local text = getBuffer(sep)

Returns the contents of the buffer, with an optional separator string sep.

print[sửa mã nguồn]


Adds the string s to the buffer.

printf[sửa mã nguồn]

printf(s, ...)

Adds the string s to the buffer. The string is formatted with any subsequent arguments, following the rules used for string.format.

Ví dụ[sửa mã nguồn]

local newBuffer = require('Mô đun:OutputBuffer')
local p = {}

function p.main()
	local getBuffer, print, printf = newBuffer()
	print('Welcome to the English Wikipedia.')
	printf('There are currently %d articles on this site.', mw.site.stats.articles)
	return getBuffer(' ')

return p

-- Assuming this module's name is "TestModule", and that mw.site.stats.articles returns 4500000,
-- {{#gọi:TestModule|main}} would output:
-- "Welcome to the English Wikipedia. There are currently 4500000 articles on this site."

return function()
	local buffer = {}
	return function(sep)
		local b = buffer
		buffer = {}
		return table.concat(b, sep)
		buffer[#buffer + 1] = text
		buffer[#buffer + 1] = string.format(...)