mirror of
https://github.com/davatorium/rofi.git
synced 2025-03-10 17:06:37 -04:00
Sort favorites by # of runs
This commit is contained in:
parent
04791f8fc4
commit
702ff68eb4
1 changed files with 48 additions and 17 deletions
|
@ -31,6 +31,7 @@
|
||||||
#include <X11/X.h>
|
#include <X11/X.h>
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
@ -44,7 +45,7 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define RUN_CACHE_FILE "rofi.runcache"
|
#define RUN_CACHE_FILE "rofi-2.runcache"
|
||||||
|
|
||||||
static inline int execsh( const char *cmd ,int run_in_term )
|
static inline int execsh( const char *cmd ,int run_in_term )
|
||||||
{
|
{
|
||||||
|
@ -54,6 +55,17 @@ static inline int execsh( const char *cmd ,int run_in_term )
|
||||||
|
|
||||||
return execlp( "/bin/sh", "sh", "-c", cmd, NULL );
|
return execlp( "/bin/sh", "sh", "-c", cmd, NULL );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef struct _element{
|
||||||
|
long int index;
|
||||||
|
char name[1024];
|
||||||
|
}element;
|
||||||
|
static int element_sort_func(const void *ea,const void *eb)
|
||||||
|
{
|
||||||
|
element *a = *(element **)ea;
|
||||||
|
element *b = *(element **)eb;
|
||||||
|
return b->index - a->index;
|
||||||
|
}
|
||||||
// execute sub-process
|
// execute sub-process
|
||||||
static pid_t exec_cmd( const char *cmd, int run_in_term )
|
static pid_t exec_cmd( const char *cmd, int run_in_term )
|
||||||
{
|
{
|
||||||
|
@ -70,7 +82,7 @@ static pid_t exec_cmd( const char *cmd, int run_in_term )
|
||||||
|
|
||||||
int curr = -1;
|
int curr = -1;
|
||||||
unsigned int index = 0;
|
unsigned int index = 0;
|
||||||
char **retv = NULL;
|
element **retv = NULL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This happens in non-critical time (After launching app)
|
* This happens in non-critical time (After launching app)
|
||||||
|
@ -83,36 +95,47 @@ static pid_t exec_cmd( const char *cmd, int run_in_term )
|
||||||
if ( fd != NULL ) {
|
if ( fd != NULL ) {
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
while ( fgets( buffer,1024,fd ) != NULL ) {
|
while ( fgets( buffer,1024,fd ) != NULL ) {
|
||||||
retv = reallocate( retv, ( index+2 )*sizeof( char* ) );
|
retv = reallocate( retv, ( index+2 )*sizeof( element* ) );
|
||||||
|
retv[index] = allocate(sizeof(element));
|
||||||
buffer[strlen( buffer )-1] = '\0';
|
buffer[strlen( buffer )-1] = '\0';
|
||||||
retv[index] = strdup( buffer );
|
char * start = NULL;
|
||||||
|
retv[index]->index = strtol(buffer, &start, 10);
|
||||||
|
snprintf(retv[index]->name, 1024, "%s", start+1);
|
||||||
retv[index+1] = NULL;
|
retv[index+1] = NULL;
|
||||||
|
|
||||||
if ( strcasecmp( retv[index], cmd ) == 0 ) {
|
if ( strcasecmp( retv[index]->name, cmd ) == 0 ) {
|
||||||
curr = index;
|
curr = index;
|
||||||
}
|
}
|
||||||
|
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose( fd );
|
fclose( fd );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(curr < 0) {
|
||||||
|
retv = reallocate( retv, ( index+2 )*sizeof( element* ) );
|
||||||
|
retv[index] = allocate(sizeof(element));
|
||||||
|
retv[index]->index = 1;
|
||||||
|
snprintf(retv[index]->name, 1024, "%s\n", cmd);
|
||||||
|
index++;
|
||||||
|
}else {
|
||||||
|
retv[curr]->index++;
|
||||||
|
}
|
||||||
|
// Sort the list.
|
||||||
|
qsort(retv, index, sizeof(element*), element_sort_func);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write out the last 25 results again.
|
* Write out the last 25 results again.
|
||||||
*/
|
*/
|
||||||
fd = fopen ( path, "w" );
|
fd = fopen ( path, "w" );
|
||||||
|
|
||||||
if ( fd ) {
|
if ( fd ) {
|
||||||
// Last one goes on top!
|
|
||||||
fputs( cmd, fd );
|
|
||||||
fputc( '\n', fd );
|
|
||||||
|
|
||||||
for ( int i = 0; i < ( int )index && i < 20; i++ ) {
|
for ( int i = 0; i < ( int )index && i < 20; i++ ) {
|
||||||
if ( i != curr ) {
|
fprintf(fd, "%ld %s\n",
|
||||||
fputs( retv[i], fd );
|
retv[i]->index,
|
||||||
fputc( '\n', fd );
|
retv[i]->name
|
||||||
}
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose( fd );
|
fclose( fd );
|
||||||
|
@ -146,9 +169,13 @@ static void delete_entry( const char *cmd )
|
||||||
if ( fd != NULL ) {
|
if ( fd != NULL ) {
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
while ( fgets( buffer,1024,fd ) != NULL ) {
|
while ( fgets( buffer,1024,fd ) != NULL ) {
|
||||||
retv = reallocate( retv, ( index+2 )*sizeof( char* ) );
|
|
||||||
buffer[strlen( buffer )-1] = '\0';
|
buffer[strlen( buffer )-1] = '\0';
|
||||||
retv[index] = strdup( buffer );
|
char *start = NULL;
|
||||||
|
// Don't use result.
|
||||||
|
strtol(buffer, &start, 10);
|
||||||
|
if(start == NULL) continue;
|
||||||
|
retv = reallocate( retv, ( index+2 )*sizeof( char* ) );
|
||||||
|
retv[index] = strdup( start );
|
||||||
retv[index+1] = NULL;
|
retv[index+1] = NULL;
|
||||||
|
|
||||||
if ( strcasecmp( retv[index], cmd ) == 0 ) {
|
if ( strcasecmp( retv[index], cmd ) == 0 ) {
|
||||||
|
@ -213,9 +240,13 @@ static char ** get_apps ( )
|
||||||
if ( fd != NULL ) {
|
if ( fd != NULL ) {
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
while ( fgets( buffer,1024,fd ) != NULL ) {
|
while ( fgets( buffer,1024,fd ) != NULL ) {
|
||||||
retv = reallocate( retv, ( index+2 )*sizeof( char* ) );
|
|
||||||
buffer[strlen( buffer )-1] = '\0';
|
buffer[strlen( buffer )-1] = '\0';
|
||||||
retv[index] = strdup( buffer );
|
char *start = NULL;
|
||||||
|
// Don't use result.
|
||||||
|
strtol(buffer, &start, 10);
|
||||||
|
if(start == NULL) continue;
|
||||||
|
retv = reallocate( retv, ( index+2 )*sizeof( char* ) );
|
||||||
|
retv[index] = strdup( start+1 );
|
||||||
retv[index+1] = NULL;
|
retv[index+1] = NULL;
|
||||||
index++;
|
index++;
|
||||||
num_favorites++;
|
num_favorites++;
|
||||||
|
|
Loading…
Add table
Reference in a new issue