Created
October 2, 2015 15:47
-
-
Save josifoski/57b2fb5d4e8efaaea7b1 to your computer and use it in GitHub Desktop.
Text file reader by blocks of lines for unix like systems in freepascal/lazarus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program fr; | |
// Simple file reader-viewer by blocks of lines | |
// programmer: Aleksandar Josifoski about.me/josifsk | |
// reading text file by blocks of requested number of lines | |
// Usage is fr numoflines YourFile for example: fr 5 Love.txt | |
// j next, k previous, g first, G last line, q or ESC quit, s for saving current block of text | |
{$mode objfpc}{$H+} | |
uses Unix, keyboard, sysutils, Crt; | |
var | |
f : Text; | |
saved : Text; | |
L, i, j : Longint; | |
ar: array[1..100000] of String; | |
si, sj, s, line : String; | |
K : TKeyEvent; | |
stcomm : String; | |
numoflines : String; | |
begin | |
// TextColor(Yellow); | |
InitKeyBoard; | |
numoflines:=ParamStr(1); | |
writeln(numoflines); | |
writeln(ParamStr(2)); | |
if (ParamStr(2)='') or (FileExists(ParamStr(2))=false) then begin | |
writeln('Simple file reader-viewer by blocks of lines'); | |
writeln('Usage is fr numoflines YourFile for example: fr 5 YourFile'); | |
writeln('Shortcuts: j next, k previous, g first, G last line, q or ESC quit'); | |
writeln('s for saving current block of text, which is saved in saved.txt'); | |
DoneKeyBoard; | |
Halt; | |
end; | |
stcomm:='tr -s ''\n'' <'+ParamStr(2)+' >frfile'; | |
L:= FpSystem(stcomm); | |
L:= FpSystem('clear'); | |
AssignFile(f, 'frfile'); | |
reset(f); | |
j:=0; | |
while not(eof(f)) do begin | |
j:=j+1; | |
s:=''; | |
for k:=1 to StrToInt(numoflines) do begin | |
readln(f, line); | |
s:= s + line + AnsiChar(#10); | |
end; | |
ar[j]:=s; | |
end; // while | |
CloseFile(f); | |
AssignFile(saved, 'saved.txt'); | |
if FileExists('saved.txt') then Append(saved) | |
else rewrite(saved); | |
if j=0 then begin | |
writeln('empty file, exit'); | |
DoneKeyBoard; | |
Halt; | |
end; | |
i:=1; | |
str(j, sj); | |
str(i, si); | |
writeln(si+'/'+sj); | |
writeln(); | |
writeln(); | |
write(ar[i]); | |
repeat | |
K:=GetKeyEvent; | |
K:=TranslateKeyEvent(K); | |
case GetKeyEventChar(K) of | |
'j' : if i < j then begin | |
L:= FpSystem('clear'); | |
i:=i+1; | |
str(i, si); | |
writeln(si+'/'+sj); | |
writeln(); | |
writeln(); | |
writeln(ar[i]); | |
end; | |
'k' : if i > 1 then begin | |
L:= FpSystem('clear'); | |
i:=i-1; | |
str(i, si); | |
writeln(si+'/'+sj); | |
writeln(); | |
writeln(); | |
writeln(ar[i]); | |
end; | |
'g' : begin | |
L:= FpSystem('clear'); | |
i:=1; | |
str(i, si); | |
writeln(si+'/'+sj); | |
writeln(); | |
writeln(); | |
writeln(ar[i]); | |
end; | |
'G' : begin | |
L:= FpSystem('clear'); | |
i:=j; | |
str(i, si); | |
writeln(si+'/'+sj); | |
writeln(); | |
writeln(); | |
writeln(ar[i]); | |
end; | |
's' : begin | |
str(i, si); | |
writeln(saved, ar[i]); | |
end; | |
'q' : L:= FpSystem('clear'); | |
#27 : L:= FpSystem('clear'); | |
end; | |
until (GetKeyEventChar(K)='q') or (GetKeyEventChar(K)=#27); | |
DoneKeyBoard; | |
CloseFile(saved); | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment