Merge pull request #3 from LuisM000/master
Character and Sentences options added
This commit is contained in:
Коммит
a97a8c6430
|
@ -23,5 +23,27 @@ namespace VS4Mac.TextGenerator.Tests
|
|||
|
||||
Assert.AreEqual(2, words.Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenerateCharactersTest()
|
||||
{
|
||||
var textGeneratorService = new TextGeneratorService();
|
||||
var words = textGeneratorService.GenerateCharacters(5);
|
||||
|
||||
Assert.AreEqual(1, words.Length);
|
||||
Assert.AreEqual(5, words[0].Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FirstGenerateCharactersAreNotNewLineOrReturnTest()
|
||||
{
|
||||
var textGeneratorService = new TextGeneratorService();
|
||||
var words = textGeneratorService.GenerateCharacters(3);
|
||||
var characters = words[0];
|
||||
|
||||
Assert.That(characters[0], Is.Not.EqualTo('\n').And.Not.EqualTo('\r'));
|
||||
Assert.That(characters[1], Is.Not.EqualTo('\n').And.Not.EqualTo('\r'));
|
||||
Assert.That(characters[2], Is.Not.EqualTo('\n').And.Not.EqualTo('\r'));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
using NLipsum.Core;
|
||||
using System.Linq;
|
||||
using NLipsum.Core;
|
||||
|
||||
namespace VS4Mac.TextGenerator.Services
|
||||
{
|
||||
|
@ -20,8 +21,10 @@ namespace VS4Mac.TextGenerator.Services
|
|||
|
||||
public string[] GenerateCharacters(int numberOfChars)
|
||||
{
|
||||
string[] words = _generator.GenerateCharacters(numberOfChars);
|
||||
|
||||
//First three characters are \n or \r
|
||||
var words = _generator.GenerateCharacters(numberOfChars + 3);
|
||||
words[0] = words[0].Remove(0, 3);
|
||||
|
||||
return words;
|
||||
}
|
||||
|
||||
|
|
|
@ -15,6 +15,10 @@ namespace VS4Mac.TextGenerator.Views
|
|||
TextEntry _numberEntry;
|
||||
HBox _buttonBox;
|
||||
Button _generateButton;
|
||||
|
||||
HBox _optionsBox;
|
||||
Label _optionsLabel;
|
||||
ComboBox _generatorOptionsComboBox;
|
||||
|
||||
TextGeneratorService _textGeneratorService;
|
||||
|
||||
|
@ -33,8 +37,8 @@ namespace VS4Mac.TextGenerator.Views
|
|||
|
||||
_mainBox = new VBox
|
||||
{
|
||||
HeightRequest = 50,
|
||||
WidthRequest = 200
|
||||
HeightRequest = 100,
|
||||
WidthRequest = 235
|
||||
};
|
||||
|
||||
_numberBox = new HBox();
|
||||
|
@ -51,17 +55,31 @@ namespace VS4Mac.TextGenerator.Views
|
|||
BackgroundColor = Styles.BaseSelectionBackgroundColor,
|
||||
LabelColor = Styles.BaseSelectionTextColor,
|
||||
WidthRequest = 48
|
||||
};
|
||||
};
|
||||
|
||||
_optionsBox = new HBox();
|
||||
_optionsLabel = new Label("Generator option:");
|
||||
_generatorOptionsComboBox = new ComboBox();
|
||||
_generatorOptionsComboBox.Items.Add("Characters");
|
||||
_generatorOptionsComboBox.Items.Add("Words");
|
||||
_generatorOptionsComboBox.Items.Add("Sentences");
|
||||
_generatorOptionsComboBox.SelectedIndex = 1;
|
||||
}
|
||||
|
||||
void BuildGui()
|
||||
{
|
||||
{
|
||||
_optionsBox.PackStart(_optionsLabel);
|
||||
_optionsBox.PackEnd(_generatorOptionsComboBox);
|
||||
|
||||
_numberBox.PackStart(_numberLabel, true);
|
||||
_numberBox.PackEnd(_numberEntry, false);
|
||||
_buttonBox.PackEnd(_generateButton);
|
||||
|
||||
_mainBox.PackStart(_numberBox);
|
||||
_mainBox.PackEnd(_buttonBox);
|
||||
|
||||
_mainBox.PackStart(_optionsBox);
|
||||
_mainBox.PackStart(_numberBox);
|
||||
_mainBox.PackEnd(_buttonBox);
|
||||
|
||||
_numberEntry.SetFocus();
|
||||
|
||||
Content = _mainBox;
|
||||
Resizable = false;
|
||||
|
@ -70,9 +88,15 @@ namespace VS4Mac.TextGenerator.Views
|
|||
void AttachEvents()
|
||||
{
|
||||
_numberEntry.Changed += OnNumberEntryChanged;
|
||||
_generateButton.Clicked += OnGenerateClicked;
|
||||
_generateButton.Clicked += OnGenerateClicked;
|
||||
_generatorOptionsComboBox.SelectionChanged += OnGeneratorOptionsChanged;
|
||||
}
|
||||
|
||||
void OnGeneratorOptionsChanged(object sender, EventArgs e)
|
||||
{
|
||||
_numberLabel.Text = $"Introduce number of {_generatorOptionsComboBox.SelectedItem.ToString().ToLower()}:";
|
||||
}
|
||||
|
||||
void OnNumberEntryChanged(object sender, EventArgs e)
|
||||
{
|
||||
if (System.Text.RegularExpressions.Regex.IsMatch(_numberEntry.Text, "[^0-9]"))
|
||||
|
@ -86,8 +110,22 @@ namespace VS4Mac.TextGenerator.Views
|
|||
try
|
||||
{
|
||||
var editor = IdeApp.Workbench.ActiveDocument.Editor;
|
||||
int.TryParse(_numberEntry.Text, out int numberOfWords);
|
||||
var words = _textGeneratorService.GenerateWords(numberOfWords);
|
||||
int.TryParse(_numberEntry.Text, out int numberOfElements);
|
||||
string[] words = null;
|
||||
|
||||
switch (_generatorOptionsComboBox.SelectedIndex)
|
||||
{
|
||||
case 0:
|
||||
words = _textGeneratorService.GenerateCharacters(numberOfElements);
|
||||
break;
|
||||
case 1:
|
||||
words = _textGeneratorService.GenerateWords(numberOfElements);
|
||||
break;
|
||||
case 2:
|
||||
words = _textGeneratorService.GenerateSentences(numberOfElements);
|
||||
break;
|
||||
}
|
||||
|
||||
var result = string.Join(" ", words);
|
||||
editor.InsertAtCaret(result);
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче