notail

initial commit

2025-03-29 22:09:24 +0000 UTC

Björn Åström <b@strm.space>

.gitignore
+1notail
LICENSE
+1MIT License+2Copyright (c) 2020 Björn Åström+3+4Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:+5+6The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.+7+8THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Makefile
+1VERSION = 0.1+2CC ?= gcc+3+4CCFLAGS = '-DVERSION="$(VERSION)"' -Wall -lm -ltcl \+5	$(shell pkg-config --cflags --libs glib-2.0)\+6	$(shell pkg-config --cflags --libs libnotify)+7+8SOURCES = notail.c+9+10.PHONY: all+11all: notail+12+13notail: $(SOURCES)+14	$(CC) -o $@ $(SOURCES) $(CCFLAGS)+15+16.PHONY: clean+17clean:+18	rm -f notail+19+20.PHONY: install+21install:+22	install -m755 ./notail /usr/local/bin/notail
README.md
+1# notail+2## Requirements+3* GLib 2.0+4* libnotify+5+6## Compiling and running+7```sh+8make+9make install+10journalctl -xef | notail+11```
notail.c
+1#include <bits/types/mbstate_t.h>+2#include <glib-2.0/glib.h>+3#include <glib-2.0/glib-unix.h>+4#include <libnotify/notification.h>+5#include <libnotify/notify.h>+6#include <getopt.h>+7+8#define READ_BUF_SIZE 4096+9+10struct notail {+11	NotifyNotification *notification;+12	gboolean running;+13	char *summary;+14	gboolean close_on_exit;+15	int close_delay;+16	gboolean is_closed;+17	gchar read_buf[READ_BUF_SIZE];+18	gsize read_buf_pos;+19	GString *notification_contents;+20	gsize max_notification_contents_len;+21};+22+23static void notail_init(struct notail *state) {+24	state->running = FALSE;+25	state->summary = "notail";+26	state->close_on_exit = FALSE;+27	state->close_delay = 0;+28	state->is_closed = FALSE;+29	state->max_notification_contents_len = 5000;+30	state->read_buf_pos = 0;+31	state->notification_contents = g_string_new(NULL);+32}+33 +34static void stop_main_loop(struct notail *state) {+35	state->running = FALSE;+36	g_main_context_wakeup(NULL);+37}+38+39static gboolean fd_callback(GIOChannel* source, GIOCondition condition, gpointer user_data) {+40	struct notail *state = (struct notail *) user_data;+41+42	if (!(condition & G_IO_IN)) {+43		stop_main_loop(state);+44+45		return FALSE;+46	}+47+48	gsize n;+49	GIOStatus status = g_io_channel_read_chars(source, state->read_buf + state->read_buf_pos, READ_BUF_SIZE - state->read_buf_pos - 1, &n, NULL);+50	state->read_buf_pos += n;+51	state->read_buf[state->read_buf_pos] = '\0';+52+53	switch (status) {+54		case G_IO_STATUS_EOF:+55		case G_IO_STATUS_ERROR:+56			stop_main_loop(state);+57+58			return FALSE;+59		case G_IO_STATUS_AGAIN:+60			return TRUE;+61		case G_IO_STATUS_NORMAL:+62			state->read_buf_pos = 0;+63			break;+64	}+65+66	g_string_append(state->notification_contents, state->read_buf);+67+68	gsize notification_contents_len = g_utf8_strlen(state->notification_contents->str, -1);+69	if (notification_contents_len > state->max_notification_contents_len) {+70		GString *new_notification_contents = g_string_new(g_utf8_substring(state->notification_contents->str, notification_contents_len - state->max_notification_contents_len, -1));+71		g_string_free(state->notification_contents, TRUE);+72		state->notification_contents = new_notification_contents;+73	}+74+75	notify_notification_update(state->notification, state->summary, state->notification_contents->str, NULL);+76+77	return TRUE;+78}+79+80static void notification_closed_callback(NotifyNotification* notification, gpointer user_data)+81{+82	struct notail *state = (struct notail *) user_data;+83+84	state->is_closed = TRUE;+85+86	stop_main_loop(state);+87}+88+89static gboolean sigint_callback(gpointer user_data)+90{+91	struct notail *state = (struct notail *) user_data;+92+93	stop_main_loop(state);+94+95	return FALSE;+96}+97+98static gboolean show_notification(gpointer user_data) {+99	struct notail *state = (struct notail *) user_data;+100+101	notify_notification_show(state->notification, NULL);+102+103	return TRUE;+104}+105+106static int notail_run(struct notail *state) {+107	notify_init("notail");+108+109	state->notification = notify_notification_new(state->summary, " ", NULL);+110+111	g_unix_signal_add (SIGINT, sigint_callback, state);+112	g_signal_connect(G_OBJECT(state->notification),+113			"closed",+114			G_CALLBACK(notification_closed_callback),+115			state);+116+117	notify_notification_set_timeout(state->notification, 0);+118	//notify_notification_show(state->notification, NULL);+119+120	GIOChannel *io_channel = g_io_channel_unix_new(0);+121	g_io_channel_set_encoding(io_channel, "UTF-8", NULL);+122	g_io_channel_set_flags(io_channel, G_IO_FLAG_NONBLOCK, NULL);+123	g_io_add_watch(io_channel, G_IO_IN | G_IO_HUP | G_IO_ERR, fd_callback, state);+124+125	g_timeout_add(100, show_notification, state);+126+127	state->running = TRUE;+128	while (state->running) {+129		g_main_context_iteration(NULL, TRUE);+130	}+131+132	g_io_channel_shutdown(io_channel, FALSE, NULL);+133	g_io_channel_unref(io_channel);+134+135	if (!state->is_closed) {+136		// Make sure the notification is displayed at least once before the program ends.+137		notify_notification_show(state->notification, NULL);+138+139		if (state->close_on_exit) {+140			if (state->close_delay != 0) {+141				notify_notification_set_timeout(state->notification, state->close_delay);+142				notify_notification_show(state->notification, NULL);+143			} else {+144				notify_notification_close(state->notification, NULL);+145			}+146		}+147	}+148+149	g_object_unref(G_OBJECT(state->notification));+150	notify_uninit();+151+152	return 0;+153}+154+155void print_version()+156{+157#ifdef VERSION+158       printf("notail version %s\n", VERSION);+159#else+160       printf("notail (unknown version)\n");+161#endif+162}+163+164char help[] =+165		"usage: notail [options] [command]\n"+166		"  -h, --help                    Display this help text.\n"+167		"  -v, --version                 Display version information.\n"+168		"  -c, --close-on-exit           Close the notification on command exit.\n"+169		"  -d, --close-delay <delay ms>  Delay in milliseconds before closing the notification after the program quits. Only has effect if used together with -c.\n"+170		"  -s, --summary <summary>       Set the summary of the notification.\n"+171		"  -C  --num-chars <num>         Output the last num characters of the output in the notification\n";+172+173void print_help()+174{+175	printf("%s", help);+176}+177+178int main(int argc, char *argv[])+179{+180	int c;+181	int option_index = 0;+182	struct notail state = {0};+183+184	while (1) {+185		static struct option long_options[] = {+186			{"help", no_argument, 0, 'h'},+187			{"version", no_argument, 0, 'v'},+188			{"close-on-exit", no_argument, 0, 'c'},+189			{"close-delay", required_argument, 0, 'd'},+190			{"summary", required_argument, 0, 's'},+191			{"num-chars", required_argument, 0, 'C'},+192			{0, 0, 0, 0},+193		};+194+195		c = getopt_long(argc, argv, "hvcd:s:C:", long_options, &option_index);+196+197		if (c == -1) {+198			break;+199		}+200+201		switch (c) {+202		case 'h':+203			print_help();+204			return EXIT_SUCCESS;+205		case 'v':+206			print_version();+207			return EXIT_SUCCESS;+208		case 'c':+209			state.close_on_exit = TRUE;+210			break;+211		case 'd':+212			state.close_delay = atoi(optarg);+213			break;+214		case 's':+215			state.summary = optarg;+216			break;+217		case 'C':+218			state.max_notification_contents_len = atoi(optarg);+219			break;+220		}+221	}+222+223	notail_init(&state);+224+225	if(notail_run(&state)) {+226		return EXIT_FAILURE;+227	}+228+229	return EXIT_SUCCESS;+230}
srcg
version 69be645