Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaIvanovV committed Jul 27, 2022
0 parents commit 4ad51f9
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
53 changes: 53 additions & 0 deletions catless
Original file line number Diff line number Diff line change
@@ -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 -<N> 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)
}

0 comments on commit 4ad51f9

Please sign in to comment.