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
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
2 changes: 2 additions & 0 deletions simple_logging.gpr
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 13 additions & 2 deletions src/simple_logging.adb
Original file line number Diff line number Diff line change
Expand Up @@ -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 --
Expand Down Expand Up @@ -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
Expand All @@ -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;

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

end Simple_Logging;
2 changes: 1 addition & 1 deletion src/simple_logging.ads
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package Simple_Logging with Preelaborate is
-- Strings should be encoded in the expected terminal encoding, which in
-- this day and age should be UTF-8 for both Linux and Windows. This is
-- likely to change in the future to require Unicode encoding so text
-- lenghts can be computed properly.
-- lengths can be computed properly.

type Levels is (Always,
Error,
Expand Down
32 changes: 32 additions & 0 deletions src/term.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifdef __linux__

#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;
}
}

#else // #ifdef __linux__

int simple_logging_term_width(void)
{
return -1;
}

#endif // #ifdef __linux__