commit 0bd28b69691c5c1c1995e8b84d2250a0141a2e8b
parent f868edf76a9dab23497e4d986b5f87a11a1d930d
Author: Claudio Alessi <smoppy@gmail.com>
Date: Thu, 15 Aug 2024 23:23:59 +0200
Implement F_NOTEXT accessible via -x flag (exclude).
Diffstat:
M | sw.1 | | | 7 | +++++-- |
M | sw.c | | | 27 | +++++++++++++++++++-------- |
2 files changed, 24 insertions(+), 10 deletions(-)
diff --git a/sw.1 b/sw.1
@@ -3,7 +3,7 @@
sw \- simple wallet
.SH SYNOPSIS
.B sw
-.RB [ \-v ]\ [ \-dfit \ <arg>]\ [ \-e \ <text>\ ...]\ [ \-l \ <limit>]\ [<date\ [time]\>\ <amount>\ <note>]
+.RB [ \-v ]\ [ \-defiltx \ <arg>]\ [<date\ [time]\>\ <amount>\ <note>]
.SH DESCRIPTION
sw is a wallet management tool which features a simple yet powerful interface
to keep track of your money movements. By default (e.g. if no -l is specified)
@@ -32,4 +32,7 @@ a value of INT_MAX (show all movements).
.TP
.B \-t\ <date\ [time]>
consider only movements before the given date and optional time
-
+.TP
+.B \-x\ <text>
+exclude all movements that include the given string (case insensitive). May
+be specified multiple times.
diff --git a/sw.c b/sw.c
@@ -3,7 +3,6 @@
#define _XOPEN_SOURCE
#define _GNU_SOURCE
-#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -14,7 +13,7 @@
#include "arg.h"
char *argv0;
-enum {F_DATEFROM, F_DATETO, F_TEXT};
+enum {F_DATEFROM, F_DATETO, F_TEXT, F_NOTEXT};
typedef union {
int i;
@@ -106,9 +105,14 @@ addfilter(unsigned int type, void *data) {
Filter *f = ecalloc(1, sizeof(Filter));
switch(type) {
- case F_TEXT: f->arg.v = data; break;
- case F_DATEFROM: f->arg.i = strtots(data); break;
- case F_DATETO: f->arg.i = strtots(data); break;
+ case F_TEXT:
+ case F_NOTEXT:
+ f->arg.v = data;
+ break;
+ case F_DATEFROM:
+ case F_DATETO:
+ f->arg.i = strtots(data);
+ break;
default: die("invalid filter type\n", type);
}
@@ -204,6 +208,10 @@ filtermov(Movement *m) {
break;
ormatch = !!strcasestr(m->note, (char *)f->arg.v);
break;
+ case F_NOTEXT:
+ if(!!strcasestr(m->note, (char *)f->arg.v))
+ return 1;
+ break;
case F_DATEFROM:
if(!(m->ts >= f->arg.i))
return 1;
@@ -214,6 +222,7 @@ filtermov(Movement *m) {
break;
}
}
+
return ormatch == -1 ? 0 : !ormatch;
}
@@ -371,7 +380,7 @@ strtots(char *s) {
void
usage(void) {
- die("Usage: %s [-v] [-dfit <arg>] [-e <text> ...] [-l <limit>] [<date [time]> <amount> <note>]\n", argv0);
+ die("Usage: %s [-v] [-defiltx <arg>] [<date [time]> <amount> <note>]\n", argv0);
}
int
@@ -389,14 +398,16 @@ main(int argc, char *argv[]) {
limit = strtoint(EARGF(usage()));
if(limit < 0)
die("%s: -l: invalid argument\n", argv0);
- if(!limit)
- limit = INT_MAX;
break;
case 't': addfilter(F_DATETO, EARGF(usage())); break;
case 'v': die("sw-"VERSION"\n");
+ case 'x': addfilter(F_NOTEXT, EARGF(usage())); break;
default: usage();
} ARGEND;
+ if(!limit)
+ limit = INT_MAX;
+
if(!*movsfilename)
snprintf(movsfilename, sizeof movsfilename, "%s/%s", getenv("HOME"), ".sw");
movsfile = fopen(movsfilename, "r");