Advertisement
ZNZNCOOP

MC pocket PC

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