« Module:LxTerm » : différence entre les versions

De Adadov.net wiki
Aucun résumé des modifications
Aucun résumé des modifications
 
(112 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
local documentation = require('Module:Documentation').main
local documentation = require('Module:Documentation').main
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
 
local randomString = require('Module:RandomString').randomString
local p = {}
local p = {}


Ligne 8 : Ligne 8 :
local args = getArgs(frame)
local args = getArgs(frame)
local userArg = args.user or nil
local userArg = args.user or nil
local prompt = "$ "
local prompt = "$ "
if not userArg then
if not userArg then
userArg = "user"
userArg = "user"
Ligne 16 : Ligne 14 :
prompt = "# "
prompt = "# "
end
end
return "["..userArg.."@linux] "..prompt
end
function p.main(frame)
local text = frame.args.text
local prompt = _definePrompt(frame)
local result = ""
local sameCommand = false
local ret = ''
local firstLine = true


return "["..userArg.."@linux] "..prompt
local html = mw.html.create('table')
html:cssText('width: 100%')
:addClass('lxterm-code')
 
for line in text:gmatch("[^\n]+") do
line = line:gsub('\n+', '')
ret = ret..line.."\n"
 
local tr = html:tag('tr')
local newline
 
if sameCommand then
local td = tr:tag('td')
td:addClass('prompt')
:css('text-align', 'right')
:wikitext(" > ")
:done()
sameCommand = false
else
local td = tr:tag('td')
td:addClass('prompt')
td:wikitext(prompt)
:done()
end
local td = tr:tag('td')
if not firstLine then
td:attr('colspan', 2)
end
td:wikitext(line)
:done()
if firstLine then
local td = tr:tag('td')
td:addClass('switch')
td:wikitext('dblclick to copy')
:done()
firstLine = false
end
if line:find("\\\s*$") then
sameCommand = true
end
tr:done()
end
-- result = result..tostring(html).."\n"
local pre=mw.html.create('pre')
:addClass('lxterm-code')
:wikitext(result)
:allDone()
local hidden = mw.html.create('div')
hidden:addClass('lxterm-txt')
:css('display', 'none')
:cssText('width: 100%; color: black;')
:wikitext('<nowiki>'..ret..'</nowiki>')
:allDone()
 
local out = mw.html.create('div')
out:wikitext(tostring(html))
:attr('title', 'Double-click pour copier facilement')
:addClass('js-lxterm')
:wikitext(tostring(hidden))
:allDone()
return tostring(out)
end
end


Ligne 23 : Ligne 96 :
local args = getArgs(frame)
local args = getArgs(frame)
local prompt = _definePrompt(frame)
local prompt = _definePrompt(frame)
local text = frame.args.text


local nbcmd = 0
local nbcmd = 0
local ret = ''
local ret = ''
local nbs = ''
local nbs = ''
 
local linenb = true
-- Numéro de commande demandé
-- Numéro de commande demandé
if args.cmdnum then
if args.cmdnum then
Ligne 33 : Ligne 108 :
end
end


html = mw.html.create('table')
local html = mw.html.create('table')
html:addClass('lxterm')
html:addClass('lxterm')
for i, v in pairs(args) do
mw.logObject(args)
if tostring(i):match('^[0-9]+$') then --Match uniquement les index numériques
for txtline in text:gmatch("[^\n]+") do
local v = txtline
if linenb == true then --Compte les commandes si nécessaire
ret = ret..txtline.."\n"
nbcmd = nbcmd+1
end
if linenb == true then --Compte les commandes si nécessaire
 
nbcmd = nbcmd+1
local line=mw.html.create('tr')
end
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
local line=mw.html.create('tr')
if linenb == true then -- Cellule pour le numéro de commande
line:tag('td')
line:tag('td')
:addClass('lxterm-cmd')
:addClass('lxterm-linenb')
:wikitext(prompt.." "..v)
:wikitext(nbcmd)
:done()
:done()
end
local l=0
local linetmp=''


ret = ret..tostring(line)
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 /> > $', '')


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


html:done()
local txt = mw.html.create('pre')
:addClass('lxterm-txt')
:addClass('hide')
:wikitext(ret)
:allDone()


return tostring(html)
return tostring(html)..tostring(txt)
end
end


Ligne 92 : Ligne 171 :
local line=mw.html.create('table')
local line=mw.html.create('table')
line:attr('id','test')
line:attr('id','test')
:addClass('lxterm')
:addClass('lxterm-switch')
:addClass('lxterm-switch')
:tag('tr')
:tag('tr')
Ligne 104 : Ligne 182 :
local hidden = mw.html.create('div')
local hidden = mw.html.create('div')
hidden:attr('id', 'test-hid')
hidden:attr('id', 'test-hid')
:addClass('lxterm-hidden')
:addClass('lxterm-txt')
:cssText('border:1px solid black; background-color: white; width: 70%; margin: 10px auto; padding: 10px; color: black;')
:cssText('border:1px solid black; background-color: white; width: 70%; margin: 10px auto; padding: 10px; color: black;')
:wikitext(hidtxt)
:wikitext(hidtxt)
Ligne 111 : Ligne 189 :


return tostring(line)..tostring(hidden)
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
end


return p
return p

Dernière version du 23 janvier 2023 à 17:35

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 \dblclick to copy
> test2
{{#invoke:LxTerm|main|cmdnum=1|user=root|text=test
test2}}
[root@linux] # testdblclick to copy
[root@linux] # test2
{{#invoke:LxTerm|main|user=nobody|text=test2}}
[nobody@linux] $ test2dblclick to copy
{{#invoke:LxTerm|main|text=test3}}
[user@linux] $ test3dblclick to copy

local documentation = require('Module:Documentation').main
local getArgs = require('Module:Arguments').getArgs
local randomString = require('Module:RandomString').randomString
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.main(frame)
	local text = frame.args.text
	local prompt = _definePrompt(frame)
	local result = ""
	local sameCommand = false
	local ret = ''
	local firstLine = true

	local html = mw.html.create('table')
	html:cssText('width: 100%')
		:addClass('lxterm-code')

	for line in text:gmatch("[^\n]+") do
		line = line:gsub('\n+', '')
		ret = ret..line.."\n"

		local tr = html:tag('tr')
		local newline

		if sameCommand then
			local td = tr:tag('td')
			td:addClass('prompt')
				:css('text-align', 'right')
				:wikitext(" > ")
				:done()
			sameCommand = false
		else
			local td = tr:tag('td')
			td:addClass('prompt')
			td:wikitext(prompt)
			:done()
		end
		local td = tr:tag('td')
		if not firstLine then
			td:attr('colspan', 2)
		end
		td:wikitext(line)
			:done()
		
		if firstLine then
			local td = tr:tag('td')
			td:addClass('switch')
			td:wikitext('dblclick to copy')
			:done()
			firstLine = false
		end			
		
		if line:find("\\\s*$") then
			sameCommand = true
		end
		tr:done()
	end
--	result = result..tostring(html).."\n"
	
	
	local pre=mw.html.create('pre')
		:addClass('lxterm-code')
		:wikitext(result)
		:allDone()
		
	local hidden = mw.html.create('div')
	hidden:addClass('lxterm-txt')
		:css('display', 'none')
		:cssText('width: 100%; color: black;')
		:wikitext('<nowiki>'..ret..'</nowiki>')
		:allDone()

	local out = mw.html.create('div')
	out:wikitext(tostring(html))
		:attr('title', 'Double-click pour copier facilement')
		:addClass('js-lxterm')
		:wikitext(tostring(hidden))
		:allDone()
	return tostring(out)
end

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

	local nbcmd = 0
	local ret = ''
	local nbs = ''
	local linenb = true
	
	-- Numéro de commande demandé
	if args.cmdnum then
		linenb = true
	end

	local html = mw.html.create('table')
	html:addClass('lxterm')
	mw.logObject(args)
	for txtline in text:gmatch("[^\n]+") do
		local v = txtline
		ret = ret..txtline.."\n"
		
		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)
			:allDone()

		html:wikitext(tostring(line))
	end
	html:allDone()

	local txt = mw.html.create('pre')
	:addClass('lxterm-txt')
	:addClass('hide')
	:wikitext(ret)
	:allDone()

	return tostring(html)..tostring(txt)
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-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-txt')
		: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

return p