Hi all.

Today I get really upset and angry due limitation of top(1): it shows
only hrrm, top processes (thank you, Chromium). Now here is a diff that
allows you to scroll process list by line or by half a screen.

I've used the '0' and '9' keys to scroll down and up, respectively.
Unfortunately, 'k' is already taken, so vi-like binding to 'j'/'k' keys
is not possible. And emacs-style 'v'-for-all looks like too complex.
Anyone, who wants to use up/down and page up/page down keys, be my guest
for converting command_chars in top.c to using multi-byte sequences,
or whatever is needed for proper handling of those keys.

Ideas, comments and (may I hope?) okays are welcome. :)

--
WBR,
  Vadim Zhukov


Index: machine.c
===================================================================
RCS file: /cvs/src/usr.bin/top/machine.c,v
retrieving revision 1.101
diff -u -p -r1.101 machine.c
--- machine.c   16 Dec 2019 19:21:17 -0000      1.101
+++ machine.c   5 Jan 2020 14:10:44 -0000
@@ -546,6 +546,14 @@ format_comm(struct kinfo_proc *kp)
        return (buf);
 }
 
+void
+skip_next_process(struct handle *hndl)
+{
+       /* find and remember the next proc structure */
+       hndl->next_proc++;
+       hndl->remaining--;
+}
+
 char *
 format_next_process(struct handle *hndl, const char *(*get_userid)(uid_t, int),
     pid_t *pid)
Index: machine.h
===================================================================
RCS file: /cvs/src/usr.bin/top/machine.h,v
retrieving revision 1.27
diff -u -p -r1.27 machine.h
--- machine.h   8 Oct 2019 20:51:03 -0000       1.27
+++ machine.h   5 Jan 2020 14:10:44 -0000
@@ -90,6 +90,7 @@ extern void     get_system_info(struct s
 extern struct handle
 *get_process_info(struct system_info *, struct process_select *,
                 int (*) (const void *, const void *));
+extern void     skip_next_process(struct handle *);
 extern char    *format_next_process(struct handle *,
                 const char *(*)(uid_t, int), pid_t *);
 extern uid_t    proc_owner(pid_t);
Index: top.1
===================================================================
RCS file: /cvs/src/usr.bin/top/top.1,v
retrieving revision 1.71
diff -u -p -r1.71 top.1
--- top.1       28 Nov 2018 22:00:30 -0000      1.71
+++ top.1       5 Jan 2020 14:10:44 -0000
@@ -291,6 +291,10 @@ or any process highlighting put in place
 interactive command.
 .It 1
 Toggle the display of per CPU or combined CPU statistics.
+.It 9 | 0 
+Scroll up/down the process list by one line.
+.It \&( | \&) 
+Scroll up/down the process list by screen half.
 .It C
 Toggle the display of process command line arguments.
 .It d Ar count
Index: top.c
===================================================================
RCS file: /cvs/src/usr.bin/top/top.c,v
retrieving revision 1.101
diff -u -p -r1.101 top.c
--- top.c       8 Oct 2019 20:51:03 -0000       1.101
+++ top.c       5 Jan 2020 14:10:44 -0000
@@ -68,6 +68,7 @@ static void   reset_display(void);
 int            rundisplay(void);
 
 static int     max_topn;       /* maximum displayable processes */
+static int     skip;           /* how many processes to skip (scroll) */
 
 extern int ncpu;
 extern int ncpuonline;
@@ -126,6 +127,10 @@ struct statics  statics;
 #define CMD_add                21
 #define CMD_hl         22
 #define CMD_cpus       23
+#define CMD_down       24
+#define CMD_up         25
+#define CMD_pagedown   26
+#define CMD_pageup     27
 
 static void
 usage(void)
@@ -557,6 +562,15 @@ restart:
                                active_procs = topn;
                        if (active_procs > max_topn)
                                active_procs = max_topn;
+                       /* determine how many process to skip, if asked to */
+                       /*
+                        * this number is tweaked by user, but gets shrinked
+                        * when number of active processes lowers too much
+                        */
+                       if (skip + active_procs > system_info.p_active)
+                               skip = system_info.p_active - active_procs;
+                       for (i = skip; i > 0; i--)
+                               skip_next_process(processes);
                        /* now show the top "n" processes. */
                        for (i = 0; i < active_procs; i++) {
                                pid_t pid;
@@ -618,7 +632,7 @@ rundisplay(void)
        char ch, *iptr;
        int change, i;
        struct pollfd pfd[1];
-       static char command_chars[] = "\f qh?en#sdkriIuSopCHg+P1";
+       static char command_chars[] = "\f qh?en#sdkriIuSopCHg+P109)(";
 
        /*
         * assume valid command unless told
@@ -966,6 +980,21 @@ rundisplay(void)
                        combine_cpus = !combine_cpus;
                        max_topn = display_resize();
                        reset_display();
+                       break;
+               case CMD_down:
+                       skip++;
+                       break;
+               case CMD_up:
+                       if (skip > 0)
+                               skip--;
+                       break;
+               case CMD_pagedown:
+                       skip += max_topn / 2;
+                       break;
+               case CMD_pageup:
+                       skip -= max_topn / 2;
+                       if (skip < 0)
+                               skip = 0;
                        break;
                default:
                        new_message(MT_standout, " BAD CASE IN SWITCH!");

Reply via email to