Skip to content

Commit

Permalink
Get working properly
Browse files Browse the repository at this point in the history
  • Loading branch information
mskelton committed Nov 8, 2022
1 parent 8d428bf commit b47f172
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 10,31 @@ set -g @plugin 'mskelton/tmux-last'

## Configuration

#### @tmux-last-command-key
### @tmux-last-command-key

Set's the key that should be bound to open the last command output. Defaults to `t`.

```tmux
set -g @tmux-last-command-key t
```

#### @tmux-last-prompt-pattern
### @tmux-last-prompt-pattern

A regexp to identify command separator. Usually a prompt. For example, if set to '] % ', the plugin will capture the latest output up until the first line that contains '] % '.

```tmux
set -g @tmux-last-prompt-pattern '] % '
```

#### @tmux-last-pager
### @tmux-last-prompt-lines

The number of lines consumed by the prompt. If your prompt takes up two lines, you could set this to `2`. Defaults to `1`.

```tmux
set -g @tmux-last-prompt-pattern 1
```

### @tmux-last-pager

The pager to use for the captured output. Defaults to `$PAGER`.

Expand Down
33 changes: 28 additions & 5 deletions plugin.sh
Original file line number Diff line number Diff line change
@@ -1,10 1,33 @@
#!/usr/bin/env bash

x=$(tmux capture-pane -p -S '-' -J -t !)
PROMPT_PATTERN=${PROMPT_PATTERN:-" ] % "}
result=$(echo "$x" | cat | sed -e "0,/$PROMPT_PATTERN/d" | sed "/$PROMPT_PATTERN/,\$d" | cat)
#!/bin/bash

# Get the custom page or use the default system pager as a fallback
PAGER_CMD=${PAGER_CMD:-"$PAGER"}
PAGER_CMD=${PAGER:-less}

PROMPT_PATTERN=${PROMPT_PATTERN:-"\$"}
PROMPT_LINES=${PROMPT_LINES:-1}

# TODO: Rewrite this to be faster
function get_last_non_empty_output {
found_output=0

while IFS='' read line; do
if [[ "$line" =~ $PROMPT_PATTERN ]]; then
# Once we found non-empty output, exit earily to finish
if [[ $found_output == 1 ]]; then
return
fi

# Consume and discard additional prompt lines
for ((i = 1; i < $PROMPT_LINES; i = 1)); do read; done
else
found_output=1
echo "$line"
fi
done
}

contents=$(tmux capture-pane -p -S '-' -J -t !)
result=$(echo "$contents" | tail -r | get_last_non_empty_output | tail -r)

echo "$result" | $PAGER_CMD
3 changes: 2 additions & 1 deletion tmux-last.tmux
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 4,7 @@ KEY=$(tmux show-option -gqv @tmux-last-capture-key)
KEY=${KEY:-t}

PROMPT_PATTERN=$(tmux show-option -gqv @tmux-last-prompt-pattern)
PROMPT_LINES=$(tmux show-option -gqv @tmux-last-prompt-lines)
PAGER_CMD=$(tmux show-option -gqv @tmux-last-pager)

tmux bind $KEY new-window -n last-command-output -e PROMPT_PATTERN="$PROMPT_PATTERN" -e PAGER_CMD="$PAGER_CMD" "$(pwd)/plugin.sh"
tmux bind $KEY new-window -n last-command-output -e PROMPT_PATTERN="$PROMPT_PATTERN" -e PROMPT_LINES="$PROMPT_LINES" -e PAGER_CMD="$PAGER_CMD" "~/dev/tmux-last/plugin.sh"

0 comments on commit b47f172

Please sign in to comment.