Skip to content

Commit a3d9b6e

Browse files
committed
initctl: fix remaining lingering artifacts in 'top' output
Signed-off-by: Joachim Wiberg <[email protected]>
1 parent 390a0f4 commit a3d9b6e

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

src/cgutil.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,13 +728,24 @@ int show_cgps(char *arg)
728728

729729
static void cgtop(uev_t *w, void *arg, int events)
730730
{
731+
static int first = 1;
731732
int lines;
732733

733734
(void)w;
734735
(void)events;
735736

736-
/* Move cursor to home instead of clearing screen to avoid flicker */
737-
fputs("\e[H", stdout);
737+
/* Re-probe screen size in case terminal was resized */
738+
ttinit(1);
739+
740+
/* Clear screen on first run to remove any artifacts */
741+
if (first) {
742+
fputs("\e[?7l\e[2J\e[H", stdout); /* Disable line wrap, clear, home */
743+
first = 0;
744+
} else {
745+
/* Move cursor to home instead of clearing screen to avoid flicker */
746+
fputs("\e[H", stdout);
747+
}
748+
738749
lines = 0;
739750
if (heading) {
740751
print_header(" VmSIZE RSS VmLIB %%MEM %%CPU GROUP");
@@ -749,6 +760,7 @@ static void cgtop(uev_t *w, void *arg, int events)
749760

750761
static void cleanup(void)
751762
{
763+
fputs("\e[?7h", stdout); /* Re-enable line wrap */
752764
ttcooked();
753765
showcursor();
754766
puts("");
@@ -799,6 +811,9 @@ int show_cgtop(char *arg)
799811
if (!hcreate(ttrows + 25))
800812
ERR(70, "failed creating hash table");
801813

814+
/* Ensure we have correct terminal size before starting */
815+
ttinit(1);
816+
802817
sysinfo(&si);
803818
total_ram = si.totalram * si.mem_unit;
804819

src/initctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1724,7 +1724,7 @@ int main(int argc, char *argv[])
17241724
}
17251725

17261726
if (interactive)
1727-
ttinit();
1727+
ttinit(0);
17281728

17291729
return cmd_parse(argc - optind, &argv[optind], command);
17301730
}

src/util.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ int fistmpfs(char *dir)
687687
* Called by initctl, and by finit at boot and shutdown, to
688688
* (re)initialize the screen size for print() et al.
689689
*/
690-
int ttinit(void)
690+
int ttinit(int probe_size)
691691
{
692692
struct pollfd fd = { STDIN_FILENO, POLLIN, 0 };
693693
struct winsize ws = { 0 };
@@ -707,6 +707,9 @@ int ttinit(void)
707707
tcsetattr(STDERR_FILENO, TCSANOW, &tc);
708708
}
709709

710+
if (probe_size)
711+
goto fallback;
712+
710713
/* 1. Try TIOCWINSZ to query window size from kernel */
711714
if (!ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws)) {
712715
ttrows = ws.ws_row;

src/util.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ int fismnt (char *dir);
9595
int fistmpfs (char *dir);
9696

9797
#ifdef HAVE_TERMIOS_H
98-
int ttinit (void);
98+
int ttinit (int probe_size);
9999
int ttraw (void);
100100
int ttcooked (void);
101101
#else
102-
#define ttinit() ttcols
102+
#define ttinit(v) ttcols
103103
#define ttraw() 0
104104
#define ttcooked() 0
105105
#endif

0 commit comments

Comments
 (0)