Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #10 from alexfornuto/2023-autocomplete
Browse files Browse the repository at this point in the history
Enable Terminus Autocomplete Automatically
  • Loading branch information
alexfornuto committed Oct 12, 2019
2 parents a9ba492 + 7c1822b commit 63e516a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ jobs:
build:
docker:
- image: circleci/php:5.6.32-cli-jessie
working_directory: ~/build
steps:
- checkout
- run: sudo apt install -y xdotool
- run: php installer.php
- run: terminus --version
- run:
name: Test Autocomplete
command: |
cd ~/build
./tests/completion.sh
38 changes: 37 additions & 1 deletion installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ function ammendPath($rcfile, $installdir, &$pathUpdated)
return $pathUpdated;
}

function checkCompletion($rcfile)
{
if( strpos(file_get_contents(getenv('HOME') . "/$rcfile"), "eval \"$(terminus autocomplete)\"") !== false)
{
return false;
}
else {
return true;
}
}

function enableAutoComplete($rcfile)
{
$evalCompletion = file_put_contents(getenv('HOME') . "/$rcfile", "\n# Enable Terminus autocomplete\neval \"$(terminus autocomplete)\"", FILE_APPEND | LOCK_EX);
if (!$evalCompletion) {
throw new Exception($rcfile . " found, but unable to write to it.");
}

return $evalCompletion;

}

// Function to determine if ~/.terminus/bin is already in $PATH
function checkpath($paths, $installdir)
{
Expand All @@ -86,7 +108,7 @@ function checkpath($paths, $installdir)
echo("Making Terminus executable... ");
chmod($installdir . "/" . $package, 0755)
or exit("\nUnable to set Terminus as executable.\n");
echo("Done.\n\n");
echo("Done.\n");

// If $installdir isn't in path, add it.
if (checkpath($paths, $installdir) === false) {
Expand All @@ -103,4 +125,18 @@ function checkpath($paths, $installdir)
" to your PATH, or execute Terminus from the full path.\n\n");
}
}

// Check for autocompletion in rcfile and amend if not
foreach ($rcfiles as $rcfile) {
if (file_exists(getenv('HOME') . "/$rcfile") && is_writable(getenv('HOME') . "/$rcfile")){
if (checkCompletion($rcfile) === true) {
enableAutoComplete($rcfile);
echo("Added Autocompletion to " . $rcfile . "\n");
}
else {
echo("Found autocompletion setting for Terminus in " . "/$rcfile" . "\n" );
}
}
}

exit();
15 changes: 15 additions & 0 deletions tests/completion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#! /bin/bash
touch completionresults.txt
script -c "terminus site:$(xdotool key Tab)" completionresults.txt

printf "\n\n"

if (grep -q "Did you mean one of these?" "completionresults.txt")
then
echo "Completion works!"
exit 0
else
echo "Completion test failed"
exit 1
fi

0 comments on commit 63e516a

Please sign in to comment.