Advertisement
AdslHouba

Boite à fonction utilitaire

Feb 8th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.02 KB | None | 0 0
  1. addPeripheral=function(id)
  2.     if not(type(id)=="string") then
  3.         error(tostring(id)..' nom perif incorrect')
  4.     end
  5.     if not(peripheral.isPresent(id)) then
  6.         error(id..' non connecte')
  7.         shell.exit()
  8.     end
  9.     return {
  10.         id=id,
  11.         pp=peripheral.wrap(id)
  12.     }
  13. end
  14. function center(texte,mon,y)
  15.     x=mon.getSize()
  16.     if #texte>x then
  17.         mon.setCursorPos(1,y)
  18.         mon.write(string.sub(texte,1,x-3)..'...')
  19.     else
  20.         mon.setCursorPos(math.floor(x/2-#texte/2+0.5),y)
  21.         mon.write(texte)
  22.     end
  23. end
  24. colorsBlit={
  25.             [colors.white]='0',
  26.             [colors.orange]='1',
  27.             [colors.magenta]='2',
  28.             [colors.lightBlue]='3',
  29.             [colors.yellow]='4',
  30.             [colors.lime]='5',
  31.             [colors.pink]='6',
  32.             [colors.gray]='7',
  33.             [colors.lightGray]='8',
  34.             [colors.cyan]='9',
  35.             [colors.purple]='a',
  36.             [colors.blue]='b',
  37.             [colors.brown]='c',
  38.             [colors.green]='d',
  39.             [colors.red]='e',
  40.             [colors.black]='f'
  41.             }
  42. function centerBlit(texte,mon,y,tx,bg)
  43.     if type(tx)=='number' and colorsBlit[tx] then
  44.         tx=string.rep(colorsBlit[tx],#texte)
  45.     end
  46.     if type(bg)=='number' and colorsBlit[bg] then
  47.         bg=string.rep(colorsBlit[bg],#texte)
  48.     end    
  49.     z=mon.getSize()
  50.     print(z,#texte)
  51.     if #texte>z then
  52.         mon.setCursorPos(1,y)
  53.         mon.blit(string.sub(texte,1,z-3)..'...',string.sub(tx,1,z),string.sub(bg,1,z))
  54.     else
  55.         mon.setCursorPos(math.floor(z/2-#texte/2+0.5),y)
  56.         print(texte)
  57.         mon.blit(texte,tx,bg)
  58.     end
  59. end
  60. configFileFc=function(programName)
  61.     return './config123/'..programName.."Config"
  62. end
  63. -- config("NomDuProg",{
  64. --  nomVariable={
  65. --    typ="string", -- choix, side, coord, couleur, boolean, table
  66. --    info="Information complementaire",
  67. --    defaut=""  -- proposition
  68. --    choix={"choix 1","choix 2"}
  69. --  }
  70. -- })
  71. config=function(programName,pConfig)
  72.     local configFile=configFileFc(programName)
  73.     local config={}
  74.     local ok=true
  75.     if not(fs.isDir('config123')) then
  76.         fs.makeDir('config123')
  77.     end
  78.     if not(fs.exists(configFile)) then
  79.         ok=false
  80.     else
  81.         file=fs.open(configFile,'r')
  82.         config=textutils.unserialize(file.readAll())
  83.         file.close()
  84.         for key,data in pairs(pConfig) do
  85.             if config[key]==nil then
  86.                 ok=false
  87.             end
  88.         end
  89.     end
  90.    
  91.     if not(ok) then
  92.         term.clear()
  93.         term.setCursorPos(1,1)
  94.         print('Pas de fichier de config trouve')
  95.         print('Creation de fichier de config ')
  96.         sleep(0.5)
  97.        
  98.         for key,data in pairs(pConfig) do
  99.             if config[key]==nil then
  100.                 term.setBackgroundColor(colors.white)
  101.                 term.setTextColor(colors.black)
  102.                 term.clear()
  103.                 term.setCursorPos(1,1)
  104.                 question="Reglez "..key
  105.                 if data.info~=nil then
  106.                     question=question..data.info
  107.                 end            
  108.                 print(question)
  109.                 if data.typ=="side" then
  110.                     data.choix={"front","back","left","right","top","bottom","autre"}
  111.                 end
  112.                 if data.typ=="coord" then
  113.                     data.choix={'EAST','WEST','SOUTH','NORTH','UP','DOWN'}
  114.                     data.typ="choix"
  115.                 end
  116.                 if data.typ=="couleur" then
  117.                     data.choix={
  118.                         [colors.white]="white",
  119.                         [colors.orange]="orange",  
  120.                         [colors.magenta]="magenta",
  121.                         [colors.lightBlue]="lightBlue",
  122.                         [colors.yellow]="yellow",
  123.                         [colors.lime]="lime",
  124.                         [colors.pink]="pink",
  125.                         [colors.gray]="gray",
  126.                         [colors.lightGray]="lightGray",
  127.                         [colors.cyan]="cyan",
  128.                         [colors.purple]="purple",
  129.                         [colors.blue]="blue",
  130.                         [colors.brown]="brown",
  131.                         [colors.green]="green",
  132.                         [colors.red]="red",
  133.                         [colors.black]="black"
  134.                     }
  135.                 end
  136.                 if data.typ=="boolean" then
  137.                     data.choix={
  138.                         [true]="true",
  139.                         [false]="false"
  140.                     }
  141.                     data.typ="choix2"
  142.                 end
  143.                
  144.                 if data.typ=="string" then
  145.                     if data.defaut~=nil then
  146.                         term.setCursorPos(1,3)
  147.                         term.write("Si vide defaut : "..data.defaut)
  148.                     end
  149.                     term.setCursorPos(1,2)
  150.                     local answer=read()
  151.                     if data.defaut~=nil and answer=='' then
  152.                         config[key]=data.defaut
  153.                     else
  154.                         config[key]=answer
  155.                     end
  156.                    
  157.                 elseif data.typ=="choix" or data.typ=="choix2" or data.typ=="couleur" or data.typ=="side" then
  158.                     j=1
  159.                     for ck,cv in pairs(data.choix) do
  160.                         if data.typ=="couleur" then
  161.                             term.setBackgroundColor(ck)
  162.                         end
  163.                         term.setCursorPos(1,j+1)
  164.                         term.write(cv)
  165.                         j=j+1
  166.                     end
  167.                     local bool=true
  168.                     while bool do
  169.                         ev,button,x,y=os.pullEvent("mouse_click")
  170.                         j=1
  171.                         for ck,cv in pairs(data.choix) do
  172.                             if y==j+1 then
  173.                                 if data.typ=="choix" then
  174.                                     config[key]=cv
  175.                                 elseif data.typ=="side" then
  176.                                     if cv=="autre" then
  177.                                         term.setCursorPos(1,10)
  178.                                         local answer=read()
  179.                                         config[key]=answer
  180.                                     else
  181.                                         config[key]=cv
  182.                                     end
  183.                                 else
  184.                                     config[key]=ck
  185.                                 end
  186.                                 bool=false
  187.                             end
  188.                             j=j+1
  189.                         end
  190.                     end                
  191.                 elseif data.typ=="table" then
  192.                     config[key]={}
  193.                 end            
  194.             end
  195.         end
  196.         term.setBackgroundColor(colors.black)
  197.         term.setTextColor(colors.white)
  198.         term.clear()
  199.         term.setCursorPos(1,1) 
  200.         file=fs.open(configFile,'w')
  201.         file.write(textutils.serialize(config))
  202.         file.close()
  203.     end  
  204.     return config
  205. end
  206. configTab=function(programName,var,action,key,value)
  207.     local configFile=configFileFc(programName)
  208.     if not(fs.exists(configFile)) then
  209.         error('ahb.configTab doit etre utiliser apres ahb.config')
  210.     else
  211.         file=fs.open(configFile,'r')
  212.         config=textutils.unserialize(file.readAll())
  213.         file.close()
  214.         if action=='add' then
  215.             if key==false then
  216.                 table.insert(config[var],value)
  217.             else
  218.                 config[var][key]=value
  219.             end
  220.         elseif action=='maj' then
  221.             config[var][key]=value
  222.         elseif action=='remove' then
  223.             table.remove(var)
  224.         end
  225.         fs.delete(configFile)
  226.         file=fs.open(configFile,'w')
  227.         file.write(textutils.serialize(config))
  228.         file.close()
  229.     end
  230. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement