Skip to content
Open
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
16 changes: 8 additions & 8 deletions tests/disable.t
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
After being disabled, plugin is not listed anymore
$ $ZPM reset
$ $ZPM "$ZPM_TEST_REPOS/example-plugin" > /dev/null
$ $ZPM disable "$ZPM_TEST_REPOS/example-plugin" > /dev/null
$ $ZPM "$ZPM_TEST_REPOS/example-plugin" > /dev/null 2>&1
$ $ZPM disable "$ZPM_TEST_REPOS/example-plugin" > /dev/null 2>&1
$ $ZPM list
Nothing to show.
[ZPM] Nothing to show.
[1]

Plugin directory is not removed after disable
$ $ZPM reset
$ $ZPM "$ZPM_TEST_REPOS/example-plugin" > /dev/null
$ $ZPM disable "$ZPM_TEST_REPOS/example-plugin" > /dev/null
$ $ZPM "$ZPM_TEST_REPOS/example-plugin" > /dev/null 2>&1
$ $ZPM disable "$ZPM_TEST_REPOS/example-plugin" > /dev/null 2>&1
$ ls $ZPM_TEST_REPOS
example-plugin
.* (re)

Plugin can be properly enabled/installed after "disable"
$ $ZPM reset
$ $ZPM "$ZPM_TEST_REPOS/example-plugin" > /dev/null
$ $ZPM disable "$ZPM_TEST_REPOS/example-plugin" > /dev/null
$ $ZPM "$ZPM_TEST_REPOS/example-plugin" > /dev/null
$ $ZPM "$ZPM_TEST_REPOS/example-plugin" > /dev/null 2>&1
$ $ZPM disable "$ZPM_TEST_REPOS/example-plugin"> /dev/null 2>&1
$ $ZPM "$ZPM_TEST_REPOS/example-plugin" > /dev/null 2>&1
$ $ZPM list
.*example-plugin@.* (re)
8 changes: 4 additions & 4 deletions tests/install.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ Can install multiple plugins under the same user.

$ $ZPM reset
$ $ZPM "$ZPM_TEST_REPOS/example-plugin"
Installing .*/example-plugin... Done. (re)
\[ZPM\] Installing .*/example-plugin... Done. (re)
$ $ZPM "$ZPM_TEST_REPOS/example-theme"
Installing .*/example-theme... Done. (re)
\[ZPM\] Installing .*/example-theme... Done. (re)
$ ls $ZPM_TEST_REPOS
example-plugin (re)
example-theme (re)

Avoid installing plugin twice
$ $ZPM reset
$ $ZPM "$ZPM_TEST_REPOS/example-plugin" > /dev/null
$ $ZPM "$ZPM_TEST_REPOS/example-plugin" > /dev/null 2>&1
$ $ZPM "$ZPM_TEST_REPOS/example-plugin"
Plugin ".*/example-plugin" already installed. (re)
\[ZPM\] Plugin ".*/example-plugin" already installed. (re)
[1]
10 changes: 5 additions & 5 deletions tests/list.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ Command `list` without any plugin registered show a message.

$ $ZPM reset
$ $ZPM list
Nothing to show.
[ZPM] Nothing to show.
[1]

Can create a new list.

$ $ZPM reset
$ $ZPM "$ZPM_TEST_REPOS/example-plugin"
Installing .*/example-plugin... Done. (re)
\[ZPM\] Installing .*/example-plugin... Done. (re)

$ cat $HOME/.zpm-init.zsh
source .*/example-plugin/example.plugin.zsh (re)
Expand All @@ -22,7 +22,7 @@ Can create a new list.
Subsequentially add a new item to the list.

$ $ZPM "$ZPM_TEST_REPOS/example-theme"
Installing .*/example-theme... Done. (re)
\[ZPM\] Installing .*/example-theme... Done. (re)
$ cat $HOME/.zpm-init.zsh
source .*/example-plugin/example.plugin.zsh (re)
fpath\+=.*/example-plugin (re)
Expand All @@ -41,8 +41,8 @@ List command show list of installed plugins.
Do not allow duplicate plugin entries/items.

$ $ZPM reset
$ $ZPM "$ZPM_TEST_REPOS/example-theme" > /dev/null
$ $ZPM "$ZPM_TEST_REPOS/example-theme" > /dev/null
$ $ZPM "$ZPM_TEST_REPOS/example-theme" > /dev/null 2>&1
$ $ZPM "$ZPM_TEST_REPOS/example-theme" > /dev/null 2>&1
[1]
$ $ZPM list
.*example-theme@.* (re)
16 changes: 8 additions & 8 deletions tests/remove.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
Can't remove non-installed plugin
$ $ZPM reset
$ $ZPM remove non_installed_plugin
Plugin "non_installed_plugin" is not installed.
[ZPM] Plugin "non_installed_plugin" is not installed.
[1]

Plugin parent directory is not removed if not empty
$ $ZPM reset
$ $ZPM "zsh-users/zsh-syntax-highlighting" > /dev/null
$ $ZPM "zsh-users/zsh-autosuggestions" > /dev/null
$ $ZPM remove "zsh-users/zsh-syntax-highlighting" > /dev/null
$ $ZPM "zsh-users/zsh-syntax-highlighting" > /dev/null 2>&1
$ $ZPM "zsh-users/zsh-autosuggestions" > /dev/null 2>&1
$ $ZPM remove "zsh-users/zsh-syntax-highlighting" > /dev/null 2>&1
$ ls $HOME/.zpm/plugins/zsh-users/
zsh-autosuggestions

Expand All @@ -19,9 +19,9 @@ Plugin directory is properly unlinked after remove and parent directory is prope
$ $ZPM list
zsh-users/zsh-autosuggestions@.* (re)

$ $ZPM "zsh-users/zsh-syntax-highlighting" > /dev/null
$ $ZPM remove "zsh-users/zsh-syntax-highlighting" > /dev/null
$ $ZPM remove "zsh-users/zsh-autosuggestions" > /dev/null
$ $ZPM "zsh-users/zsh-syntax-highlighting" > /dev/null 2>&1
$ $ZPM remove "zsh-users/zsh-syntax-highlighting" > /dev/null 2>&1
$ $ZPM remove "zsh-users/zsh-autosuggestions" > /dev/null 2>&1

$ [ -d $HOME/.zpm/plugins/zsh-users/zsh-syntax-highlighting ]
[1]
Expand All @@ -31,6 +31,6 @@ Plugin directory is properly unlinked after remove and parent directory is prope

After being removed, plugin is not listed anymore
$ $ZPM list
Nothing to show.
[ZPM] Nothing to show.
[1]

2 changes: 1 addition & 1 deletion tests/save.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
Test error message when .$ZPM-init.zsh is absent.
$ $ZPM reset
$ $ZPM save
Could not open ".*". Check the file exists and can be read. (re)
\[ZPM\] Could not open ".*". Check the file exists and can be read. (re)
[1]
92 changes: 60 additions & 32 deletions zpm.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <errno.h>
#include <unistd.h>
#include <limits.h>

#include <stdarg.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
Expand All @@ -19,6 +19,34 @@

static char zpm_init[PATH_MAX];
static char zpm_list[PATH_MAX];
static int zpm_has_color = 0;
static const char* zpm_usage = "Usage:\n\tzpm \"zsh-users/zsh-syntax-highlighting\"\n\tzpm disable \"zsh-users/zsh-syntax-highlighting\"\n\
\tzpm remove \"zsh-users/zsh-syntax-highlighting\"\n\nAvailable commands:\n\tzpm reset\n\tzpm list\n\tzpm update\n\tzpm save\n\tzpm help\n\tzpm version";

enum ZPM_MSG_TYPE {
ZPM_MSG,
ZPM_ERR,
ZPM_LOG,
ZPM_DEBUG,
ZPM_NONE
};

void zpm_msg(enum ZPM_MSG_TYPE type, const char* fmt, ...) {
char msg[PATH_MAX];
FILE* f = type == ZPM_ERR ? stderr : stdout;
va_list arg;
va_start(arg, fmt);
vsnprintf(msg, 256, fmt, arg);
if (type != ZPM_NONE) {
if(zpm_has_color) {
int color = type == ZPM_ERR ? 31 : 32;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check coding style.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I mostly pasted untested code from the issue, so while fixing I forgot that 😕

fprintf(f, "\033[1m[\033[0m\033[%imZPM\033[0m\033[1m]\033[0m ", color);
} else {
fprintf(f, "[ZPM] ");
}
} fprintf(f, "%s", msg);
va_end(arg);
}

char* generate_plugin_path(char* plugin_name) {
if (plugin_name[0] == '/') {
Expand Down Expand Up @@ -270,7 +298,6 @@ char* generate_repository_url(char* plugin_name) {
} else {
strcpy(url, plugin_name);
}


return url;
}
Expand Down Expand Up @@ -329,10 +356,10 @@ int plugins_update_local_clone() {
char* plugin_name = strtok(listing, "\n");

if (!strcmp(listing, "Nothing to show.")) {
printf("Nothing to update.");
zpm_msg(ZPM_ERR, "Nothing to update.");
return 1;
}
printf("Updating plugins...\n");
zpm_msg(ZPM_MSG, "Updating plugins...\n");
while (plugin_name) {
if (plugin_name[0] == '/') {
plugin_name = strtok(NULL, "\n");
Expand All @@ -341,7 +368,7 @@ int plugins_update_local_clone() {
strcpy(command, "cd ~/.zpm/plugins/");
strcat(command, plugin_name);
strcat(command, "; git pull");
printf("Updating %s...\n", plugin_name);
zpm_msg(ZPM_MSG, "Updating %s...\n", plugin_name);
ret = system(command);
plugin_name = strtok(NULL, "\n");
}
Expand All @@ -350,11 +377,7 @@ int plugins_update_local_clone() {
}

int usage(char* ret) {
printf("%s\n", "Usage:\n\tzpm \"zsh-users/zsh-syntax-highlighting\"");
printf("%s\n", "\tzpm disable \"zsh-users/zsh-syntax-highlighting\"");
printf("%s\n", "\tzpm remove \"zsh-users/zsh-syntax-highlighting\"");
printf("%s\n", "\nAvailable commands:\n\tzpm reset\n\tzpm list");
printf("%s\n", "\tzpm update\n\tzpm save\n\tzpm help\n\tzpm version");
zpm_msg(ZPM_NONE, "%s\n", zpm_usage);
return ret ? 0 : 1;
}

Expand All @@ -380,7 +403,7 @@ char* plugin_get_hash(char* plugin_name) {
fp = popen(command, "r");
if (!fp) {
free(plugin_path);
printf("Failed to run command\n" );
zpm_msg(ZPM_ERR, "Failed to run command\n" );
return NULL;
}
fgets(plugin_hash, sizeof(plugin_hash), fp);
Expand All @@ -394,18 +417,25 @@ int plugin_print_list() {
char* plugin_name = strtok(listing, "\n");

if (!strcmp(listing, "Nothing to show.")) {
printf("%s\n", listing);
zpm_msg(ZPM_MSG, "%s\n", listing);
free(listing);
return 1;
}
while (plugin_name) {
char* hash = plugin_get_hash(plugin_name);
printf("%s", plugin_name);
if (hash) {
printf("@%s", hash);
if(zpm_has_color) {
zpm_msg(ZPM_NONE, "\033[1m%s\033[0m@%s", plugin_name, hash);
} else {
zpm_msg(ZPM_NONE, "%s@%s", plugin_name, hash);
}
free(hash);
} else {
printf("\n");
if(zpm_has_color) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the need to for this condition?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • because bold relates to fancy output.
  • because bold messes with cram 😕

zpm_msg(ZPM_NONE, "\033[1m%s\033[0m\n", plugin_name);
} else {
zpm_msg(ZPM_NONE, "%s\n", plugin_name);
}
}
plugin_name = strtok(NULL, "\n");
}
Expand All @@ -418,34 +448,34 @@ int plugin_print_script() {
FILE* store = fopen(zpm_init, "r");

if (!store) {
printf("Could not open \"%s\". Check the file exists and can be read.\n", zpm_init);
zpm_msg(ZPM_ERR, "Could not open \"%s\". Check the file exists and can be read.\n", zpm_init);
return 1;
}

memset(entry, 0, PATH_MAX);
while (fgets(entry, PATH_MAX, store)) {
char plugin[PATH_MAX];
strncpy(plugin, entry, strlen(entry) -1);
printf("zpm \"%s\"\n", plugin);
zpm_msg(ZPM_NONE, "zpm \"%s\"\n", plugin);
}
fclose(store);
return 0;
}

int plugin_remove_entry(char* plugin_name, char* file_name) {
if (!plugin_name) {
printf("remove/uninstall command needs argument.\n");
zpm_msg(ZPM_ERR, "remove/uninstall command needs argument.\n");
return 1;
}
FILE* store = fopen(file_name, "r");
if (!store) {
printf("Could not open \"%s\". Check the file exists and can be read.\n", file_name);
zpm_msg(ZPM_ERR,"Could not open \"%s\". Check the file exists and can be read.\n", file_name);
return 1;
}

FILE* tmp = fopen("/tmp/.zpm_tmp", "w");
if (!tmp) {
printf("Could not open \"%s\". Check the file can be written.\n", "/tmp/.zpm_tmp");
zpm_msg(ZPM_ERR, "Could not open \"%s\". Check the file can be written.\n", "/tmp/.zpm_tmp");
return 1;
}

Expand Down Expand Up @@ -473,11 +503,11 @@ int plugin_remove_entry(char* plugin_name, char* file_name) {

int plugin_remove(char* plugin_name, int uninstall) {
if (!plugin_name) {
printf("remove needs an argument.\n");
zpm_msg(ZPM_ERR, "remove needs an argument.\n");
return 1;
}
if (!plugin_entry_exists(plugin_name)) {
printf("Plugin \"%s\" is not installed.\n", plugin_name);
zpm_msg(ZPM_ERR, "Plugin \"%s\" is not installed.\n", plugin_name);
return 1;
}
plugin_remove_entry(plugin_name, zpm_init);
Expand Down Expand Up @@ -512,32 +542,30 @@ void zpm_config_init() {

strcpy(zpm_list, home);
strcat(zpm_list, "/.zpm/plugin_list");

zpm_has_color = getenv("ZPM_COLOR") ? 1 : 0;
}

int plugin_install(char* plugin_name) {
int status = strstr(plugin_name, "/") ? 0 : -1;
char install[PATH_MAX];


if (plugin_entry_exists(plugin_name)) {
printf("Plugin \"%s\" already installed.\n", plugin_name);
zpm_msg(ZPM_ERR, "Plugin \"%s\" already installed.\n", plugin_name);
return 1;
}
strcpy(install, "Installing ");
strcat(install, plugin_name);
strcat(install, "... ");
printf("%s", install);
zpm_msg(ZPM_MSG, "Installing %s... ", plugin_name);
fflush(stdout);

if (local_clone_exists(plugin_name) == 1) {
status = locally_clone_plugin(plugin_name);
}

if (status == 0) {
printf("%s\n", "Done.");
zpm_msg(ZPM_NONE, "%s\n", "Done.");
generate_plugin_entry(plugin_name);
plugin_list_add_item(plugin_name);
} else {
printf("%s\n", "Error!");
zpm_msg(ZPM_NONE, "%s\n", "Error!");
}
return status;
}
Expand All @@ -563,7 +591,7 @@ int main(int argc, char* argv[]) {
} else if (strstr(argv[1], "help")) {
return usage(argv[1]);
} else if (strstr(argv[1], "version")) {
printf("%s\n", ZPM_VERSION);
zpm_msg(ZPM_MSG, "%s\n", ZPM_VERSION);
return 0;
} else {
return plugin_install(argv[1]);
Expand Down