Module:LxTerm

De Adadov.net wiki
Documentation icon Documentation Module[voir] [éditer] [historique] [purger]

Ce module implémente {{LxTerm}}, il met en forme les lignes de commande et ajoute les prompt.

{{#invoke:LxTerm|main|cmdnum=1|user=root|text=test \
test2}}
[root@linux] # test \
 > test2
{{#invoke:LxTerm|main|cmdnum=1|user=root|text=test
test2}}
[root@linux] # test
[root@linux] # test2
{{#invoke:LxTerm|main|user=nobody|text=test2}}
[nobody@linux] $ test2
{{#invoke:LxTerm|main|text=test3}}
[user@linux] $ test3

local documentation = require('Module:Documentation').main
local getArgs = require('Module:Arguments').getArgs

local p = {}

local function _definePrompt(frame)
	--[{{{user|user}}}@linux] {{#ifeq: {{{user}}}|root|# |$ }}
	local args = getArgs(frame)
	local userArg = args.user or nil

	local prompt = "$ "

	if not userArg then
		userArg = "user"
	elseif userArg == "root" then
		prompt = "# "
	end

	return "["..userArg.."@linux] "..prompt
end

function p.mulCommands(frame)
	local args = getArgs(frame)
	local prompt = _definePrompt(frame)

	local nbcmd = 0
	local ret = ''
	local nbs = ''

	-- Numéro de commande demandé
	if args.cmdnum then
		linenb = true
	end

	html = mw.html.create('table')
	html:addClass('lxterm')
	for i, v in pairs(args) do
		if tostring(i):match('^[0-9]+$') then --Match uniquement les index numériques
			
			if linenb == true then --Compte les commandes si nécessaire
				nbcmd = nbcmd+1
			end

			local line=mw.html.create('tr')
			if linenb == true then -- Cellule pour le numéro de commande
				line:tag('td')
					:addClass('lxterm-linenb')
					:wikitext(nbcmd)
					:done()
			end
			local l=0
			local linetmp=''

			v = v:gsub('([^\n]+)\n', '%1 <br /> > ') -- On met les retour à la ligne pour les commandes sur plusieurs lignes
			v = v:gsub('\\ <br />', '<br />') -- On supprime les \ présents
			v = v:gsub('<br />', '\\ <br />') -- Pour en ajouter partout
			v = v:gsub('\\ <br /> > $', '')

			-- Création de la cellule pour la commande
			line:tag('td')
				:addClass('lxterm-cmd')
				:wikitext(prompt.." "..v)
				:done()

			ret = ret..tostring(line)

			html:wikitext(tostring(line))
		end
	end

	html:done()

	return tostring(html)
end

function p.singleCommand(frame)
	local args = getArgs(frame)
	local prompt = _definePrompt(frame)
	local sameCommand = false
	local v = args.text or frame.args[1]

	local result = ""
	
	v = v:gsub('([^\n]+)\n', '%1 <br /> > ') -- On met les retour à la ligne pour les commandes sur plusieurs lignes
	v = v:gsub('\\ <br />', '<br />') -- On supprime les \ présents
	v = v:gsub('<br />', '\\ <br />') -- Pour en ajouter partout
	v = v:gsub('\\ <br /> > $', '')

	local script = mw.html.create('script')
	script:wikitext('')

	local line=mw.html.create('table')
	line:attr('id','test')
		:addClass('lxterm')
		:addClass('lxterm-switch')
		:tag('tr')
		:tag('td')
		:addClass('lxterm-cmd')
		:wikitext(prompt..v)
		:allDone()

	local hidtxt = v:gsub(' > ', '')
	hidtxt = hidtxt:gsub('<br />', '\n')
	local hidden = mw.html.create('div')
	hidden:attr('id', 'test-hid')
		:addClass('lxterm-hidden')
		:cssText('border:1px solid black; background-color: white; width: 70%; margin: 10px auto; padding: 10px; color: black;')
		:wikitext(hidtxt)
		:wikitext('<a href="" id="test-btn">switch</a>')
		:allDone()

	return tostring(line)..tostring(hidden)
end

function p.main(frame)
	local text = frame.args.text
	local prompt = _definePrompt(frame)
	local result = ""
	local sameCommand = false
	
	for line in text:gmatch("[^\n]+") do
		local newline
		if sameCommand then
			newline = " > "..line
			sameCommand = false
		else
			newline = prompt..line
		end
		
		if line:find("\\ *$") then
			sameCommand = true
		end
		result = result..newline.."\n"
	end

	local pre=mw.html.create('pre')
		:wikitext(result)
		:allDone()
	return tostring(pre)
end

return p