Advertisement
Alex1979

nc

Jun 19th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.39 KB | None | 0 0
  1.  
  2.  
  3.     --[[ Midday Commander Color Ver. 1.1
  4.          Created by Dimus
  5.          No rights reserved ]]
  6.     local function SetColor(cl)
  7.       term.setTextColor(cl[1])
  8.       term.setBackgroundColor(cl[2])
  9.     end
  10.     local NormalCl, PanelCl, SelectCl, WindowCl, AlarmWinCl
  11.     NormalCl={colors.white,colors.black}
  12.     if term.isColor() then
  13.       PanelCl={colors.white,colors.blue}
  14.       SelectCl={colors.black,colors.cyan}
  15.       WindowCl={colors.white,colors.green}
  16.       AlarmWinCl={colors.white,colors.red}
  17.     else
  18.       PanelCl=NormalCl
  19.       SelectCl={colors.black,colors.white}
  20.       WindowCl=NormalCl
  21.       AlarmWinCl=NormalCl
  22.     end
  23.     local wScr, hScr = term.getSize()
  24.     local wPan = math.ceil(wScr / 2)
  25.     local Menu
  26.     if wScr<51 then
  27.       Menu={'F4 Edt','F5 Cpy','F6 Mov','F7 Dir','F8 Del','F10 Ext'}
  28.     else
  29.       Menu={'F4 Edit','F5 Copy','F6 Move','F7 Dir','F8 Del','F10 Exit'}
  30.     end
  31.     local xMenu=-1
  32.     for i=1,#Menu do xMenu=xMenu+#Menu[i]+1 end
  33.     xMenu=math.ceil((wScr-xMenu) / 2)
  34.      
  35.     local panel ={}
  36.     local Active
  37.      
  38.     local cmdstr=''
  39.     local curpos=0
  40.     function ShowCmd()
  41.       SetColor(NormalCl)
  42.       term.setCursorPos(1, hScr-1)
  43.       term.clearLine()
  44.       term.write(shell.dir()..'> '..cmdstr)
  45.       term.setCursorPos(term.getCursorPos()-curpos, hScr-1)
  46.     end
  47.      
  48.     function panel:ShowFirst()
  49.       local p=self.Path..'/'
  50.       if #p> wPan-6 then p='..'..p:sub(#p-wPan+7) end
  51.       p=' '..p..' '
  52.       term.setCursorPos(self.X, 1)
  53.       term.write('+'..string.rep('-',wPan-2)..'+')
  54.       term.setCursorPos(self.X+(wPan-#p)/2,1)
  55.       if self==Active then
  56.         SetColor(SelectCl)
  57.         term.write(p)
  58.         SetColor(PanelCl)
  59.       else
  60.         term.write(p)
  61.       end
  62.     end
  63.      
  64.     function panel:ShowLine(Line)
  65.       term.setCursorPos(self.X, Line-self.Shift+2)
  66.       if self.tFiles[Line]~=nil then
  67.         local Name=self.tFiles[Line]
  68.         if self.tSize[Line]=='DIR' then Name='/'..Name end
  69.         if #Name>wPan-4 then Name=Name:sub(1,wPan-6)..'..' end
  70.         Name=' '..Name..string.rep(' ',wPan-#Name-4)..' '
  71.         term.write('|')
  72.         if self==Active and Line==self.CurLine then
  73.           SetColor(SelectCl)
  74.           term.write(Name)
  75.           SetColor(PanelCl)
  76.         else
  77.           term.write(Name)
  78.         end
  79.         term.write('|')
  80.       else
  81.         term.write('|'..string.rep(' ',wPan-2)..'|')
  82.       end
  83.     end
  84.      
  85.     function panel:ShowLines()
  86.       for i=self.Shift, self.Shift+hScr-5 do
  87.         self:ShowLine(i)
  88.       end
  89.     end
  90.      
  91.     function panel:ShowLast()
  92.       term.setCursorPos(self.X, hScr-2)
  93.       term.write('+'..string.rep('-',wPan-2)..'+')
  94.       term.setCursorPos(self.X+2, hScr-2)
  95.       write(self.tSize[self.CurLine])
  96.       if self.tSize[self.CurLine]~='DIR' then write(' b') end
  97.     end
  98.      
  99.     function panel:Show()
  100.       SetColor(PanelCl)
  101.       self:ShowFirst()
  102.       self:ShowLines()
  103.       self:ShowLast()
  104.     end
  105.      
  106.     function panel:GetFiles()
  107.       local Files=fs.list(self.Path)
  108.       table.sort(Files)
  109.       if self.Path=='' then
  110.         self.tFiles={}
  111.         self.tSize={}
  112.       else
  113.         self.tFiles={'..'}
  114.         self.tSize={'DIR'}
  115.       end
  116.       for n,Item in pairs(Files) do
  117.         local sPath=fs.combine(self.Path,Item)
  118.         if fs.isDir(sPath) then
  119.           table.insert(self.tFiles,Item)
  120.           table.insert(self.tSize,'DIR')
  121.         end
  122.       end
  123.       for n,Item in pairs(Files) do
  124.         local sPath=fs.combine(self.Path,Item)
  125.         if not fs.isDir(sPath) then
  126.           table.insert(self.tFiles,Item)
  127.           table.insert(self.tSize,fs.getSize(sPath))
  128.         end
  129.       end
  130.       self:Show()
  131.     end
  132.      
  133.     function panel:new(x,path)
  134.       local obj={X = x, Path =path, tFiles={}, tSize={}, CurLine=1, Shift=1}
  135.       setmetatable(obj,self)
  136.       self.__index=self
  137.       return obj
  138.     end
  139.      
  140.     local Left =panel:new(1,'')
  141.     local Rght =panel:new(wPan,shell.dir())
  142.      
  143.     Active =Rght
  144.      
  145.     local function ShowPanels()
  146.       SetColor(NormalCl)
  147.       term.clear()
  148.       Left:GetFiles()
  149.       Rght:GetFiles()
  150.       term.setCursorPos(xMenu, hScr)
  151.       for i=1,#Menu do
  152.       SetColor(SelectCl)
  153.       term.write(Menu[i])
  154.       SetColor(NormalCl)
  155.       term.write(' ')
  156.       end
  157.       term.setCursorBlink(true)
  158.     end
  159.      
  160.     local function Dialog(cl,Lines,Str,But)
  161.       SetColor(cl)
  162.       local H=#Lines+3
  163.       local CurBut=1
  164.       if Str then H=H+1 CurBut=0 end
  165.       if not But then But={'Ok'} end
  166.       local function Buttons()
  167.         local Butt=''
  168.         for i=1,#But do
  169.           if i==CurBut then
  170.             Butt=Butt..'['..But[i]..']'
  171.           else
  172.             Butt=Butt..' '..But[i]..' '
  173.           end
  174.         end
  175.         return Butt
  176.       end
  177.       local W=#Buttons()
  178.       for i=1,#Lines do
  179.         if #Lines[i]>W then W=#Lines[i] end
  180.       end
  181.       if Str and (#Str>W) then W=#Str end
  182.       W=W+4
  183.       local x= math.ceil((wScr-W)/2)
  184.       local y= math.ceil((hScr-H)/2)
  185.       term.setCursorPos(x, y)
  186.       term.write(string.rep('=',W))
  187.       for i=1,#Lines+2 do
  188.         term.setCursorPos(x, y+i)
  189.         term.write('H'..string.rep(' ',W-2)..'H')
  190.       end
  191.       term.setCursorPos(x, y+H-1)
  192.       term.write(string.rep('=',W))
  193.       for i=1,#Lines do
  194.         term.setCursorPos(x+(W-#Lines[i])/2, y+i)
  195.         term.write(Lines[i])
  196.       end
  197.      
  198.       while true do
  199.         term.setCursorBlink(CurBut==0)
  200.         term.setCursorPos(x+(W-#Buttons())/2, y+H-2)
  201.         term.write(Buttons())
  202.         if CurBut==0 then
  203.           term.setCursorPos(x+2, y+H-3)
  204.           local S=Str
  205.           if #S>W-4 then S='..'..S:sub(#S-W+7) end
  206.           term.write(S)
  207.         end
  208.         local event,p1=os.pullEvent()
  209.         if event=='key' then
  210.           if p1==keys.enter then
  211.             if CurBut==0 then CurBut=1 end
  212.             return But[CurBut],Str
  213.           elseif p1==keys.left and CurBut~=0 then
  214.             if CurBut>1 then CurBut=CurBut-1 end
  215.           elseif p1==keys.right and CurBut~=0 then
  216.             if CurBut<#But then CurBut=CurBut+1 end
  217.           elseif p1==keys.tab then
  218.             if CurBut<#But then CurBut=CurBut+1
  219.             else CurBut=Str and 0 or 1
  220.             end
  221.           elseif p1==keys.backspace and CurBut==0 and #Str>0 then
  222.             term.setCursorPos(x, y+H-3)
  223.             term.write('H'..string.rep(' ',W-2)..'H')
  224.             Str=Str:sub(1,#Str-1)
  225.           end
  226.         elseif event=='char' and CurBut==0 then
  227.           Str=Str..p1
  228.         end
  229.       end
  230.     end
  231.      
  232.     local cmd, Alt, Ok, Name
  233.      
  234.     local function CopyMove(action,func)
  235.       Name = ((Active==Rght) and Left or Rght).Path..'/'..cmd
  236.       cmd=Active.Path..'/'..cmd
  237.       Ok,Name=Dialog(WindowCl,{action,cmd,'to:'},Name,{'<Ok>','Cansel'})
  238.       if Ok=='<Ok>' then
  239.         if cmd==Name then
  240.           Dialog(AlarmWinCl,{'Cannot copy/move file to it self!'})
  241.         else
  242.           if fs.exists(Name) then
  243.             if Dialog(AlarmWinCl,{'File already exists!',Name,'Overwrite it?'},nil,{'Yes','No'})=='Yes' then
  244.               fs.delete(Name)
  245.               func(cmd, Name)
  246.             end
  247.           else
  248.             func(cmd, Name)
  249.           end
  250.         end
  251.       end
  252.       ShowPanels()
  253.     end
  254.      
  255.     local Line
  256.     local work=true
  257.     local eventKey={}
  258.     eventKey[keys.up]=function()
  259.       if Active.CurLine>1 then
  260.         Line,Active.CurLine=Active.CurLine,Active.CurLine-1
  261.         if Active.CurLine<Active.Shift then
  262.           Active.Shift=Active.CurLine
  263.           Active:ShowLines()
  264.         else
  265.           Active:ShowLine(Active.CurLine)
  266.           Active:ShowLine(Line)
  267.         end
  268.         Active:ShowLast()
  269.       end
  270.     end
  271.      
  272.     eventKey[keys.down]=function()
  273.       if Active.CurLine<#Active.tFiles then
  274.         Line,Active.CurLine=Active.CurLine,Active.CurLine+1
  275.         if Active.CurLine>Active.Shift+hScr-5 then
  276.           Active.Shift=Active.CurLine-hScr+5
  277.           Active:ShowLines()
  278.         else
  279.           Active:ShowLine(Active.CurLine)
  280.           Active:ShowLine(Line)
  281.         end
  282.         Active:ShowLast()
  283.       end
  284.     end
  285.      
  286.     eventKey[keys.left]=function()
  287.       if curpos<#cmdstr then curpos=curpos+1 end
  288.     end
  289.      
  290.     eventKey[keys.right]=function()
  291.       if curpos>0 then curpos=curpos-1 end
  292.     end
  293.      
  294.     eventKey[keys.tab]=function()
  295.       Active=(Active==Rght) and Left or Rght
  296.       shell.setDir(Active.Path)
  297.       Left:ShowFirst()
  298.       Rght:ShowFirst()
  299.       Left:ShowLine(Left.CurLine)
  300.       Rght:ShowLine(Rght.CurLine)
  301.     end
  302.      
  303.     eventKey[keys.enter]=function()
  304.       local function exec(cmd)
  305.         SetColor(NormalCl)
  306.         term.clear()
  307.         term.setCursorPos(1,1)
  308.         shell.run(cmd)
  309.         sleep(3)
  310.         ShowPanels()
  311.       end
  312.       curpos=0
  313.       if Alt=='C' then
  314.         cmdstr=cmdstr..cmd..' '
  315.       else
  316.         if cmdstr~='' then
  317.           exec(cmdstr)
  318.           cmdstr=''
  319.           return
  320.         end
  321.         if Active.tSize[Active.CurLine]=='DIR' then
  322.           local NewDir=shell.resolve(cmd)
  323.           shell.setDir(NewDir)
  324.           Active.Path=NewDir
  325.           Active.CurLine=1
  326.           Active.Shift=1
  327.           Active:GetFiles()
  328.         else
  329.           exec(cmd)
  330.         end
  331.       end
  332.     end
  333.      
  334.     eventKey[keys.backspace]=function()
  335.       if cmdstr~='' then
  336.         if curpos==0 then cmdstr=cmdstr:sub(1,-2)
  337.         else cmdstr=cmdstr:sub(1,-2-curpos)..cmdstr:sub(-curpos)
  338.         end
  339.       end
  340.     end
  341.      
  342.     eventKey[keys.delete]=function()
  343.       if cmdstr~='' then
  344.         if curpos>0 then
  345.           curpos=curpos-1
  346.           if curpos==0 then
  347.             cmdstr=cmdstr:sub(1,-2)
  348.           else
  349.             cmdstr=cmdstr:sub(1,-2-curpos)..cmdstr:sub(-curpos)
  350.           end
  351.         end
  352.       end
  353.     end
  354.      
  355.     eventKey[keys['end']]=function() curpos=0 end
  356.      
  357.     eventKey[keys.home]=function() curpos=#cmdstr end
  358.      
  359.     eventKey[keys.leftShift]=function()
  360.       Alt='S'
  361.       os.startTimer(0.5)
  362.     end
  363.     eventKey[keys.rightShift]=eventKey[keys.leftShift]
  364.      
  365.     eventKey[keys.leftCtrl]=function()
  366.       Alt='C'
  367.       os.startTimer(0.5)
  368.     end
  369.     eventKey[keys.rightCtrl]=eventKey[keys.leftCtrl]
  370.      
  371.     eventKey[keys.f4]=function()
  372.       if Alt=='S' then
  373.         Alt=''
  374.         Ok,Name=Dialog(WindowCl,{'File name:'},'',{'<Ok>','Cansel'})
  375.         if Ok=='<Ok>' then
  376.           shell.run('/rom/programs/edit '..Name)
  377.         end
  378.       else
  379.         if Active.tSize[Active.CurLine]=='DIR' then
  380.           Dialog(AlarmWinCl,{'Error!', cmd, 'is not a file'})
  381.         else
  382.           shell.run('/rom/programs/edit '..cmd)
  383.         end
  384.       end
  385.       ShowPanels()
  386.     end
  387.      
  388.     eventKey[keys.f5]=function()
  389.       CopyMove('Copy file:',fs.copy)
  390.     end
  391.      
  392.     eventKey[keys.f6]=function()
  393.       CopyMove('Move file:',fs.move)
  394.     end
  395.      
  396.     eventKey[keys.f7]=function()
  397.       Ok,Name=Dialog(WindowCl,{'Directory name:'},'',{'<Ok>','Cansel'})
  398.       if Ok=='<Ok>' then
  399.         if Name=='..' or fs.exists(shell.resolve(Name)) then
  400.           ShowPanels()
  401.           Dialog(AlarmWinCl,{' File exists '})
  402.         else
  403.           fs.makeDir(shell.resolve(Name))
  404.         end
  405.       end
  406.       ShowPanels()
  407.     end
  408.      
  409.     eventKey[keys.f8]=function()
  410.       if cmd=='..' then
  411.         Dialog(AlarmWinCl,{'Cannot operate on ".."!'})
  412.       else
  413.         if Dialog(AlarmWinCl,{'Do you want to delete', cmd..'?'}, nil, {'Yes','No'})=='Yes' then
  414.           fs.delete(shell.resolve(cmd))
  415.           if Active.CurLine==#Active.tFiles then Active.CurLine=Active.CurLine-1 end
  416.         end
  417.       end
  418.       ShowPanels()
  419.     end
  420.      
  421.     eventKey[keys.f10]=function()
  422.       work=false
  423.     end
  424.      
  425.     ShowPanels()
  426.     ShowCmd()
  427.     local Line
  428.     while work do
  429.       local event,p1=os.pullEvent()
  430.       cmd=Active.tFiles[Active.CurLine]
  431.       if event=='char' then
  432.         if curpos==0 then cmdstr=cmdstr..p1
  433.         else cmdstr=cmdstr:sub(1,-1-curpos)..p1..cmdstr:sub(-curpos)
  434.         end
  435.         ShowCmd()
  436.       elseif event=='key' then
  437.         if eventKey[p1]~=nil then
  438.           SetColor(PanelCl)
  439.           eventKey[p1]()
  440.           ShowCmd()
  441.         end
  442.       elseif event=='timer' then
  443.         Alt=''
  444.       end
  445.     end
  446.     SetColor(NormalCl)
  447.     term.setCursorPos(1, hScr)
  448.     term.clearLine()
  449.     print('Thank you for using Midday Commander!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement