This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
master39's profile picture

[CCPrinter] PEDIT, text editor with print option

Started by master39, 14 June 2012 - 12:13 PM
master39 #1
Posted 14 June 2012 - 02:13 PM
This program is a text editor with some new functions that permit to simple use the CCprinter peripheral without programming

Requirements:
-obviously CCPrinter peripheral by louitzie

Features:
-print in pages if the text is too long for one sheet only
-different printing colors for lines:
-!r red
-!b blue
-!g green
-!w white
-!s standard (grey)

Future plans:
-scan function
-colored paper

Copy the code in a file called pedit in your "%appdata%.minecraftmodscomputercraftluaromprograms" folder

Code:
Spoiler



local tArgs = { ... }
if #tArgs == 0 then
print( "Usage: pedit <path>" )
return
end

local sPath = shell.resolve( tArgs[1] )
local bReadOnly = fs.isReadOnly( sPath )
if fs.exists( sPath ) and fs.isDir( sPath ) then
print( "Cannot edit a directory" )
return
end

local w,h = term.getSize()
local x,y = 1,1
local scrollX, scrollY = 0,0

local bMenu = false
local nMenuItem = 1

local sStatus = ""
local tLines = {}

local function load()
tLines = {}
if fs.exists( sPath ) then
local file = io.open( sPath, "r" )
local sLine = file:read()
while sLine do
table.insert( tLines, sLine )
sLine = file:read()
end
file:close()
else
table.insert( tLines, "" )
end
end

local function save()
local file = io.open( sPath, "w" )
if file then
for n, sLine in ipairs( tLines ) do
file:write( sLine .. "n" )
end
file:close()
return true
end
return false
end
function findside()
  if peripheral.getType("bottom")=="printer" then side="bottom"
  elseif peripheral.getType("front")=="printer" then side="front"
  elseif peripheral.getType("left")=="printer" then side="left"
  elseif peripheral.getType("right")=="printer" then side="right"
  elseif peripheral.getType("top")=="printer" then side="top"
  elseif peripheral.getType("back")=="printer" then side="back"
  end
  return side
end
local function colprint(l,str)
    --18,30,34,50
    if string.find(str,"!r") then 
     cl=18
     str2=string.gsub(str,"!r","")
    elseif string.find(str,"!b") then 
     cl=50
     str2=string.gsub(str,"!b","") 
    elseif string.find(str,"!g") then 
     cl=30
     str2=string.gsub(str,"!g","") 
    elseif string.find(str,"!w") then 
     cl=34
     str2=string.gsub(str,"!w","") 
    elseif string.find(str,"!s") then 
     cl=12
     str2=string.gsub(str,"!s","")
    else str2=str
    end
    printer.drawText(1,l,str2,cl)
end
local function print()
    local side=findside()    
    printer.open(side)
    local l=1
    local col=12
    for n, sLine in ipairs( tLines ) do
     if l<13 then
      col=colprint(l,sLine)      
      l=l+1
     else
      printer.eject()
      sleep(0.1)
      printer.drawText(1,1,sLine.."n",col)
      l=2
     end
    end
   printer.eject()
end
local function redrawText()
for y=1,h-1 do
term.setCursorPos( 1 - scrollX, y )
term.clearLine()

local sLine = tLines[ y + scrollY ]
if sLine ~= nil then
term.write( sLine )
end
end
term.setCursorPos( x - scrollX, y - scrollY )
end

local function redrawLine()
local sLine = tLines[y]
term.setCursorPos( 1 - scrollX, y - scrollY )
term.clearLine()
term.write( sLine )
term.setCursorPos( x - scrollX, y - scrollY )
end

local tMenuItems = { "Save", "Exit", "Print" }

local function redrawMenu()
    term.setCursorPos( 1, h )
term.clearLine()

if not bMenu then
term.write( sStatus )
else
for n,sItem in ipairs( tMenuItems ) do
if n == nMenuItem then
term.write( "["..sItem.."]" )
else
term.write( " "..sItem.." " )
end
end
end

    term.setCursorPos( x - scrollX, y - scrollY )
end

local function setStatus( _sText )
sStatus = _sText
redrawMenu()
end

local function setCursor( x, y )
local screenX = x - scrollX
local screenY = y - scrollY

local bRedraw = false
if screenX < 1 then
scrollX = x - 1
screenX = 1
bRedraw = true
elseif screenX > w then
scrollX = x - w
screenX = w
bRedraw = true
end

if screenY < 1 then
scrollY = y - 1
screenY = 1
bRedraw = true
elseif screenY > h-1 then
scrollY = y - (h-1)
screenY = h-1
bRedraw = true
end

if bRedraw then
redrawText()
end
term.setCursorPos( screenX, screenY )
end

load()

term.clear()
term.setCursorPos(x,y)
term.setCursorBlink( true )

redrawText()
setStatus( "Press Ctrl to access menu" )

local bRunning = true
local tMenuFunctions = {
["Exit"] = function()
bRunning = false
end,
["Save"] = function()
if save() then
setStatus( "Saved to "..sPath )
else
setStatus( "Access denied" )
end
end,
   ["Print"] = function()
print()
end, 
}

local function doMenuItem( _n )
local fnMenu = tMenuFunctions[ tMenuItems[_n] ]
if fnMenu then
fnMenu()
end

bMenu = false
term.setCursorBlink( true )
redrawMenu()
end

while bRunning do
  local sEvent, param = os.pullEvent()
  if sEvent == "key" then
    if param == 200 then
-- Up
if not bMenu then
-- Move cursor up
if y > 1 then
y = y - 1
x = math.min( x, string.len( tLines[y] ) + 1 )
setCursor( x, y )
end
end

elseif param == 208 then
-- Down
if not bMenu then
-- Move cursor down
if y < #tLines then
y = y + 1
x = math.min( x, string.len( tLines[y] ) + 1 )
setCursor( x, y )
end
end

elseif param == 203 then
-- Left
if not bMenu then
-- Move cursor left
if x > 1 then
x = x - 1
setCursor( x, y )
end
else
-- Move menu left
nMenuItem = nMenuItem - 1
if nMenuItem < 1 then
nMenuItem = #tMenuItems
end
redrawMenu()
end


elseif param == 205 then
-- Right
if not bMenu then
-- Move cursor right
      if x < string.len( tLines[y] ) + 1 then
x = x + 1
setCursor( x, y )
  end
else
-- Move menu right
nMenuItem = nMenuItem + 1
  if nMenuItem > #tMenuItems then
nMenuItem = 1
  end
  redrawMenu()
end

elseif param == 14 then
-- Backspace
if not bMenu then
      if x > 1 then
-- Remove character
local sLine = tLines[y]
tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
redrawLine()

x = x - 1
setCursor( x, y )

elseif y > 1 then
-- Remove newline
local sPrevLen = string.len( tLines[y-1] )
tLines[y-1] = tLines[y-1] .. tLines[y]
table.remove( tLines, y )
redrawText()

x = sPrevLen + 1
y = y - 1
setCursor( x, y )
  end
end

elseif param == 28 then
-- Enter
if not bMenu then
-- Newline
local sLine = tLines[y]
tLines[y] = string.sub(sLine,1,x-1)
table.insert( tLines, y+1, string.sub(sLine,x) )
redrawText()

x = 1
y = y + 1
setCursor( x, y ) 

else
-- Menu selection
doMenuItem( nMenuItem )
end

elseif param == 29 then
-- Menu toggle
bMenu = not bMenu
if bMenu then
term.setCursorBlink( false )
nMenuItem = 1
else
term.setCursorBlink( true )
end
redrawMenu()

end

  elseif sEvent == "char" then
if not bMenu then
     -- Input text
local sLine = tLines[y]
if string.len(sLine)==21 then
--A capo 
tLines[y] = string.sub(sLine,1,x-1)
table.insert( tLines, y+1, string.sub(sLine,x) )
redrawText()
         x = 1
y = y + 1
setCursor( x, y )
        else
tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
redrawLine()
x = x + string.len( param )
setCursor( x, y )
   end
else
-- Select menu items
for n,sMenuItem in ipairs( tMenuItems ) do
if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
doMenuItem( n )
break
end
end
end
  end
end

term.clear()
term.setCursorBlink( false )
term.setCursorPos( 1, 1 )


Screens:

Spoiler

Shot at 2012-06-14



Shot at 2012-06-14


Shot at 2012-06-14
Maarten551 #2
Posted 14 June 2012 - 02:21 PM
This seems pretty nice, I wish turtlesterminals could have colors :(/>/>.

edit : But one question, why is there spaces after 'i'?
master39 #3
Posted 14 June 2012 - 02:23 PM
This seems pretty nice, I wish turtlesterminals could have colors :(/>/>.
u're right!
slickstevens #4
Posted 17 June 2012 - 06:19 AM
Hey, I would like to know if you would like to work together to make your program a lot more indepth… For ex… Making a base program that gives you a menu to choose different options… for example… scan something print something check ink levels etc… If you are interested send me a pm.
master39 #5
Posted 17 July 2012 - 10:43 PM
Hey, I would like to know if you would like to work together to make your program a lot more indepth… For ex… Making a base program that gives you a menu to choose different options… for example… scan something print something check ink levels etc… If you are interested send me a pm.
sorry for the delay but i had no connection recently, now I haven't time to write a new program, but you can continue my work if you want to, i only ask you to send me a link to your work
bobyiy #6
Posted 12 December 2012 - 03:18 AM
please correct code for printer

make a tutorial video please . thanks
Dlcruz129 #7
Posted 12 December 2012 - 05:09 PM
please correct code for printer

make a tutorial video please . thanks

Achievement Get!
Bump a 5 month old topic.
Sammich Lord #8
Posted 12 December 2012 - 05:19 PM
please correct code for printer

make a tutorial video please . thanks

Achievement Get!
Bump a 5 month old topic.
I play PS3 so that statement is very racist.
Dlcruz129 #9
Posted 12 December 2012 - 06:46 PM
please correct code for printer

make a tutorial video please . thanks

Achievement Get!
Bump a 5 month old topic.
I play PS3 so that statement is very racist.

Ummm… How?
bjornir90 #10
Posted 12 December 2012 - 07:22 PM
Because… wait no the PC also have achievements !
billysback #11
Posted 13 December 2012 - 11:27 AM
PS3 have "Trophies"
I don't have a PS3 (Xbox) but I still know this, it's such a stupid name…

How about:
"Challenge get"
that better?
Sammich Lord #12
Posted 13 December 2012 - 11:37 AM
PS3 have "Trophies"
I don't have a PS3 (Xbox) but I still know this, it's such a stupid name…

How about:
"Challenge get"
that better?
Much better.
Dlcruz129 #13
Posted 16 December 2012 - 05:31 AM
PS3 have "Trophies"
I don't have a PS3 (Xbox) but I still know this, it's such a stupid name…

How about:
"Challenge get"
that better?
Much better.

I still fail to see how "Achievement Get!" is racist…