From 51bd183f064d0c0ea5e0184d9f6b6b62e5c01e50 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 3 Aug 2025 15:41:28 +0300 Subject: added --quiet, --stdin and --stdout for easier integration into UNIX pipelines * In principle --stdin and --stdout could have been done in pure TECO code using the <^T> command. Having built-in command-line arguments however has several advantages: * Significantly faster than reading byte-wise with ^T. * Performs EOL normalization unless specifying --8bit of course. * Significantly shortens command-lines. `sciteco -qio` and `sciteco -qi` can be real replacements for sed and awk. * You can even place SciTECO into the middle of a pipeline while editing interactively: foo | sciteco -qio --no-profile | bar Unfortunately, this will not currently work when munging the profile as command-line parameters are also transmitted via the unnamed buffer. This should be changed to use special Q-registers (FIXME). * --quiet can help to improve the test suite (TODO). Should probably be the default in TE_CHECK(). * --stdin and --stdout allow to simplify many SciTECO scripts, avoiding temporary files, especially for womenpage generation (TODO). * For processing potentially infinite streams, you will still have to read using ^T. --- src/view.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'src/view.c') diff --git a/src/view.c b/src/view.c index 0fc1986..e44b386 100644 --- a/src/view.c +++ b/src/view.c @@ -350,6 +350,44 @@ teco_view_load_from_file(teco_view_t *ctx, const gchar *filename, return TRUE; } +/** + * Load stdin until EOF into view's document. + * + * @param ctx The view to load. + * @param clear Whether to completely replace document + * (leaving dot at the beginning of the document) or insert at dot + * (leaving dot at the end of the insertion). + * @param error A GError. + * @return FALSE in case of a GError. + * + * @memberof teco_view_t + */ +gboolean +teco_view_load_from_stdin(teco_view_t *ctx, gboolean clear, GError **error) +{ +#ifdef G_OS_WIN32 + g_autoptr(GIOChannel) channel = g_io_channel_win32_new_fd(0); +#else + g_autoptr(GIOChannel) channel = g_io_channel_unix_new(0); +#endif + g_assert(channel != NULL); + + /* + * The file loading algorithm does not need buffered + * streams, so disabling buffering should increase + * performance (slightly). + */ + g_io_channel_set_encoding(channel, NULL, NULL); + g_io_channel_set_buffered(channel, FALSE); + + if (!teco_view_load_from_channel(ctx, channel, clear, error)) { + g_prefix_error_literal(error, "Error reading stdin: "); + return FALSE; + } + + return TRUE; +} + #if 0 /* @@ -554,6 +592,31 @@ teco_view_save_to_file(teco_view_t *ctx, const gchar *filename, GError **error) return TRUE; } +/** @memberof teco_view_t */ +gboolean +teco_view_save_to_stdout(teco_view_t *ctx, GError **error) +{ +#ifdef G_OS_WIN32 + g_autoptr(GIOChannel) channel = g_io_channel_win32_new_fd(1); +#else + g_autoptr(GIOChannel) channel = g_io_channel_unix_new(1); +#endif + g_assert(channel != NULL); + + /* + * teco_view_save_to_channel() expects a buffered and blocking channel + */ + g_io_channel_set_encoding(channel, NULL, NULL); + g_io_channel_set_buffered(channel, TRUE); + + if (!teco_view_save_to_channel(ctx, channel, error)) { + g_prefix_error_literal(error, "Error writing to stdout: "); + return FALSE; + } + + return TRUE; +} + /** * Convert a glyph index to a byte offset as used by Scintilla. * -- cgit v1.2.3