Skip to content

Commit

Permalink
kbinds/suggs/TAB: exclude sort values not available in light mode
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-arch committed Feb 23, 2024
1 parent 082b8b9 commit 24cf8c2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 26 deletions.
4 changes: 4 additions & 0 deletions src/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,10 @@ extern time_t curdir_mtime;
#define FILE_SIZE(s) (conf.apparent_size == 1 ? (s).st_size \
: (s).st_blocks * S_BLKSIZE)

/* Do we have the sort method S in light mode? */
#define ST_IN_LIGHT_MODE(s) ((s) == SNAME || (s) == SVER || (s) == SINO \
|| (s) == SEXT || (s) == SNONE)

#define UNUSED(x) (void)(x) /* Just silence the compiler's warning */
#define TOUPPER(c) (((c) >= 'a' && (c) <= 'z') ? ((c) - 'a' + 'A') : (c))
#define TOLOWER(c) (((c) >= 'A' && (c) <= 'Z') ? ((c) - 'A' + 'a') : (c))
Expand Down
10 changes: 9 additions & 1 deletion src/keybinds.c
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,10 @@ rl_sort_next(int count, int key)
conf.sort++;
#endif /* !ST_BTIME */

if (conf.light_mode == 1)
while (!ST_IN_LIGHT_MODE(conf.sort + 1) && conf.sort + 1 <= SORT_TYPES)
conf.sort++;

conf.sort++;
if (conf.sort > SORT_TYPES)
conf.sort = 0;
Expand Down Expand Up @@ -1224,9 +1228,13 @@ rl_sort_previous(int count, int key)
conf.sort--;
#endif /* !ST_BTIME */

if (conf.light_mode == 1)
while (!ST_IN_LIGHT_MODE(conf.sort - 1) && conf.sort - 1 >= 0)
conf.sort--;

conf.sort--;
if (conf.sort < 0)
conf.sort = SORT_TYPES;
conf.sort = conf.light_mode == 1 ? SINO : SORT_TYPES;

if (conf.autols == 1) {
sort_switch = 1;
Expand Down
10 changes: 8 additions & 2 deletions src/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -2061,7 +2061,8 @@ sort_num_generator(const char *text, int state)
i = 0;

int num_text = atoi(text);
if (num_text == INT_MIN)
if (num_text == INT_MIN
|| (conf.light_mode == 1 && !ST_IN_LIGHT_MODE(num_text)))
return (char *)NULL;

static char *const sorts[] = {
Expand Down Expand Up @@ -2172,6 +2173,9 @@ sort_name_generator(const char *text, int state)
}

while ((name = sort_methods[i++].name) != NULL) {
if (conf.light_mode == 1 && i > 0
&& !ST_IN_LIGHT_MODE(sort_methods[i - 1].num))
continue;
if (strncmp(name, text, len) == 0)
return strdup(name);
}
Expand Down Expand Up @@ -4269,8 +4273,10 @@ my_rl_completion(const char *text, const int start, const int end)
/* Sort number */
if (*lb == 's' && (strncmp(lb, "st ", 3) == 0
|| strncmp(lb, "sort ", 5) == 0)
&& is_number(text) && n >= 0 && n <= SORT_TYPES)
&& is_number(text) && n >= 0 && n <= SORT_TYPES) {
rl_attempted_completion_over = 1;
return complete_sort_num(text, words_n);
}
}

/* ### DESELECT COMPLETION ### */
Expand Down
57 changes: 35 additions & 22 deletions src/sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@
#include "listing.h"
#include "messages.h" /* SORT_USAGE */

/* Do we have the sort method S in light mode? */
#define ST_IN_LIGHT_MODE(s) ((s) == SNAME || (s) == SVER || (s) == SINO \
|| (s) == SEXT || (s) == SNONE)

int
skip_nonexec(const struct dirent *ent)
{
Expand Down Expand Up @@ -355,26 +351,30 @@ alphasort_insensitive(const struct dirent **a, const struct dirent **b)
// return (ret - (ret * 2));
}

static char *
num_to_sort_name(const int n)
{
switch (n) {
case SNONE: return "none";
case SNAME: return "name";
case STSIZE: return "size";
case SATIME: return "atime";
case SBTIME: return "btime";
case SCTIME: return "ctime";
case SMTIME: return "mtime";
case SVER: return "version";
case SEXT: return "extension";
case SINO: return "inode";
case SOWN: return "owner";
case SGRP: return "group";
default: return "unknown";
}
}

void
print_sort_method(void)
{
char *name = (char *)NULL;

switch (conf.sort) {
case SNONE: name = "none"; break;
case SNAME: name = "name"; break;
case STSIZE: name = "size"; break;
case SATIME: name = "atime"; break;
case SBTIME: name = "btime"; break;
case SCTIME: name = "ctime"; break;
case SMTIME: name = "mtime"; break;
case SVER: name = "version"; break;
case SEXT: name = "extension"; break;
case SINO: name = "inode"; break;
case SOWN: name = "owner"; break;
case SGRP: name = "group"; break;
default: name = "unknown"; break;
}
char *name = num_to_sort_name(conf.sort);

printf("%s%s%s%s", BOLD, name, NC,
(conf.sort_reverse == 1) ? " [rev]" : "");
Expand Down Expand Up @@ -420,6 +420,13 @@ set_sort_by_name(char **arg)
for (i = 0; i <= SORT_TYPES; i++) {
if (*(*arg) == *sort_methods[i].name
&& strcmp(*arg, sort_methods[i].name) == 0) {
if (conf.light_mode == 1
&& !ST_IN_LIGHT_MODE(sort_methods[i].num)) {
fprintf(stderr, _("st: '%s': Not available in light mode\n"),
sort_methods[i].name);
return FUNC_FAILURE;
}

*arg = xnrealloc(*arg, 32, sizeof(char));
snprintf(*arg, 32, "%d", sort_methods[i].num);
return FUNC_SUCCESS;
Expand Down Expand Up @@ -454,9 +461,15 @@ sort_function(char **arg)
/* Argument is a number */
const int n = atoi(arg[1]);

if (conf.light_mode == 1 && !ST_IN_LIGHT_MODE(n)) {
fprintf(stderr, _("st: %d (%s): Not available in light mode\n"),
n, num_to_sort_name(n));
return FUNC_FAILURE;
}

#ifndef ST_BTIME
if (n == SBTIME) {
puts(_("st: Birth time is not available on this platform"));
fputs(_("st: Birth time is not available on this platform"), stderr);
return FUNC_FAILURE;
}
#endif /* !ST_BTIME */
Expand Down
3 changes: 2 additions & 1 deletion src/suggestions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1635,7 +1635,8 @@ check_sort_methods(const char *str, const size_t len)
}

int a = atoi(str);
if (a < 0 || a > SORT_TYPES) {
if (a < 0 || a > SORT_TYPES
|| (conf.light_mode == 1 && !ST_IN_LIGHT_MODE(a))) {
if (suggestion.printed)
clear_suggestion(CS_FREEBUF);
return NO_MATCH;
Expand Down

0 comments on commit 24cf8c2

Please sign in to comment.