view toys/pending/watch.c @ 1037:af1780148f7c draft

Implement ls --color=auto, suggested by Rich Felker.
author Rob Landley <rob@landley.net>
date Sun, 01 Sep 2013 08:00:41 -0500
parents fe7725c1819a
children 580a0300b68f
line wrap: on
line source

/* watch.c - Execute a program periodically
 *
 * Copyright 2013 Sandeep Sharma <sandeep.jack2756@gmail.com>
 * Copyright 2013 Kyungwan Han <asura321@gmail.com>
 *
USE_WATCH(NEWTOY(watch, "^<1n#<0=2t", TOYFLAG_USR|TOYFLAG_BIN))

config WATCH
  bool "watch"
  default n
  help
    Usage: watch [-n SEC] [-t] PROG ARGS

    Run PROG periodically

    -n  Loop period in seconds (default 2)
    -t  Don't print header
*/
#define FOR_watch
#include "toys.h"

GLOBALS(
  int interval; 
)

void watch_main(void)
{
  int i = 0, hlen;
  time_t t;
  unsigned width = 80, len = sizeof("1234-67-90 23:56:89");//time format
  char *header, *cmd = *toys.optargs;

  while(toys.optargs[++i]) cmd = xmsprintf("%s %s", cmd, toys.optargs[i]);
  header = xmsprintf("Every %us: %s", TT.interval, cmd);

  while(1) {
    xprintf("\033[H\033[J");
    if(!(toys.optflags & FLAG_t)) {
      xprintf("%s", header);
      hlen = strlen(header);
      terminal_size(&width, NULL);
      if (!width) width = 80; //on serial it may return 0.
      if (width > (hlen + len)) {                         
        time(&t);                                         
        strftime(toybuf, len, "%Y-%m-%d %H:%M:%S", localtime(&t));
        xprintf("%*s", width - hlen, toybuf);             
      }
      xprintf("\n\n"); // 1'\n' for space between header and result
    }
    fflush(NULL); //making sure the screen is clear
    system(cmd);
    sleep(TT.interval);
  }
  if (CFG_TOYBOX_FREE) free(header);
}