initial commit
2025-03-29 22:09:24 +0000 UTC
Björn Åström <b@strm.space>
+1notail
+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.
+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:+18rm -f notail+19+20.PHONY: install+21install:+22install -m755 ./notail /usr/local/bin/notail
+1# notail+2## Requirements+3* GLib 2.0+4* libnotify+5+6## Compiling and running+7```sh+8make+9make install+10journalctl -xef | notail+11```
+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 {+11NotifyNotification *notification;+12gboolean running;+13char *summary;+14gboolean close_on_exit;+15int close_delay;+16gboolean is_closed;+17gchar read_buf[READ_BUF_SIZE];+18gsize read_buf_pos;+19GString *notification_contents;+20gsize max_notification_contents_len;+21};+22+23static void notail_init(struct notail *state) {+24state->running = FALSE;+25state->summary = "notail";+26state->close_on_exit = FALSE;+27state->close_delay = 0;+28state->is_closed = FALSE;+29state->max_notification_contents_len = 5000;+30state->read_buf_pos = 0;+31state->notification_contents = g_string_new(NULL);+32}+33+34static void stop_main_loop(struct notail *state) {+35state->running = FALSE;+36g_main_context_wakeup(NULL);+37}+38+39static gboolean fd_callback(GIOChannel* source, GIOCondition condition, gpointer user_data) {+40struct notail *state = (struct notail *) user_data;+41+42if (!(condition & G_IO_IN)) {+43stop_main_loop(state);+44+45return FALSE;+46}+47+48gsize n;+49GIOStatus status = g_io_channel_read_chars(source, state->read_buf + state->read_buf_pos, READ_BUF_SIZE - state->read_buf_pos - 1, &n, NULL);+50state->read_buf_pos += n;+51state->read_buf[state->read_buf_pos] = '\0';+52+53switch (status) {+54case G_IO_STATUS_EOF:+55case G_IO_STATUS_ERROR:+56stop_main_loop(state);+57+58return FALSE;+59case G_IO_STATUS_AGAIN:+60return TRUE;+61case G_IO_STATUS_NORMAL:+62state->read_buf_pos = 0;+63break;+64}+65+66g_string_append(state->notification_contents, state->read_buf);+67+68gsize notification_contents_len = g_utf8_strlen(state->notification_contents->str, -1);+69if (notification_contents_len > state->max_notification_contents_len) {+70GString *new_notification_contents = g_string_new(g_utf8_substring(state->notification_contents->str, notification_contents_len - state->max_notification_contents_len, -1));+71g_string_free(state->notification_contents, TRUE);+72state->notification_contents = new_notification_contents;+73}+74+75notify_notification_update(state->notification, state->summary, state->notification_contents->str, NULL);+76+77return TRUE;+78}+79+80static void notification_closed_callback(NotifyNotification* notification, gpointer user_data)+81{+82struct notail *state = (struct notail *) user_data;+83+84state->is_closed = TRUE;+85+86stop_main_loop(state);+87}+88+89static gboolean sigint_callback(gpointer user_data)+90{+91struct notail *state = (struct notail *) user_data;+92+93stop_main_loop(state);+94+95return FALSE;+96}+97+98static gboolean show_notification(gpointer user_data) {+99struct notail *state = (struct notail *) user_data;+100+101notify_notification_show(state->notification, NULL);+102+103return TRUE;+104}+105+106static int notail_run(struct notail *state) {+107notify_init("notail");+108+109state->notification = notify_notification_new(state->summary, " ", NULL);+110+111g_unix_signal_add (SIGINT, sigint_callback, state);+112g_signal_connect(G_OBJECT(state->notification),+113"closed",+114G_CALLBACK(notification_closed_callback),+115state);+116+117notify_notification_set_timeout(state->notification, 0);+118//notify_notification_show(state->notification, NULL);+119+120GIOChannel *io_channel = g_io_channel_unix_new(0);+121g_io_channel_set_encoding(io_channel, "UTF-8", NULL);+122g_io_channel_set_flags(io_channel, G_IO_FLAG_NONBLOCK, NULL);+123g_io_add_watch(io_channel, G_IO_IN | G_IO_HUP | G_IO_ERR, fd_callback, state);+124+125g_timeout_add(100, show_notification, state);+126+127state->running = TRUE;+128while (state->running) {+129g_main_context_iteration(NULL, TRUE);+130}+131+132g_io_channel_shutdown(io_channel, FALSE, NULL);+133g_io_channel_unref(io_channel);+134+135if (!state->is_closed) {+136// Make sure the notification is displayed at least once before the program ends.+137notify_notification_show(state->notification, NULL);+138+139if (state->close_on_exit) {+140if (state->close_delay != 0) {+141notify_notification_set_timeout(state->notification, state->close_delay);+142notify_notification_show(state->notification, NULL);+143} else {+144notify_notification_close(state->notification, NULL);+145}+146}+147}+148+149g_object_unref(G_OBJECT(state->notification));+150notify_uninit();+151+152return 0;+153}+154+155void print_version()+156{+157#ifdef VERSION+158printf("notail version %s\n", VERSION);+159#else+160printf("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{+175printf("%s", help);+176}+177+178int main(int argc, char *argv[])+179{+180int c;+181int option_index = 0;+182struct notail state = {0};+183+184while (1) {+185static 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+195c = getopt_long(argc, argv, "hvcd:s:C:", long_options, &option_index);+196+197if (c == -1) {+198break;+199}+200+201switch (c) {+202case 'h':+203print_help();+204return EXIT_SUCCESS;+205case 'v':+206print_version();+207return EXIT_SUCCESS;+208case 'c':+209state.close_on_exit = TRUE;+210break;+211case 'd':+212state.close_delay = atoi(optarg);+213break;+214case 's':+215state.summary = optarg;+216break;+217case 'C':+218state.max_notification_contents_len = atoi(optarg);+219break;+220}+221}+222+223notail_init(&state);+224+225if(notail_run(&state)) {+226return EXIT_FAILURE;+227}+228+229return EXIT_SUCCESS;+230}