<commit_before>/**
 * @file
 * @brief Internal representation of command line entity.
 *
 * @date 08.02.09
 * @author <NAME>
 */

#include "cmdline.h"
#include <util/math.h>
#include <string.h>
#include <util/array.h>

CMDLINE * cmdline_init(CMDLINE *this) {
	CMDLINE_HISTORY *history;
	if (this == NULL) {
		return NULL;
	}

	history = this->history;
	history->array[0][0] = '\0';
	history->array[1][0] = '\0';
	history->index = 0;
	this->string[0] = '\0';
	this->length = 0;
	this->cursor = 0;
	this->history_cursor = 0;
	this->is_insert_mode = 0;
	return this;
}

static bool cmdline_history_move_to(CMDLINE *cmdline, int to) {
	int new_pos;
	CMDLINE_HISTORY * history = cmdline->history;
	if (to < 0) {
		to = to + (-to / CMDLINE_HISTORY_SIZE + 1) * CMDLINE_HISTORY_SIZE;
	}
	new_pos = to % CMDLINE_HISTORY_SIZE;
	if (cmdline->history_cursor == new_pos) {
		return false;
	}
	if (history->array[new_pos][0] == '\0' && new_pos != history->index) {
		return false;
	}

	if (cmdline->history_cursor == history->index) {
		strncpy(history->array[history->index], cmdline->string,
				ARRAY_SIZE(history->array[history->index]));
	}

	cmdline->history_cursor = new_pos;
	strcpy(cmdline->string, history->array[new_pos]);
	cmdline->length = strlen(cmdline->string);
	cmdline->cursor = cmdline->length;
	return true;
}

static bool cmdline_history_move_by(CMDLINE *cmdline, int by) {
	return cmdline_history_move_to(cmdline, cmdline->history_cursor + by);
}

bool cmdline_history_backward(CMDLINE *cmdline) {
	return cmdline_history_move_by(cmdline, -1);
}

bool cmdline_history_forward(CMDLINE *cmdline) {
	return cmdline_history_move_by(cmdline, 1);
}

bool cmdline_history_new_entry(CMDLINE *cmdline) {
	CMDLINE_HISTORY * history = cmdline->history;
	if (cmdline->string[0] == '\0') {
		return false;
	}

	if (0 == strcmp(cmdline->string, history->array[(history->index
			+ CMDLINE_HISTORY_SIZE - 1) % CMDLINE_HISTORY_SIZE])) {
		cmdline->length = 0;
		cmdline->cursor = 0;
			cmdline->string[0] = '\0';
		return false;
	}

	strcpy(history->array[history->index], cmdline->string);

	history->index = (history->index + 1) % CMDLINE_HISTORY_SIZE;
	history->array[history->index][0] = '\0';
	history->array[(history->index + 1) % CMDLINE_HISTORY_SIZE][0] = '\0';

	cmdline->history_cursor = history->index;
	cmdline->length = 0;
	cmdline->cursor = 0;
	cmdline->string[0] = '\0';

	return true;
}

bool cmdline_cursor_move_to(CMDLINE *cmdline, int to) {
	int old_cursor = cmdline->cursor;
	to = min(cmdline->length, to);
	to = max(0, to);
	cmdline->cursor = to;
	return cmdline->cursor != old_cursor;
}

bool cmdline_cursor_move_by(CMDLINE *cmdline, int by) {
	return cmdline_cursor_move_to(cmdline, cmdline->cursor + by);
}

bool cmdline_cursor_right(CMDLINE *cmdline) {<commit_msg>cmdline: stone aged bug with cmdline fixed<commit_after>/**
 * @file
 * @brief Internal representation of command line entity.
 *
 * @date 08.02.09
 * @author <NAME>
 */

#include "cmdline.h"
#include <util/math.h>
#include <string.h>
#include <util/array.h>

CMDLINE * cmdline_init(CMDLINE *this) {
	CMDLINE_HISTORY *history;
	if (this == NULL) {
		return NULL;
	}

	history = this->history;
	history->array[0][0] = '\0';
	history->array[1][0] = '\0';
	history->index = 0;
	this->string[0] = '\0';
	this->length = 0;
	this->cursor = 0;
	this->history_cursor = 0;
	this->is_insert_mode = 0;
	return this;
}

static bool cmdline_history_move_to(CMDLINE *cmdline, int to) {
	int new_pos;
	CMDLINE_HISTORY * history = cmdline->history;
	if (to < 0) {
		to = to + (-to / CMDLINE_HISTORY_SIZE + 1) * CMDLINE_HISTORY_SIZE;
	}
	new_pos = to % CMDLINE_HISTORY_SIZE;
	if (cmdline->history_cursor == new_pos) {
		return false;
	}
	if (history->array[new_pos][0] == '\0' && new_pos != history->index) {
		return false;
	}

	if (cmdline->history_cursor == history->index) {
		strncpy(history->array[history->index], cmdline->string,
				ARRAY_SIZE(history->array[history->index]));
	}

	cmdline->history_cursor = new_pos;
	strcpy(cmdline->string, history->array[new_pos]);
	cmdline->length = strlen(cmdline->string);
	cmdline->cursor = cmdline->length;
	return true;
}

static bool cmdline_history_move_by(CMDLINE *cmdline, int by) {
	return cmdline_history_move_to(cmdline, cmdline->history_cursor + by);
}

bool cmdline_history_backward(CMDLINE *cmdline) {
	return cmdline_history_move_by(cmdline, -1);
}

bool cmdline_history_forward(CMDLINE *cmdline) {
	return cmdline_history_move_by(cmdline, 1);
}

bool cmdline_history_new_entry(CMDLINE *cmdline) {
	bool ret = true;
	CMDLINE_HISTORY * history = cmdline->history;
	if (cmdline->string[0] == '\0') {
		return false;
	}

	if (0 == strcmp(cmdline->string, history->array[(history->index
			+ CMDLINE_HISTORY_SIZE - 1) % CMDLINE_HISTORY_SIZE])) {
		ret = false;
	} else {
		strcpy(history->array[history->index], cmdline->string);

		history->index = (history->index + 1) % CMDLINE_HISTORY_SIZE;
		history->array[history->index][0] = '\0';
		history->array[(history->index + 1) % CMDLINE_HISTORY_SIZE][0] = '\0';
	}

	cmdline->history_cursor = history->index;
	cmdline->length = 0;
	cmdline->cursor = 0;
	cmdline->string[0] = '\0';

	return ret;
}

bool cmdline_cursor_move_to(CMDLINE *cmdline, int to) {
	int old_cursor = cmdline->cursor;
	to = min(cmdline->length, to);
	to = max(0, to);
	cmdline->cursor = to;
	return cmdline->cursor != old_cursor;
}

bool cmdline_cursor_move_by(CMDLINE *cmdline, int by) {
	return cmdline_cursor_move_to(cmdline, cmdline->cursor + by);
}

bool cmdline_cursor_right(CMDLINE *cmdline) {
{'fraction_non_alphanumeric': 0.16071428571428573, 'fraction_numerical': 0.015151515151515152, 'mean_word_length': 4.788100208768268, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 118, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4870430', 'n_tokens_mistral': 2197, 'n_tokens_neox': 2139, 'n_words': 410}