1

Calling something like ag (a grep alternative) results in an output that's human readable. Results are printed with headers. Useful for humans, horrible for programs.

$ ag 'filter\('
tests/string_lookup/tests.py
75:        self.assertSequenceEqual(Article.objects.filter(submitted_from__contains='192.0.2'), [a])
77:        self.assertEqual(Article.objects.filter(submitted_from__contains='32').count(), 0)

tests/test_runner/test_debug_sql.py
16:            Person.objects.filter(first_name='pass').count()
20:            Person.objects.filter(first_name='fail').count()

Same program, piped into something else, outputs:

$ ag 'filter\(' | cat
tests/string_lookup/tests.py:75:        self.assertSequenceEqual(Article.objects.filter(submitted_from__contains='192.0.2'), [a])
tests/string_lookup/tests.py:77:        self.assertEqual(Article.objects.filter(submitted_from__contains='32').count(), 0)
tests/test_runner/test_debug_sql.py:16:            Person.objects.filter(first_name='pass').count()
tests/test_runner/test_debug_sql.py:20:            Person.objects.filter(first_name='fail').count()

This output is much better suited for use by other programs, so I assume that ag is aware that it's being piped.

How is ag aware of where it's output is being sent?

1 Answer 1

4

The program calls isatty() on the stdout file descriptor (i.e. 1). If the function returns true, the output is connected to a tty device (a terminal).

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .