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

Tail for ComputerCraft

Started by kornichen, 04 January 2015 - 08:35 AM
kornichen #1
Posted 04 January 2015 - 09:35 AM
Hello,

when using linux I often fall back on using tail which is a program that comes with linux. It is not completely done yet, so you can't use every parameter of tail, you can use on linux or unix but the most important parameters are already implemented.

Wikipedia describes it like this:
tail is a program on Unix and Unix-like systems used to display the tail end of a text file or piped data.

Download:
Pastebin: 976V0Fhs
pastebin get 976V0Fhs tail

Usage:
tail [OPTION] FILE
Already implemented parameters:
-n N - Output of the last N lines instead of the default 10
-f - Output of the attached data if the file grows
Examples of usage:
tail
tail testfile
tail -n 25 testfile
tail -f testfile
tail -nf 5 testfile
tail -fn 5 testfile
The first line prints out a list of all parameters.
The second line prints out the last 10 lines of testfile
The third line prints out the last 25 lines of testfile
The fourth line prints out the last 10 lines of testfile and every lines that gets attached to testfile. Cancel with CTRL+T.
The fifth and the sixth line do exactly the same. The only difference is that the parameters are swapped around. Both lines print out the last 5 lines of testfile and wait for new lines to be attached to testfile to print them out.
Edited on 04 January 2015 - 08:36 AM
SquidDev #2
Posted 04 January 2015 - 10:38 AM
This is pretty useful. Just a couple of things:
  • Locals! Everything really should be a local variable
  • Follow currently uses sleep calls in an infinite loop, maybe you could link in to fs.write (and the close/flush methods) to implement a file system watcher with events instead.
kornichen #3
Posted 04 January 2015 - 12:20 PM
This is pretty useful. Just a couple of things:
  • Locals! Everything really should be a local variable
  • Follow currently uses sleep calls in an infinite loop, maybe you could link in to fs.write (and the close/flush methods) to implement a file system watcher with events instead.

I know it is better to use local variables but I always tend not to use them. I will change it, don't worry ;)/>
And I will take a deeper look at the fs API to solve this little problem.
doublequestionmark #4
Posted 05 January 2015 - 03:24 AM
This Is Great, I always wanted tail in computercraft.