Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate status when necessary #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
truncate status on small terminals
When the TTY is too narrow to contain the output, the assumption
that a carriage return and whitespace while erase the line no
longer holds. Prior to printing, check the number of columns
available in the TTY.

See alire-project/alire#1329 for a demonstration of the issue.
atalii committed Nov 17, 2023
commit 820a527193bf227db646dddabba7a5e9b9ca6deb
2 changes: 2 additions & 0 deletions simple_logging.gpr
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@ project Simple_Logging is
for Object_Dir use "obj";
for Library_Dir use "lib";

for Languages use ("C", "Ada");

package Builder is
for Switches ("ada") use ("-j0", "-g");
end Builder;
15 changes: 13 additions & 2 deletions src/simple_logging.adb
Original file line number Diff line number Diff line change
@@ -5,13 +5,18 @@ with GNAT.IO;
with Simple_Logging.Decorators;
with Simple_Logging.Filtering;

with Interfaces.C;

pragma Warnings (Off);
-- This is compiler-internal unit. We only use the Clock, which is highly
-- unlikely to change its specification.
with System.OS_Primitives;
pragma Warnings (On);

package body Simple_Logging is
function Get_Term_Width return Interfaces.C.Int;
pragma Import
(Convention => C, Entity => Get_Term_Width, External_Name => "simple_logging_term_width");

---------
-- Log --
@@ -172,6 +177,8 @@ package body Simple_Logging is
Line : Unbounded_String;
Pred : Unbounded_String;
-- Status of the precedent scope, to eliminate duplicates

Effective_Length : Integer;
begin
for Status of Statuses loop
if Status.Level <= Simple_Logging.Level
@@ -187,7 +194,12 @@ package body Simple_Logging is
Line := Indicator & " " & Line;
end if;

return To_String (Line);
Effective_Length := Integer (Get_Term_Width);
if Effective_Length = -1 or else Effective_Length > Length (Line) then
Effective_Length := Length (Line);
end if;

return To_String (Head (Line, Effective_Length));
end Build_Status_Line;

-----------------------
@@ -255,5 +267,4 @@ package body Simple_Logging is
end if;
end;
end Step;

end Simple_Logging;
21 changes: 21 additions & 0 deletions src/term.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdbool.h>
#include <sys/ioctl.h>
#include <termios.h>

bool ioctl_failure = false;

int simple_logging_term_width(void)
{
struct winsize x;
int r;

if (ioctl_failure) return -1;
r = ioctl(2, TIOCGWINSZ, &x);

if (r != 0) {
ioctl_failure = true;
return -1;
} else {
return x.ws_col;
}
}