Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gap/framework/ProgressPrinter.gi
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ InstallValue(ProgressPrinter, rec(
Pattern := fail,
InitialConfiguration := fail,
CurProcess := fail,
IsActive := true,
));

InstallGlobalFunction("SetLayout", function(layout)
Expand Down
66 changes: 66 additions & 0 deletions gap/framework/Terminal.gi
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

InstallGlobalFunction("PB_Print", function(msg)
local pos;
if not ProgressPrinter.IsActive then
return;
fi;
pos := ProgressPrinter.Cursor.x + Length(msg);
if pos > ProgressPrinter.Dimensions.w + 1 then
# Error("Trying to print more than the screen width allows to");
Expand All @@ -35,16 +38,25 @@ InstallGlobalFunction("PB_Print", function(msg)
end);

InstallGlobalFunction("PB_SetStyleAndColor", function(r)
if not ProgressPrinter.IsActive then
return;
fi;
PB_SetStyle(r.style);
PB_SetForegroundColor(r.foregroundColor);
PB_SetBackgroundColor(r.backgroundColor);
end);

InstallGlobalFunction("PB_ResetStyleAndColor", function()
if not ProgressPrinter.IsActive then
return;
fi;
WriteAll(STDOut, "\033[0m");
end);

InstallGlobalFunction("PB_SetStyle", function(mode)
if not ProgressPrinter.IsActive then
return;
fi;
if mode = "default" then
WriteAll(STDOut, "\033[22m\033[23m\033[24m\033[25m");
elif mode = "bold" then
Expand All @@ -61,6 +73,9 @@ InstallGlobalFunction("PB_SetStyle", function(mode)
end);

InstallGlobalFunction("PB_SetForegroundColor", function(color)
if not ProgressPrinter.IsActive then
return;
fi;
if color = "default" then
WriteAll(STDOut, "\033[39m");
elif color = "black" then
Expand All @@ -83,6 +98,9 @@ InstallGlobalFunction("PB_SetForegroundColor", function(color)
end);

InstallGlobalFunction("PB_SetBackgroundColor", function(color)
if not ProgressPrinter.IsActive then
return;
fi;
if color = "default" then
WriteAll(STDOut, "\033[49m");
elif color = "black" then
Expand All @@ -105,15 +123,24 @@ InstallGlobalFunction("PB_SetBackgroundColor", function(color)
end);

InstallGlobalFunction("PB_HideCursor", function()
if not ProgressPrinter.IsActive then
return;
fi;
WriteAll(STDOut, "\033[?25l"); # hide cursor
end);

InstallGlobalFunction("PB_ShowCursor", function()
if not ProgressPrinter.IsActive then
return;
fi;
WriteAll(STDOut, "\033[?25h"); # show cursor
end);

InstallGlobalFunction("PB_MoveCursorDown", function(move)
local n, m, x;
if not ProgressPrinter.IsActive then
return;
fi;
move := AbsInt(move);
n := ProgressPrinter.Cursor.y + move;
if ProgressPrinter.Dimensions.h < n then
Expand All @@ -133,13 +160,19 @@ InstallGlobalFunction("PB_MoveCursorDown", function(move)
end);

InstallGlobalFunction("PB_MoveCursorUp", function(move)
if not ProgressPrinter.IsActive then
return;
fi;
move := AbsInt(move);
WriteAll(STDOut, Concatenation("\033[", String(move), "A")); # move cursor up X lines
ProgressPrinter.Cursor.y := ProgressPrinter.Cursor.y - move;
end);

InstallGlobalFunction("PB_MoveCursorToLine", function(n)
local move;
if not ProgressPrinter.IsActive then
return;
fi;
move := n - ProgressPrinter.Cursor.y;
if move > 0 then
PB_MoveCursorDown(move);
Expand All @@ -149,19 +182,28 @@ InstallGlobalFunction("PB_MoveCursorToLine", function(n)
end);

InstallGlobalFunction("PB_MoveCursorRight", function(move)
if not ProgressPrinter.IsActive then
return;
fi;
move := AbsInt(move);
WriteAll(STDOut, Concatenation("\033[", String(move), "C")); # move cursor right X characters
ProgressPrinter.Cursor.x := ProgressPrinter.Cursor.x + move;
end);

InstallGlobalFunction("PB_MoveCursorLeft", function(move)
if not ProgressPrinter.IsActive then
return;
fi;
move := AbsInt(move);
WriteAll(STDOut, Concatenation("\033[", String(move), "D")); # move cursor left X characters
ProgressPrinter.Cursor.x := ProgressPrinter.Cursor.x - move;
end);

InstallGlobalFunction("PB_MoveCursorToChar", function(n)
local move;
if not ProgressPrinter.IsActive then
return;
fi;
move := n - ProgressPrinter.Cursor.x;
if move > 0 then
PB_MoveCursorRight(move);
Expand All @@ -171,32 +213,50 @@ InstallGlobalFunction("PB_MoveCursorToChar", function(n)
end);

InstallGlobalFunction("PB_MoveCursorToCoordinate", function(x, y)
if not ProgressPrinter.IsActive then
return;
fi;
PB_MoveCursorToChar(x);
PB_MoveCursorToLine(y);
end);

InstallGlobalFunction("PB_MoveCursorToProcessEnd", function()
if not ProgressPrinter.IsActive then
return;
fi;
PB_MoveCursorToCoordinate(1, 1 + PB_Reduce(
ProgressPrinter.RootProcess,
{value, proc} -> value + proc.blocks.(ProgressPrinter.Pattern.id).h, 0
));
end);

InstallGlobalFunction("PB_MoveCursorToStartOfLine", function()
if not ProgressPrinter.IsActive then
return;
fi;
WriteAll(STDOut, "\r"); # move cursor to the start of the line
ProgressPrinter.Cursor.x := 1;
end);

InstallGlobalFunction("PB_ClearLine", function()
if not ProgressPrinter.IsActive then
return;
fi;
WriteAll(STDOut, "\033[2K"); # erase the entire line
end);

InstallGlobalFunction("PB_RefreshLine", function()
if not ProgressPrinter.IsActive then
return;
fi;
PB_MoveCursorToStartOfLine();
PB_ClearLine();
end);

InstallGlobalFunction("PB_ClearScreen", function()
if not ProgressPrinter.IsActive then
return;
fi;
PB_MoveCursorToLine(ProgressPrinter.Dimensions.h);
PB_RefreshLine();
while ProgressPrinter.Cursor.y > 1 do
Expand All @@ -207,6 +267,9 @@ end);

InstallGlobalFunction("PB_ClearProcess", function(process)
local block, j;
if not ProgressPrinter.IsActive then
return;
fi;
if IsBound(process.blocks) then
block := process.blocks.(ProgressPrinter.Pattern.id);
PB_MoveCursorToLine(block.y);
Expand All @@ -220,6 +283,9 @@ end);

InstallGlobalFunction("PB_ClearBlock", function(block)
local empty, j;
if not ProgressPrinter.IsActive then
return;
fi;
empty := Concatenation(ListWithIdenticalEntries(block.w, " "));
for j in [1 .. block.h] do
PB_MoveCursorToCoordinate(block.x, block.y + j - 1);
Expand Down
12 changes: 10 additions & 2 deletions gap/printer-module/ProgressBar.gi
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ PB_ProgressBarPrinter.generate := function(process, id, options)
else
# progress bar length_total
completedSteps := Maximum(0, process.completedSteps);
r := completedSteps / process.totalSteps;
if process.totalSteps = 0 then
r := 1;
else
r := completedSteps / process.totalSteps;
fi;
length_full := Int(length_total * r);
length_empty := length_total - length_full;
# print progress bar
Expand All @@ -102,7 +106,11 @@ PB_ProgressBarPrinter.update := function(process, id, options)
PB_ProgressBarPrinter.printIndefinite(process, id, options);
else
# progress bar length_total
r := completedSteps / process.totalSteps;
if process.totalSteps = 0 then
r := 1;
else
r := completedSteps / process.totalSteps;
fi;
length_full := Int(block.length_total * r);
# print progress bar
l := length_full - block.length_full;
Expand Down