From patchwork Fri Oct 2 16:41:23 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Burri X-Patchwork-Id: 7317641 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id CE8299F302 for ; Fri, 2 Oct 2015 16:50:15 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D49A320821 for ; Fri, 2 Oct 2015 16:50:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1A49F2078A for ; Fri, 2 Oct 2015 16:50:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752315AbbJBQuG (ORCPT ); Fri, 2 Oct 2015 12:50:06 -0400 Received: from www.digint.ch ([92.42.190.51]:38440 "EHLO mail.digint.ch" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751494AbbJBQuE (ORCPT ); Fri, 2 Oct 2015 12:50:04 -0400 X-Greylist: delayed 553 seconds by postgrey-1.27 at vger.kernel.org; Fri, 02 Oct 2015 12:50:04 EDT Received: from tty0.ch (77-59-134-149.dclient.hispeed.ch [77.59.134.149]) by mail.digint.ch (Postfix) with SMTP id A935C2F70BF0; Fri, 2 Oct 2015 18:40:51 +0200 (CEST) Received: by tty0.ch (sSMTP sendmail emulation); Fri, 02 Oct 2015 18:41:33 +0200 From: axel@tty0.ch To: linux-btrfs@vger.kernel.org Cc: Axel Burri Subject: [PATCH 4/4] btrfs-progs: change -t option for subvolume list to print a simple space-separated table (making it machine-readable) Date: Fri, 2 Oct 2015 18:41:23 +0200 Message-Id: <1443804083-876-5-git-send-email-axel@tty0.ch> X-Mailer: git-send-email 2.4.9 In-Reply-To: <1443804083-876-1-git-send-email-axel@tty0.ch> References: <1443804083-876-1-git-send-email-axel@tty0.ch> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Old implementation used tabs "\t", and tried to work around problems by guessing amount of tabs needed (e.g. "\t\t" after top level", with buggy output as soon as empty uuids are printed). This will never work correctly, as tab width is a user-defined setting in the terminal. Keep it simple and don't reimplement the wheel, for nice tabular output we have the "column" command from util-linux: btrfs subvolume list -t | column -t Signed-off-by: Axel Burri --- btrfs-list.c | 40 ++++++++++++---------------------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/btrfs-list.c b/btrfs-list.c index f8396e7..c09257a 100644 --- a/btrfs-list.c +++ b/btrfs-list.c @@ -45,12 +45,12 @@ struct root_lookup { }; static struct { - char *name; + char *name; /* machine-readable column identifier: [a-z_]+ */ char *column_name; int need_print; } btrfs_list_columns[] = { { - .name = "ID", + .name = "id", .column_name = "ID", .need_print = 0, }, @@ -70,7 +70,7 @@ static struct { .need_print = 0, }, { - .name = "top level", + .name = "top_level", .column_name = "Top Level", .need_print = 0, }, @@ -1465,13 +1465,10 @@ static void print_single_volume_info_table(struct root_info *subv) if (!btrfs_list_columns[i].need_print) continue; - print_subvolume_column(subv, i); - - if (i != BTRFS_LIST_PATH) - printf("\t"); + if (i != 0) + printf(" "); - if (i == BTRFS_LIST_TOP_LEVEL) - printf("\t"); + print_subvolume_column(subv, i); } printf("\n"); } @@ -1496,30 +1493,17 @@ static void print_single_volume_info_default(struct root_info *subv) static void print_all_volume_info_tab_head(void) { int i; - int len; - char barrier[20]; - - for (i = 0; i < BTRFS_LIST_ALL; i++) { - if (btrfs_list_columns[i].need_print) - printf("%s\t", btrfs_list_columns[i].name); - - if (i == BTRFS_LIST_ALL-1) - printf("\n"); - } for (i = 0; i < BTRFS_LIST_ALL; i++) { - memset(barrier, 0, sizeof(barrier)); + if (!btrfs_list_columns[i].need_print) + continue; - if (btrfs_list_columns[i].need_print) { - len = strlen(btrfs_list_columns[i].name); - while (len--) - strcat(barrier, "-"); + if (i != 0) + printf(" "); - printf("%s\t", barrier); - } - if (i == BTRFS_LIST_ALL-1) - printf("\n"); + printf(btrfs_list_columns[i].name); } + printf("\n"); } static void print_all_volume_info(struct root_lookup *sorted_tree,