column.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "builtin.h"
  2. #include "cache.h"
  3. #include "config.h"
  4. #include "strbuf.h"
  5. #include "parse-options.h"
  6. #include "string-list.h"
  7. #include "column.h"
  8. static const char * const builtin_column_usage[] = {
  9. N_("git column [<options>]"),
  10. NULL
  11. };
  12. static unsigned int colopts;
  13. static int column_config(const char *var, const char *value, void *cb)
  14. {
  15. return git_column_config(var, value, cb, &colopts);
  16. }
  17. int cmd_column(int argc, const char **argv, const char *prefix)
  18. {
  19. struct string_list list = STRING_LIST_INIT_DUP;
  20. struct strbuf sb = STRBUF_INIT;
  21. struct column_options copts;
  22. const char *command = NULL, *real_command = NULL;
  23. struct option options[] = {
  24. OPT_STRING(0, "command", &real_command, N_("name"), N_("lookup config vars")),
  25. OPT_COLUMN(0, "mode", &colopts, N_("layout to use")),
  26. OPT_INTEGER(0, "raw-mode", &colopts, N_("layout to use")),
  27. OPT_INTEGER(0, "width", &copts.width, N_("Maximum width")),
  28. OPT_STRING(0, "indent", &copts.indent, N_("string"), N_("Padding space on left border")),
  29. OPT_INTEGER(0, "nl", &copts.nl, N_("Padding space on right border")),
  30. OPT_INTEGER(0, "padding", &copts.padding, N_("Padding space between columns")),
  31. OPT_END()
  32. };
  33. /* This one is special and must be the first one */
  34. if (argc > 1 && starts_with(argv[1], "--command=")) {
  35. command = argv[1] + 10;
  36. git_config(column_config, (void *)command);
  37. } else
  38. git_config(column_config, NULL);
  39. memset(&copts, 0, sizeof(copts));
  40. copts.padding = 1;
  41. argc = parse_options(argc, argv, prefix, options, builtin_column_usage, 0);
  42. if (argc)
  43. usage_with_options(builtin_column_usage, options);
  44. if (real_command || command) {
  45. if (!real_command || !command || strcmp(real_command, command))
  46. die(_("--command must be the first argument"));
  47. }
  48. finalize_colopts(&colopts, -1);
  49. while (!strbuf_getline(&sb, stdin))
  50. string_list_append(&list, sb.buf);
  51. print_columns(&list, colopts, &copts);
  52. return 0;
  53. }