Skip to content

Commit d47deeb

Browse files
committed
Pack bash initialization into an installed script
Issue: #17
1 parent 37dff63 commit d47deeb

File tree

3 files changed

+66
-40
lines changed

3 files changed

+66
-40
lines changed

README.md

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -170,48 +170,14 @@ if you want to use the capabilities of the _ai-cli_ library, configure your syst
170170
to use the Homebrew commands in preference to the ones supplied with macOS.
171171

172172
## Shell startup configuration
173-
You can configure the _ai-cli_ library to be always available in your _bash_ shell by
174-
adding the following lines in your `.bashrc` file.
175-
Adjust the `AI_CLI_LIB` setting to match the _ai-cli_ library installation path;
173+
You can configure the _ai-cli_ library to be always available in your _bash_
174+
shell by adding the following lines in your `.bashrc` file
175+
(ideally near its beginning for performance reasons).
176+
Adjust the provided path match the _ai-cli_ library installation path;
176177
it is currently set for a local installation in your home directory.
177178
```bash
178-
# >>> initialize the ai-cli library >>>
179-
180-
# Location of the ai-cli shared library; adjust as needed.
181-
AI_CLI_LIB=("$HOME/lib/ai_cli."*)
182-
183-
# Execute only if configured, installed, and not initialized.
184-
if [[ -r ~/.aicliconfig && -r $AI_CLI_LIB && "$LD_PRELOAD" != *$AI_CLI_LIB* ]]
185-
then
186-
# Set Linux and Cygwin environment variable.
187-
if [ -z "$LD_PRELOAD" ] ; then
188-
export LD_PRELOAD="$AI_CLI_LIB"
189-
else
190-
LD_PRELOAD="$LD_PRELOAD:$AI_CLI_LIB"
191-
fi
192-
193-
# Set macOS environment variables.
194-
if [ -z "$DYLD_LIBRARY_PATH" ] ; then
195-
export DYLD_LIBRARY_PATH=/opt/homebrew/lib
196-
else
197-
DYLD_LIBRARY_PATH="/opt/homebrew/lib:$DYLD_LIBRARY_PATH"
198-
fi
199-
if [ -z "$DYLD_INSERT_LIBRARIES" ] ; then
200-
export DYLD_INSERT_LIBRARIES="$AI_CLI_LIB"
201-
else
202-
DYLD_INSERT_LIBRARIES="$LD_PRELOAD:$AI_CLI_LIB"
203-
fi
204-
205-
# Overlay current bash with a new instance, which will include the required
206-
# environment variables.
207-
if shopt -q login_shell ; then
208-
exec -l bash
209-
else
210-
exec bash
211-
fi
212-
213-
fi
214-
# <<< initialize the ai-cli library <<<
179+
# Initialize the ai-cli library
180+
source $HOME/share/ai-cli/ai-cli-activate-bash.sh
215181
```
216182

217183
## Reference documentation

src/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ MANPREFIX ?= "$(PREFIX)/share/man/"
2626
SHAREPREFIX ?= "$(PREFIX)/share/ai-cli"
2727

2828
PROGS=rl_driver $(SHARED_LIB)
29+
ACTIVATION_SCRIPTS=$(wildcard ai-cli-activate-*)
2930
RL_SRC=ai_cli.c config.c ini.c fetch_anthropic.c fetch_hal.c fetch_openai.c \
3031
fetch_llamacpp.c support.c
3132
TEST_SRC=$(wildcard *_test.c)
@@ -113,6 +114,9 @@ install: ai_cli.$(DLL_EXTENSION) # Help: Install library and manual pages
113114
install -m 644 ai_cli.5 $(DESTDIR)$(MANPREFIX)/man5
114115
install -m 644 ai_cli.7 $(DESTDIR)$(MANPREFIX)/man7
115116
install -m 644 ai-cli-config $(DESTDIR)$(SHAREPREFIX)/config
117+
for s in $(ACTIVATION_SCRIPTS) ; do \
118+
sed -e "s|__LIBPREFIX__|$(LIBPREFIX)|" $$s >$(DESTDIR)$(SHAREPREFIX)/$$s ; \
119+
done
116120

117121
help: # Help: Show this help message
118122
@echo 'The following make targets are available.'

src/ai-cli-activate-bash.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#
2+
# ai_cli - readline wrapper to obtain a generative AI suggestion
3+
#
4+
# Bash commands to activate ai-cli
5+
#
6+
# Copyright 2023-2024 Diomidis Spinellis
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
#
20+
21+
if [ "$0" != bash -a "$0" != -bash ] ; then
22+
echo 'The ai-cli-lib activation script must be sourced, not executed.' 1>&2
23+
exit 1
24+
fi
25+
26+
AI_CLI_LIB=("__LIBPREFIX__/ai_cli."*)
27+
if [[ $- == *i* # Shell is interactive
28+
&& -r ~/.aicliconfig # User has configured script
29+
&& -r $AI_CLI_LIB # Library installed
30+
&& "$LD_PRELOAD" != *$AI_CLI_LIB* ]] # Variable not set
31+
then
32+
# Linux and Cygwin (is also used for checking)
33+
if [ -z "$LD_PRELOAD" ] ; then
34+
export LD_PRELOAD="$AI_CLI_LIB"
35+
else
36+
LD_PRELOAD="$LD_PRELOAD:$AI_CLI_LIB"
37+
fi
38+
39+
# macOS
40+
if [ -z "$DYLD_LIBRARY_PATH" ] ; then
41+
export DYLD_LIBRARY_PATH=/opt/homebrew/lib
42+
else
43+
DYLD_LIBRARY_PATH="/opt/homebrew/lib:$DYLD_LIBRARY_PATH"
44+
fi
45+
if [ -z "$DYLD_INSERT_LIBRARIES" ] ; then
46+
export DYLD_INSERT_LIBRARIES="$AI_CLI_LIB"
47+
else
48+
DYLD_INSERT_LIBRARIES="$LD_PRELOAD:$AI_CLI_LIB"
49+
fi
50+
51+
if shopt -q login_shell ; then
52+
exec -l bash
53+
else
54+
exec bash
55+
fi
56+
fi

0 commit comments

Comments
 (0)