<commit_before>﻿using LibTextPet.General;
using LibTextPet.Msg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TextPet.Commands {
	internal class TrimCommand : CliCommand {
		public override string Name => "trim";
		public override string RunString => "Trimming scripts...";
		
		private const string maxArg = "max";
		private const string amountArg = "amount";
		private const string preserveTextArg = "preserve-text";

		public TrimCommand(CommandLineInterface cli, TextPetCore core)
			: base(cli, core, new string[0], new OptionalArgument[] {
				new OptionalArgument(maxArg, 'm', new string[] {
					amountArg,
				}),
				new OptionalArgument(preserveTextArg, 'p'),
			}) { }

		protected override void RunImplementation() {
			string maxStr = GetOptionalValues(maxArg)?[0];
			int max = maxStr != null ? NumberParser.ParseInt32(maxStr) : int.MaxValue;
			bool preserveText = GetOptionalValues(preserveTextArg) != null;

			// Cycle through every script in every text archive.
			foreach (TextArchive ta in this.Core.TextArchives) {
				foreach (Script script in ta) {
					// Find the first script-ending command.
					int endPos = 0;
					for (int i = 0; i < script.Count; i++) {
						if (script[i].EndsScript) {
							endPos = i;
							break;
						}
					}

					// Remove commands from the end of the script until the script-ending command or maximum is reached.
					int trimmed = 0;
					while (script.Count > endPos && (maxStr == null || trimmed < max)) {
						// If the preverse text option is enabled, cancel if this would trim non-whitespace text.
						if (preserveText) {
							TextElement textElem = script[script.Count - 1] as TextElement;
							if (textElem != null && !textElem.Text.All(c => Char.IsWhiteSpace(c))) {
								break;
							}
						}

						script.RemoveAt(script.Count - 1);
						trimmed++;
					}
				}
			}
		}
	}<commit_msg>Add --preserve-commands option to trim command.<commit_after>﻿using LibTextPet.General;
using LibTextPet.Msg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TextPet.Commands {
	internal class TrimCommand : CliCommand {
		public override string Name => "trim";
		public override string RunString => "Trimming scripts...";
		
		private const string maxArg = "max";
		private const string amountArg = "amount";
		private const string preserveTextArg = "preserve-text";
		private const string preserveCommandsArg = "preserve-commands";

		public TrimCommand(CommandLineInterface cli, TextPetCore core)
			: base(cli, core, new string[0], new OptionalArgument[] {
				new OptionalArgument(maxArg, 'm', new string[] {
					amountArg,
				}),
				new OptionalArgument(preserveTextArg, 't'),
				new OptionalArgument(preserveCommandsArg, 'c'),
			}) { }

		protected override void RunImplementation() {
			string maxStr = GetOptionalValues(maxArg)?[0];
			int max = maxStr != null ? NumberParser.ParseInt32(maxStr) : int.MaxValue;
			bool preserveText = GetOptionalValues(preserveTextArg) != null;
			bool preserveCommands = GetOptionalValues(preserveCommandsArg) != null;

			// Cycle through every script in every text archive.
			foreach (TextArchive ta in this.Core.TextArchives) {
				foreach (Script script in ta) {
					// Find the first script-ending command.
					int endPos = 0;
					for (int i = 0; i < script.Count; i++) {
						if (script[i].EndsScript) {
							endPos = i;
							break;
						}
					}

					// Remove elements from the end of the script until the script-ending element or maximum is reached.
					int trimmed = 0;
					while (script.Count > endPos && (maxStr == null || trimmed < max)) {
						// If the preverse text option is enabled, cancel if this would trim non-whitespace text.
						if (preserveText) {
							TextElement textElem = script[script.Count - 1] as TextElement;
							if (textElem != null && !textElem.Text.All(c => Char.IsWhiteSpace(c))) {
								break;
							}
						}
						// If the preserve commands option is enabled, cancel if this would trim commands.
						if (preserveCommands) {
							Command cmdElem = script[script.Count - 1] as Command;
							if (cmdElem != null) {
								break;
							}
						}

						script.RemoveAt(script.Count - 1);
						trimmed++;
					}
				}
			}
		}
	}
{'fraction_non_alphanumeric': 0.10843373493975904, 'fraction_numerical': 0.004402224281742354, 'mean_word_length': 3.02705223880597, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '6589168', 'n_tokens_mistral': 1618, 'n_tokens_neox': 1233, 'n_words': 394}