Skip to content

Commit

Permalink
Merge pull request #9 from marcomorain/time-format
Browse files Browse the repository at this point in the history
Add Time Format as suggested in #2
  • Loading branch information
marcomorain committed Mar 8, 2014
2 parents 3d5c432 + d1e1549 commit 8806572
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $ echo "Hello world" | tcat
```

## Usage
The following is the ouput from the `ping` command:
The following is the ouput from the `ping` command:
```
$ ping -c 3 github.com
PING github.com (192.30.252.131): 56 data bytes
Expand All @@ -38,7 +38,24 @@ $ ping -c 3 github.com | tcat
2014-02-09T20:58:44+0000 round-trip min/avg/max/stddev = 106.783/110.892/116.658/4.198 ms
```

Time Cat prepends each line of input with a timestamp followed by a single tab character. This will be the first whitespace character on the live. This means that it is very easy to remove the timestamps again. You can use `cut -f 2`, for example.
The format that the time is printed in can be overiden by the user in one of two ways. You can specify the format using the `-f` or `--format` command-line options. You can also set the format by setting the `TCAT_FORMAT` environmental variable. The format should be specified using the `strftime(3)` syntax. If the format is overridden using the command-line options, this will take precence over the environment variable.

Here are three examples showing the default usage, format passed as a command-line option, and through the environment variable.

```
$ echo default | tcat
2014-03-08T13:15:43+0000 default
$ echo command-line | tcat --format '%H:%M:%S'
13:17:03 command-line
$ echo environment | TCAT_FORMAT='%Y/%m/%d' tcat
2014/03/08 environment
```

## Advanced Usage
Time Cat prepends each line of input with a timestamp followed by a single tab character. This will be the first whitespace character on the line. This means that it is very easy to remove the timestamps again. You can use `cut -f 2`, for example.

```
$ echo foo
foo
Expand All @@ -49,7 +66,7 @@ foo
```

## Building
Time Cat should build easily on any Unix. It has been tested on Ubuntu and OSX using gcc and clang.
Time Cat should build easily on any Unix. It has been tested on Ubuntu and OSX using gcc and clang.

To build Time Cat just navigate to where you have cloned the respository and run `make`.

Expand Down
32 changes: 23 additions & 9 deletions tcat.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ enum {
version_patch = 2
};

static const char program_name[] = "tcat";
static const char env_flag[] = "TCAT_FORMAT";
static const char *format = "%FT%T%z\t";
static const char program_name[] = "tcat";
static const char env_flag[] = "TCAT_FORMAT";
static const char default_format[] = "%FT%T%z";
static const char* format = default_format;

static void io_error(FILE* file) {
if (feof(file)) {
Expand All @@ -36,6 +37,9 @@ static void print_time() {
if(len != fwrite(buffer, 1, len, stdout)){
io_error(stdout);
}
if (fputc('\t', stdout) == EOF) {
io_error(stdout);
}
}

static void version(FILE* output) {
Expand All @@ -52,9 +56,10 @@ static inline void usage(FILE* output) {
"Available Options:\n"
"\t-v, --version\tprint %s version\n"
"\t-h, --help\tprint this help\n"
"\t-f, --format\tset time format string in strftime syntax (default: \"%s\")\n"
"\n"
"Help can be found at https://github.com/marcomorain/tcat\n",
program_name);
program_name, default_format);
}

static int match(const char* x, const char* a, const char* b) {
Expand All @@ -65,6 +70,11 @@ int main(int argc, char** argv) {

int bad_usage = 0;

char *format_env = getenv(env_flag);
if (format_env) {
format = format_env;
}

for (int i = 1; i < argc; i++) {
if (match(argv[i], "-v", "--version")) {
version(stdout);
Expand All @@ -76,6 +86,15 @@ int main(int argc, char** argv) {
return EXIT_SUCCESS;
}

if (match(argv[i], "-f", "--format")) {
if (++i >= argc) {
fprintf(stderr, "Error: no format string given provided for %s option\n", argv[i-1]);
return EXIT_FAILURE;
}
format = argv[i];
continue;
}

// Show all bad options
bad_usage = 1;
fprintf(stderr, "Invalid option:\t%s\n", argv[i]);
Expand All @@ -91,11 +110,6 @@ int main(int argc, char** argv) {
fprintf(stderr, "Warning: input is from TTY\n");
}

char *format_env = getenv(env_flag);
if (format_env) {
format = format_env;
}

int last = '\n';
for(int c = fgetc(stdin); c != EOF; c = fgetc(stdin)) {
if (last == '\n') {
Expand Down

0 comments on commit 8806572

Please sign in to comment.