Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .envrc.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PATH_add .

# for git-changelog (https://github.com/dannyben/git-changelog)
export CHANGELOG_COMMIT_URL=https://github.com/DannyBen/opcode/commit/%h
export CHANGELOG_COMPARE_URL=https://github.com/dannyben/opcode/compare/%s
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Upgrade the outdated bash
run: brew install bash
- name: Upgrade the outdated bash and install completions
run: brew install bash bash-completion
- name: Run setup
run: ./setup
- name: Test setup artifacts
Expand Down
39 changes: 27 additions & 12 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,64 @@
Change Log
Changelog
========================================

v1.0.0 - 2024-05-14
----------------------------------------

- Add support for clean multiline commands (auto-joined with &&) [`9a46eb5`](https://github.com/DannyBen/opcode/commit/9a46eb5)
- Fix completions setup for mac [`7ecdaba`](https://github.com/DannyBen/opcode/commit/7ecdaba)
- Compare [`v0.6.5..v1.0.0`](https://github.com/dannyben/opcode/compare/v0.6.5..v1.0.0)


v0.6.5 - 2024-03-08
----------------------------------------

- Add color functions for use in op.conf
- Add color functions for use in op.conf [`926708c`](https://github.com/DannyBen/opcode/commit/926708c)
- Compare [`v0.6.4..v0.6.5`](https://github.com/dannyben/opcode/compare/v0.6.4..v0.6.5)


v0.6.4 - 2023-03-12
----------------------------------------

- Fix inability to use a one character command
- Normalize with shfmt and shellcheck
- Fix inability to use a one character command [`07b777f`](https://github.com/DannyBen/opcode/commit/07b777f)
- Normalize with shfmt and shellcheck [`73bbd07`](https://github.com/DannyBen/opcode/commit/73bbd07)
- Compare [`v0.6.3..v0.6.4`](https://github.com/dannyben/opcode/compare/v0.6.3..v0.6.4)


v0.6.3 - 2022-11-10
----------------------------------------

- Add private commands
- Add private commands [`bf40d7b`](https://github.com/DannyBen/opcode/commit/bf40d7b)
- Compare [`v0.6.2..v0.6.3`](https://github.com/dannyben/opcode/compare/v0.6.2..v0.6.3)


v0.6.2 - 2022-11-05
----------------------------------------

- Enable bash completions for 'op -w' as well
- Allow running 'op -w' without code to see all codes and commands
- Enable bash completions for 'op -w' as well [`16e0aac`](https://github.com/DannyBen/opcode/commit/16e0aac)
- Allow running 'op -w' without code to see all codes and commands [`0e6c3fd`](https://github.com/DannyBen/opcode/commit/0e6c3fd)
- Compare [`v0.6.1..v0.6.2`](https://github.com/dannyben/opcode/compare/v0.6.1..v0.6.2)


v0.6.1 - 2022-11-04
----------------------------------------

- Prefer exact match over partial match commands
- Prefer exact match over partial match commands [`b04877d`](https://github.com/DannyBen/opcode/commit/b04877d)
- Compare [`v0.6.0..v0.6.1`](https://github.com/dannyben/opcode/compare/v0.6.0..v0.6.1)


v0.6.0 - 2021-05-04
----------------------------------------

- Remove bashcompinit from zsh autocompletion
- Change autocomplete installation
- Remove bashcompinit from zsh autocompletion [`c478257`](https://github.com/DannyBen/opcode/commit/c478257)
- Change autocomplete installation [`f1a104d`](https://github.com/DannyBen/opcode/commit/f1a104d)
- Compare [`v0.5.2..v0.6.0`](https://github.com/dannyben/opcode/compare/v0.5.2..v0.6.0)


v0.5.2 - 2020-10-26
----------------------------------------

- Change setup script to be non-interactive (for CI)
- Update setup script to avoid installing autocomplete if there is no ~/.bashrc
- Change setup script to be non-interactive (for CI) [`aa3b16f`](https://github.com/DannyBen/opcode/commit/aa3b16f)
- Update setup script to avoid installing autocomplete if there is no ~/.bashrc [`e395bbc`](https://github.com/DannyBen/opcode/commit/e395bbc)
- Compare [`v0.5.1..v0.5.2`](https://github.com/dannyben/opcode/compare/v0.5.1..v0.5.2)


36 changes: 31 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Opcode - Local Command Shortcuts

![Version](https://img.shields.io/badge/version-0.6.5-blue.svg)
![Version](https://img.shields.io/badge/version-1.0.0-blue.svg)
![Test](https://github.com/DannyBen/opcode/workflows/Test/badge.svg)

</div>
Expand Down Expand Up @@ -158,12 +158,38 @@ pull

## Multiline Commands

You may split your command to multiple lines by ending the line with a
backslash, and indenting the subsequent lines by at least one space:
In order to specify multiple commands for a single code, provide the commands
indented with one or more spaces immediately under the command code:

```shell
up: docker-compose build && \
docker-compose up web
up:
docker compose build
docker compose up web
```

Note that these commands will be joined together with `&&` - so the above
will be converted to:

```shell
docker compose build && docker compose up web
```

## Concatenated Lines

In case you wish to split your command to multiple lines without joining the
lines with `&&`, you can add escape the newline by using `\` (backslash):

```shell
concat: echo who \
ordered \
this \
pizza
```

Using this syntax will generate this command:

```shell
echo who ordered this pizza
```

## Private Commands
Expand Down
36 changes: 31 additions & 5 deletions doc/op.1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.\" Automatically generated by Pandoc 3.1.9
.\"
.TH "op" "1" "March 2024" "Version 0.6.5" "local command shortcuts"
.TH "op" "1" "May 2024" "Version 1.0.0" "local command shortcuts"
.SH NAME
\f[B]op\f[R] - local command shortcuts
.SH SYNOPSIS
Expand Down Expand Up @@ -137,12 +137,38 @@ clean: rm tmp/*
build: docker build
.EE
.SS Multiline Commands
You may split your command to multiple lines by ending the line with a
backslash, and indenting the subsequent lines by at least one space:
In order to specify multiple commands for a single code, provide the
commands indented with one or more spaces immediately under the command
code:
.IP
.EX
up: docker-compose build && \[rs]
docker-compose up web
up:
docker compose build
docker compose up web
.EE
.PP
Note that these commands will be joined together with \f[B]&&\f[R] - so
the above will be converted to:
.IP
.EX
docker compose build && docker compose up web
.EE
.SS Concatenated Lines
In case you wish to split your command to multiple lines without joining
the lines with \f[B]&&\f[R], you can add escape the newline by using
\f[B]\[rs]\f[R] (backslash):
.IP
.EX
concat: echo who \[rs]
ordered \[rs]
this \[rs]
pizza
.EE
.PP
Using this syntax will generate this command:
.IP
.EX
echo who ordered this pizza
.EE
.SH PARTIAL COMMAND MATCHING
When running a command, opcode will first try to find an exact match.
Expand Down
34 changes: 30 additions & 4 deletions doc/op.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,38 @@ build: docker build

## Multiline Commands

You may split your command to multiple lines by ending the line with a
backslash, and indenting the subsequent lines by at least one space:
In order to specify multiple commands for a single code, provide the commands
indented with one or more spaces immediately under the command code:

```shell
up: docker-compose build && \
docker-compose up web
up:
docker compose build
docker compose up web
```

Note that these commands will be joined together with **&&** - so the above
will be converted to:

```shell
docker compose build && docker compose up web
```

## Concatenated Lines

In case you wish to split your command to multiple lines without joining the
lines with **&&**, you can add escape the newline by using **\\** (backslash):

```shell
concat: echo who \
ordered \
this \
pizza
```

Using this syntax will generate this command:

```shell
echo who ordered this pizza
```


Expand Down
87 changes: 62 additions & 25 deletions op
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ opcode_context() {
fi
}

abort() {
echo "$1" >&2
exit "${2:-1}"
}

need_config() {
if [[ ! -f $CONFIG_FILE ]]; then
echo "Cannot find config file ($CONFIG_FILE)"
exit 1
abort "Cannot find config file ($CONFIG_FILE)"
fi
}

Expand All @@ -63,28 +67,63 @@ opcode_context() {
exit 1
fi

exact="^${CODE}:[[:space:]]*(.+)$"
fuzzy="^${CODE}[^\:]*:[[:space:]]*(.+)$"
COMMAND=$(get_command_from_file "exact")
if [[ -z $COMMAND ]]; then
COMMAND=$(get_command_from_file "fuzzy")
fi
}

# shellcheck disable=SC2162
while IFS= read line || [ -n "$line" ]; do
if [[ $line =~ $exact ]]; then
COMMAND="${BASH_REMATCH[1]}"
break
fi
done <"$CONFIG_FILE"
get_command_from_file() {
local pattern

if [[ -n "$COMMAND" ]]; then
return
if [[ $1 == "fuzzy" ]]; then
pattern="^${CODE}[^:]*:[[:space:]]*(.*)$"
else
pattern="^${CODE}:[[:space:]]*(.*)$"
fi

# shellcheck disable=SC2162
while IFS= read line || [ -n "$line" ]; do
if [[ $line =~ $fuzzy ]]; then
COMMAND="${BASH_REMATCH[1]}"
break
local command=""
local collect_command=false
local temp_line=""

while IFS= read -r line || [[ -n $line ]]; do
if [[ -n "$temp_line" ]]; then
trimmed_line=${line#"${line%%[![:space:]]*}"}
temp_line="${temp_line%\\}$trimmed_line"
if [[ "$line" =~ \\$ ]]; then
continue
else
line="$temp_line"
temp_line=""
fi
fi

if [[ "$line" =~ \\$ ]]; then
trimmed_line=${line#"${line%%[![:space:]]*}"}
temp_line="${temp_line%\\}$trimmed_line"
continue
fi

if $collect_command; then
if [[ -z "${line}" || ! "${line}" =~ ^[[:space:]]+ ]]; then
break
fi

trimmed_line="${line#"${line%%[![:space:]]*}"}"
command="${command:+$command && }$trimmed_line"
else
if [[ $line =~ $pattern ]]; then
command="${BASH_REMATCH[1]}"
if [[ -n $command ]]; then
break
fi

collect_command=true
fi
fi
done <"$CONFIG_FILE"

echo "$command"
}

run_command() {
Expand All @@ -97,8 +136,7 @@ opcode_context() {
eval "$COMMAND" "$@"
fi
else
echo "Code not found: $CODE"
exit 1
abort "Code not found: $CODE"
fi
}

Expand Down Expand Up @@ -135,7 +173,7 @@ opcode_context() {

list_codes() {
need_config
regex="^([^#:]+):"
regex="^([a-zA-Z0-9_-]+):"
break_regex="^[[:space:]]*private[[:space:]]*$"

# shellcheck disable=SC2162
Expand All @@ -151,7 +189,7 @@ opcode_context() {

list_codes_extended() {
need_config
regex="^([^#:]+):"
regex="^([a-zA-Z0-9_-]+):"
comment_regex="^#\? ?(.*)"
break_regex="^[[:space:]]*private[[:space:]]*$"

Expand Down Expand Up @@ -199,8 +237,7 @@ opcode_context() {
# shellcheck disable=SC2086
echo $COMMAND
else
echo "Code not found: $CODE"
exit 1
abort "Code not found: $CODE"
fi

fi
Expand Down Expand Up @@ -270,7 +307,7 @@ opcode_context() {
bold() { printf "\e[1m%b\e[0m\n" "$*"; }

opcode_initialize() {
VERSION="0.6.5"
VERSION="1.0.0"
LONG_USAGE=false
set_config_file
set -e
Expand Down
Loading