diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c70b5cb --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Nikita Ivanov + +Permission 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: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE 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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2880f3a --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +PREFIX = /usr/local +BINPREFIX = $(DESTDIR)$(PREFIX)/bin +SCRIPT = catless + +INSTALL = install + +all: + +install: + $(INSTALL) -d $(BINPREFIX) + $(INSTALL) $(SCRIPT) $(BINPREFIX) + +uninstall: + $(RM) $(BINPREFIX)/$(SCRIPT) + +.PHONY: all install uninstall diff --git a/README.md b/README.md new file mode 100644 index 0000000..5f3c0d1 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# catless + +A small Awk script that prints input to standard output +if it fits in the terminal window, otherwise opening a pager. + +## Dependencies + +* [Gawk](https://www.gnu.org/software/gawk) + +## Installation + +### Manual + +```sh +git clone https://github.com/NikitaIvanovV/catless +cd catless +sudo make install +``` + +Uninstall with `sudo make uninstall` + +### AUR + +If you are an Arch Linux user, you can install +[`catless-git`](https://aur.archlinux.org/packages/catless-git) +AUR package. + +```sh +yay -S catless-git +``` + +## Usage + +``` +catless file +command | catless +``` + +catless respects `$PAGER` environmental variable. diff --git a/catless b/catless new file mode 100755 index 0000000..76a20ce --- /dev/null +++ b/catless @@ -0,0 +1,53 @@ +#!/usr/bin/gawk -f + +# +# Prints input to standard output if it fits in the terminal window, +# otherwise opening a pager. +# + +function printpager(str) { + printf "%s", str | pager +} + +BEGIN { + pager = "${PAGER:-less}" + + # Don't raise fatal error when pager is exited + PROCINFO[pager, "NONFATAL"] = 1 + + # Get lines and columns in terminal + tput = "tput lines cols" + tput | getline lines + tput | getline cols + close(tput) + + # Process the - argument to override lines + if (ARGC > 1 && ARGV[1] ~ /^-[0-9]+$/) { + lines = ARGV[1] + sub(/^-/, "", lines) + delete ARGV[1] + } +} + +NR <= lines { + buf = buf $0 RS + + # Take into account line wrapping + lines -= int((length-1)/cols) + + if (NR >= lines) { + printpager(buf) + buf = "" + } + + next +} + +{ printpager($0 RS) } + +END { + if (buf != "") + printf "%s", buf + + close(pager) +}