From 49bb2f4fc8cf7b97c8c8f3d0477e90945dd9b5b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Tue, 29 Oct 2024 19:44:09 +0100 Subject: [PATCH] Properly align columns from list subcommand. --- TODO.md | 2 +- src/cmd.cpp | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/TODO.md b/TODO.md index c238239..2fb2cee 100644 --- a/TODO.md +++ b/TODO.md @@ -4,7 +4,7 @@ - [ ] Create a man page. - [ ] Add more documentation to `help` subcommand. - [ ] Add import/export functionality. -- [ ] Properly align output columns from `list` subcommand. +- [X] Properly align output columns from `list` subcommand. - [X] Add feature for editing recipe name and description. - [X] Name - [X] Description diff --git a/src/cmd.cpp b/src/cmd.cpp index 86fc96f..87f604f 100644 --- a/src/cmd.cpp +++ b/src/cmd.cpp @@ -20,6 +20,8 @@ #include "util.hpp" #include +#include +#include #include #include #include @@ -71,6 +73,8 @@ int cmd_add(void) { int cmd_list(int argc, char *argv[]) { db db; std::vector ingredients, tags; + struct winsize winsize; + const int id_col_sz = 5, name_col_sz = 24; int opt; while((opt = getopt(argc, argv, "i:t:")) not_eq -1) { @@ -92,10 +96,20 @@ int cmd_list(int argc, char *argv[]) { } } + ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize); + const int desc_col_sz = winsize.ws_col - (id_col_sz + name_col_sz + 1); + db.open(); - for(const auto &recipe : db.get_recipes(ingredients, tags)) - std::cout << recipe.id << " | " << recipe.name << " | " << recipe.description << std::endl; + std::cout << std::left << std::setw(id_col_sz) << "ID" + << std::setw(name_col_sz) << "NAME" + << std::setw(desc_col_sz) << "DESCRIPTION" << std::endl; + + for(const auto &recipe : db.get_recipes(ingredients, tags)) { + std::cout << std::left << std::setw(id_col_sz) << recipe.id + << std::setw(name_col_sz) << recipe.name + << std::setw(desc_col_sz) << recipe.description << std::endl; + } db.close();