Fixing cursor navigation in selection (UP/DOWN arrows and prompt 2

selection switches) + reminding C conventions.
This commit is contained in:
Martin Dvorak 2013-12-13 21:19:25 +01:00
parent 63479cc718
commit 87d4d5844c
6 changed files with 79 additions and 75 deletions

View file

@ -108,34 +108,34 @@ void alloc_selection(int size) {
}
}
int make_selection(char* prefix, char **historyFileItems, int historyFileItemsCount, int maxSelectionCount) {
int make_selection(char *prefix, HistoryItems *history, int maxSelectionCount) {
alloc_selection(sizeof(char*) * maxSelectionCount); // TODO realloc
int i, selectionCount=0;
HashSet set;
hashset_init(&set);
for(i=0; i<historyFileItemsCount && selectionCount<maxSelectionCount; i++) {
if(historyFileItems[i]!=NULL && !hashset_contains(&set, historyFileItems[i])) {
for(i=0; i<history->count && selectionCount<maxSelectionCount; i++) {
if(history->items[i]!=NULL && !hashset_contains(&set, history->items[i])) {
if(prefix==NULL) {
selection[selectionCount++]=historyFileItems[i];
hashset_add(&set, historyFileItems[i]);
selection[selectionCount++]=history->items[i];
hashset_add(&set, history->items[i]);
} else {
if(historyFileItems[i]==strstr(historyFileItems[i], prefix)) {
selection[selectionCount++]=historyFileItems[i];
hashset_add(&set, historyFileItems[i]);
if(history->items[i]==strstr(history->items[i], prefix)) {
selection[selectionCount++]=history->items[i];
hashset_add(&set, history->items[i]);
}
}
}
}
if(prefix!=NULL && selectionCount<maxSelectionCount) {
for(i=0; i<historyFileItemsCount && selectionCount<maxSelectionCount; i++) {
if(!hashset_contains(&set, historyFileItems[i])) {
char* substring = strstr(historyFileItems[i], prefix);
if (substring != NULL && substring!=historyFileItems[i]) {
selection[selectionCount++]=historyFileItems[i];
hashset_add(&set, historyFileItems[i]);
for(i=0; i<history->count && selectionCount<maxSelectionCount; i++) {
if(!hashset_contains(&set, history->items[i])) {
char *substring = strstr(history->items[i], prefix);
if (substring != NULL && substring!=history->items[i]) {
selection[selectionCount++]=history->items[i];
hashset_add(&set, history->items[i]);
}
}
}
@ -145,9 +145,9 @@ int make_selection(char* prefix, char **historyFileItems, int historyFileItemsCo
return selectionCount;
}
char* print_selection(WINDOW *win, int maxHistoryItems, char *prefix, int historyFileItemsCount, char** historyFileItems) {
char* result="";
int selectionCount=make_selection(prefix, historyFileItems, historyFileItemsCount, maxHistoryItems);
char *print_selection(WINDOW *win, int maxHistoryItems, char *prefix, HistoryItems *history) {
char *result="";
int selectionCount=make_selection(prefix, history, maxHistoryItems);
if (selectionCount > 0) {
result = selection[0];
}
@ -211,7 +211,7 @@ void color_attr_off(int c) {
}
}
char* selection_loop(char **historyFileItems, int historyFileItemsCount) {
char *selection_loop(HistoryItems *history) {
initscr();
color_start();
@ -219,7 +219,7 @@ char* selection_loop(char **historyFileItems, int historyFileItemsCount) {
color_attr_on(COLOR_PAIR(1));
print_history_label(stdscr);
print_help_label(stdscr);
print_selection(stdscr, get_max_history_items(stdscr), NULL, historyFileItemsCount, historyFileItems);
print_selection(stdscr, get_max_history_items(stdscr), NULL, history);
int basex = print_prompt(stdscr);
int x = basex;
color_attr_off(COLOR_PAIR(1));
@ -230,7 +230,7 @@ char* selection_loop(char **historyFileItems, int historyFileItemsCount) {
int y = 1, c, maxHistoryItems, cursorX, cursorY;
bool done = FALSE;
char prefix[500]="";
char* result="";
char *result="";
while (!done) {
maxHistoryItems=get_max_history_items(stdscr);
@ -259,31 +259,35 @@ char* selection_loop(char **historyFileItems, int historyFileItemsCount) {
}
if(strlen(prefix)>0) {
make_selection(prefix, historyFileItems, historyFileItemsCount, maxHistoryItems);
make_selection(prefix, history, maxHistoryItems);
} else {
make_selection(NULL, historyFileItems, historyFileItemsCount, maxHistoryItems);
make_selection(NULL, history, maxHistoryItems);
}
result = print_selection(stdscr, maxHistoryItems, prefix, historyFileItemsCount, historyFileItems);
result = print_selection(stdscr, maxHistoryItems, prefix, history);
move(y, basex+strlen(prefix));
break;
case KEY_UP:
case 65:
if(selectionCursorPosition>SELECTION_CURSOR_IN_PROMPT) {
previousSelectionCursorPosition=selectionCursorPosition;
previousSelectionCursorPosition=selectionCursorPosition;
if(selectionCursorPosition>0) {
selectionCursorPosition--;
} else {
previousSelectionCursorPosition=SELECTION_CURSOR_IN_PROMPT;
selectionCursorPosition=selectionSize-1;
}
highlight_selection(selectionCursorPosition, previousSelectionCursorPosition);
break;
case KEY_DOWN:
case 66:
previousSelectionCursorPosition=selectionCursorPosition;
if((selectionCursorPosition+1)<selectionSize) {
selectionCursorPosition++;
if(selectionCursorPosition==SELECTION_CURSOR_IN_PROMPT) {
selectionCursorPosition=previousSelectionCursorPosition=0;
} else {
selectionCursorPosition=0;
previousSelectionCursorPosition=selectionCursorPosition;
if((selectionCursorPosition+1)<selectionSize) {
selectionCursorPosition++;
} else {
selectionCursorPosition=0;
}
}
highlight_selection(selectionCursorPosition, previousSelectionCursorPosition);
break;
@ -299,6 +303,8 @@ char* selection_loop(char **historyFileItems, int historyFileItemsCount) {
LOGCURSOR(Y_OFFSET_HELP);
if(c!=27) {
selectionCursorPosition=SELECTION_CURSOR_IN_PROMPT;
strcat(prefix, (char*)(&c));
wattron(stdscr,A_BOLD);
mvprintw(y, basex, "%s", prefix);
@ -307,7 +313,7 @@ char* selection_loop(char **historyFileItems, int historyFileItemsCount) {
wattroff(stdscr,A_BOLD);
clrtoeol();
result = print_selection(stdscr, maxHistoryItems, prefix, historyFileItemsCount, historyFileItems);
result = print_selection(stdscr, maxHistoryItems, prefix, history);
move(cursorY, cursorX);
refresh();
}
@ -320,10 +326,11 @@ char* selection_loop(char **historyFileItems, int historyFileItemsCount) {
}
void hstr() {
char** items=get_history_items();
int itemsCount=get_history_items_size();
char* command = selection_loop(items, itemsCount);
HistoryItems *historyFileItems=get_history_items();
HistoryItems *history=prioritize_history(historyFileItems);
char *command = selection_loop(history);
fill_terminal_input(command);
free_prioritized_history();
free_history_items();
}

View file

@ -1,9 +1,9 @@
#include "include/hstr_history.h"
static int historyItemsCount;
static char** historyItems;
static HistoryItems *history;
static HistoryItems *prioritizedHistory;
char* get_history_file() {
char *get_history_file() {
char *home = getenv(ENV_VAR_HOME);
char *fileName = (char*) malloc(
strlen(home) + 1 + strlen(FILE_HISTORY) + 1);
@ -13,6 +13,14 @@ char* get_history_file() {
return fileName;
}
HistoryItems *prioritize_history(HistoryItems *historyFileItems) {
return historyFileItems;
}
void free_prioritized_history() {
//free(prioritizedHistory->items);
//free(prioritizedHistory);
}
#ifdef GET_HISTORY_FROM_FILE
@ -20,7 +28,7 @@ char* get_history_file() {
static char *historyAsString;
char *load_history_file() {
char* fileName = get_history_file();
char *fileName = get_history_file();
if(access(fileName, F_OK) != -1) {
char *file_contents;
long input_file_size;
@ -71,7 +79,7 @@ char **get_tokenized_history(char *history, int lines) {
return tokens;
}
char** get_history_items() {
char **get_history_items() {
historyAsString = load_history_file(FILE_HISTORY);
historyItemsCount = count_history_lines(historyAsString);
historyItems = get_tokenized_history(historyAsString, historyItemsCount);
@ -94,17 +102,19 @@ void free_history_items() {
void flush_history() {
const char* filename = "history";
const char *filename = "history";
char *const args[1] = {"-a"};
execvp(filename, args);
}
char** get_history_items() {
HistoryItems *get_history_items() {
history=(HistoryItems *)malloc(sizeof(HistoryItems));
flush_history();
using_history();
char *historyFile = getenv(ENV_VAR_HISTFILE);
if(historyFile==NULL || strlen(historyFile)==0) {
if(!historyFile || strlen(historyFile)==0) {
historyFile=get_history_file();
}
@ -115,13 +125,13 @@ char** get_history_items() {
HISTORY_STATE *historyState=history_get_history_state();
if(historyState->length > 0) {
historyItemsCount=historyState->length;
history->count=historyState->length;
history->items=(char**)malloc(history->count * sizeof(char*));
historyItems=(char**)malloc(historyItemsCount*sizeof(char*));
HIST_ENTRY **historyList=history_list();
int i;
char *entry, *item;
for(i=0; i<historyItemsCount; i++) {
for(i=0; i<history->count; i++) {
entry=historyList[i]->line;
if(entry!=NULL) {
item=malloc(strlen(entry)+1);
@ -130,22 +140,19 @@ char** get_history_items() {
item=malloc(2);
strcpy(item, " ");
}
historyItems[i]=item;
history->items[i]=item;
}
} else {
historyItemsCount=0;
historyItems=NULL;
history->count=0;
history->items=NULL;
}
return historyItems;
}
int get_history_items_size() {
return historyItemsCount;
return history;
}
void free_history_items() {
free(historyItems);
free(history->items);
free(history);
}

View file

@ -10,7 +10,7 @@ void tiocsti() {
}
}
void fill_terminal_input(char* cmd){
void fill_terminal_input(char *cmd){
size_t size = strlen(cmd);
int i;
char *c;
@ -24,7 +24,7 @@ void fill_terminal_input(char* cmd){
void reverse_char_pointer_array(char **array, int length) {
int i;
char * temp;
char *temp;
for (i=0; i<length/2; i++) {
temp = array[i];
array[i] = array[length-i-1];

View file

@ -5,10 +5,6 @@
#include <stdio.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
#define TABLE_SIZE 10007
struct HashNode {
@ -30,9 +26,4 @@ int hashset_remove( HashSet * hs, const char *key );
int hashset_size( const HashSet * hs );
void hashset_print( const HashSet * hs );
#ifdef __cplusplus
}
#endif
#endif

View file

@ -18,8 +18,15 @@
#define FILE_HISTORY ".bash_history"
char** get_history_items();
int get_history_items_size();
typedef struct {
char **items;
int count;
} HistoryItems;
HistoryItems *get_history_items();
void free_history_items();
HistoryItems *prioritize_history(HistoryItems *historyFileItems);
void free_prioritized_history();
#endif

View file

@ -6,16 +6,8 @@
#include <string.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
void tiocsti();
void fill_terminal_input(char* cmd);
void reverse_char_pointer_array(char **array, int length);
#ifdef __cplusplus
}
#endif
#endif