ctrl k
List feature (#7)
Merged
Lukas Iklé opened 2 years ago
No description
Commits were merged into target branch
  • src/list.c
    ■ ■ ■ ■ ■ ■
     1 +#include "list.h"
     2 +#include "constants.h"
     3 +#include "interval.h"
     4 +#include "message.pb-c.h"
     5 +#include "net.h"
     6 +#include <cargs.h>
     7 +#include <confuse.h>
     8 +#include <stdio.h>
     9 +#include <stdlib.h>
     10 +#include <string.h>
     11 +#include <time.h>
     12 + 
     13 +static cag_option options[] = {
     14 + {.identifier = 'r',
     15 + .access_letters = "r",
     16 + .access_name = "repository",
     17 + .description = "Specify the repository name to operate on.",
     18 + .value_name = "NAME"},
     19 + {.identifier = 'h',
     20 + .access_letters = "h",
     21 + .access_name = "help",
     22 + .description = "Shows the help for the add command.",
     23 + .value_name = NULL},
     24 +};
     25 + 
     26 +static void print_help()
     27 +{
     28 + printf("Usage: ziit add [OPTION...] <interval> <message> [<tags>]\n");
     29 + printf("Adds a new entry to the time log.\n\n");
     30 + cag_option_print(options, CAG_ARRAY_SIZE(options), stdout);
     31 +}
     32 + 
     33 +static int pad_right(char *buffer, int buffer_len, int padding)
     34 +{
     35 + size_t content_len;
     36 + 
     37 + if (buffer_len < padding + 1) {
     38 + return -1;
     39 + }
     40 + 
     41 + buffer[padding] = '\0';
     42 + content_len = strlen(buffer);
     43 + 
     44 + for (int i = (int)content_len; i < padding; ++i) {
     45 + buffer[i] = ' ';
     46 + }
     47 + 
     48 + if (content_len > padding - 2) {
     49 + for (int i = padding - 4; i < padding - 1 && i > 3; ++i) {
     50 + buffer[i] = '.';
     51 + }
     52 + buffer[padding - 1] = ' ';
     53 + }
     54 + 
     55 + return 0;
     56 +}
     57 + 
     58 +int run_list(int argc, char *argv[], cfg_t *cfg)
     59 +{
     60 + ziit_connection con;
     61 + ZiitMessage list_message, *response;
     62 + ZiitListRequest list_request;
     63 + ZiitEntry *entry;
     64 + 
     65 + cag_option_context ctx;
     66 + const char *repository, *interval;
     67 + int option_index, option_count;
     68 + 
     69 + ziit_message__init(&list_message);
     70 + ziit_list_request__init(&list_request);
     71 + 
     72 + cag_option_prepare(&ctx, options, CAG_ARRAY_SIZE(options), argc, argv);
     73 + 
     74 + repository = "default";
     75 + 
     76 + while (cag_option_fetch(&ctx)) {
     77 + char identifier = cag_option_get(&ctx);
     78 + switch (identifier) {
     79 + case 'r':
     80 + repository = cag_option_get_value(&ctx);
     81 + break;
     82 + default:
     83 + fprintf(stderr, "Unknown option supplied to the command. Aborting.\n");
     84 + case 'h':
     85 + print_help();
     86 + return EXIT_FAILURE;
     87 + }
     88 + }
     89 + 
     90 + option_index = cag_option_get_index(&ctx) + 1;
     91 + option_count = argc - option_index;
     92 + if (option_count < 1) {
     93 + fprintf(stderr, "Insufficient arguments supplied.\n");
     94 + print_help();
     95 + return EXIT_FAILURE;
     96 + }
     97 + 
     98 + interval = argv[option_index];
     99 + 
     100 + if (ziit_parse_interval(interval, &list_request.start, &list_request.offset) <
     101 + 0) {
     102 + fprintf(stderr, "Could not parse supplied interval.\n");
     103 + print_help();
     104 + return EXIT_FAILURE;
     105 + }
     106 + 
     107 + if (ziit_connection_open(&con, cfg, repository) < 0) {
     108 + fprintf(stderr, "Could not open connection.\n");
     109 + goto err_open;
     110 + }
     111 + 
     112 + list_message.content_case = ZIIT_MESSAGE__CONTENT_LIST_REQUEST;
     113 + list_message.list_request = &list_request;
     114 + 
     115 + if (ziit_connection_send(&con, &list_message) < 0) {
     116 + fprintf(stderr, "Could not send list message to server.\n");
     117 + goto err_send;
     118 + }
     119 + 
     120 + response = ziit_connection_receive(&con);
     121 + if (response == NULL) {
     122 + fprintf(stderr, "Could not receive resposne from server.\n");
     123 + goto err_response;
     124 + }
     125 + 
     126 + if (response->content_case != ZIIT_MESSAGE__CONTENT_LIST_RESPONSE) {
     127 + fprintf(stderr, "Unexpected answer from server.\n");
     128 + goto err_response;
     129 + }
     130 + 
     131 + if (response->list_response->code != 0) {
     132 + fprintf(stderr, "Listing entries FAILED:\n");
     133 + fprintf(stderr, "%s\n", response->add_response->message);
     134 + goto err_result;
     135 + } else {
     136 + char buffer[255];
     137 + time_t t;
     138 + struct tm *tf;
     139 + int padding_id, padding_at, padding_tags, bs, padding_duration;
     140 + 
     141 + padding_id = 13;
     142 + padding_at = 27;
     143 + padding_duration = 14;
     144 + padding_tags = 17;
     145 + bs = 255;
     146 + 
     147 + strcpy(buffer, "id");
     148 + pad_right(buffer, bs, padding_id);
     149 + printf("%s", buffer);
     150 + 
     151 + strcpy(buffer, "at");
     152 + pad_right(buffer, bs, padding_at);
     153 + printf("%s", buffer);
     154 + 
     155 + strcpy(buffer, "duration");
     156 + pad_right(buffer, bs, padding_duration);
     157 + printf("%s", buffer);
     158 + 
     159 + strcpy(buffer, "tags");
     160 + pad_right(buffer, bs, padding_tags);
     161 + printf("%s", buffer);
     162 + 
     163 + printf("message\n");
     164 + 
     165 + for (int i = 0; i < response->list_response->n_entries; ++i) {
     166 + 
     167 + entry = response->list_response->entries[i];
     168 + t = (time_t)entry->at_time;
     169 + tf = localtime(&t);
     170 + 
     171 + snprintf(buffer, bs, "%li ", t);
     172 + pad_right(buffer, bs, padding_id);
     173 + printf("%s", buffer);
     174 + 
     175 + strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S %Z", tf);
     176 + pad_right(buffer, bs, padding_at);
     177 + printf("%s", buffer);
     178 + 
     179 + snprintf(buffer, bs, "%lih %02lim %02lis", entry->duration / 60 / 60,
     180 + (entry->duration / 60) % 60, entry->duration % 60);
     181 + pad_right(buffer, bs, padding_duration);
     182 + printf("%s", buffer);
     183 + 
     184 + snprintf(buffer, bs, "%s", entry->tags);
     185 + pad_right(buffer, bs, padding_tags);
     186 + printf("%s", buffer);
     187 + 
     188 + printf("%s\n", entry->message);
     189 + }
     190 + }
     191 + 
     192 + ziit_message__free_unpacked(response, NULL);
     193 + ziit_connection_close(&con);
     194 + 
     195 + return EXIT_SUCCESS;
     196 + 
     197 +err_result:
     198 + ziit_message__free_unpacked(response, NULL);
     199 +err_response:
     200 +err_send:
     201 + ziit_connection_close(&con);
     202 +err_open:
     203 + return -1;
     204 +}
     205 + 
  • src/list.h
    ■ ■ ■ ■ ■ ■
     1 +#pragma once
     2 + 
     3 +#include <confuse.h>
     4 + 
     5 +int run_list(int argc, char *argv[], cfg_t *cfg);
     6 + 
  • src/main.c
    ■ ■ ■ ■
    skipped 1 lines
    2 2  #include <strings.h>
    3 3   
    4 4  #include "add.h"
    5  -#include "config.h"
    6 5  #include "constants.h"
     6 +#include "config.h"
    7 7  #include "init.h"
     8 +#include "list.h"
    8 9  #include "remove.h"
    9 10  #include "status.h"
    10 11  #include <cargs.h>
    skipped 48 lines
    59 60   result = run_add(argc, argv, cfg);
    60 61   } else if (strcmp(subprogram, "remove") == 0) {
    61 62   result = run_remove(argc, argv, cfg);
     63 + } else if (strcmp(subprogram, "list") == 0) {
     64 + result = run_list(argc, argv, cfg);
    62 65   } else {
    63 66   printf("Unknown command.\n");
    64 67   }
    skipped 13 lines
  • src/message.proto
    ■ ■ ■ ■ ■ ■
    skipped 59 lines
    60 60   
    61 61  message ZiitListRequest {
    62 62   bytes cookie = 1;
    63  - uint64 from = 2;
    64  - uint64 to = 3;
    65  - uint64 interval = 4;
     63 + uint64 start = 2;
     64 + uint64 offset = 3;
    66 65  }
    67 66   
    68 67  message ZiitListResponse {
    69 68   StatusCode code = 1;
    70  - repeated ZiitEntry entries = 3;
     69 + repeated ZiitEntry entries = 2;
    71 70  }
    72 71   
    73 72  message ZiitSummaryRequest {
    skipped 44 lines
    118 117   ZiitModifyRequest modify_request = 8;
    119 118   ZiitModifyResponse modify_response = 9;
    120 119   ZiitListRequest list_request = 10;
    121  - ZiitSummaryRequest summary_request = 11;
    122  - ZiitSummaryResponse summary_response = 12;
     120 + ZiitListResponse list_response = 11;
     121 + ZiitSummaryRequest summary_request = 12;
     122 + ZiitSummaryResponse summary_response = 13;
    123 123   }
    124 124  }
pull request 1 of 1
Submitter Lukas Iklé
Target master
Source list-feature
Reviewers
Assignees
Merge Strategy
Create Merge Commit
Watchers (2)
Reference
pull request acosom/ziit/ziit-cli#7
Please wait...
Page is in error, reload to recover