private void ExecuteCommand() { var document = RtCommand.Document; var textRange = new TextRange(document.ContentStart, document.ContentEnd); var text = textRange.Text; text = text.TrimEnd('\r', '\n', ' ', '\t'); _trace.TraceVerbose("Writing \"{0}\"...", text); var adjustedText = AdjustForAccentSymbols(text); adjustedText = AdjustForSpecialCharacters(adjustedText); var now = DateTime.Now; User32.SetForegroundWindow(_targetWindow); SendKeys.SendWait(adjustedText + "\r"); Activate(); document.Blocks.Clear(); string stateTag = null; var payload = CreatePayload(now, text); // TODO: Fix a rare bug where this throws exception because the file is in use. _dataStore.Write(ref stateTag, payload); _position = _dataStore.Eof; }
private void CloseAndReturnSelection(object sender, RoutedEventArgs e) { e.Handled = true; SelectedItem = GetSelected(); _searcher.CancelSearch(); Close(); }
public IStoredItem GetPrevious(IStoredItem item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (item == _bof) { throw new ArgumentException("Cannot read before BOF."); } var bm = (FileStoreItem)item; if (bm.Position == 0L) { return(_bof); } using (var reader = GetReader()) { var stream = reader.BaseStream; var position = bm == _eof ? stream.Length : bm.Position; for ( ;;) { var binarySize = 0; var record = ReadPreviousRecord(reader, ref position, ref binarySize); if (record == null) { return(_bof); } return(new FileStoreItem(StateTag, position, ToPayload(record), binarySize)); } } }
private void RtCommand_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Up && _position != _dataStore.Bof) { var previous = _dataStore.GetPrevious(_position); SetCommand(previous); _position = previous; e.Handled = true; return; } if (e.Key == Key.Down && _position != _dataStore.Eof) { var next = _dataStore.GetNext(_position); SetCommand(next); _position = next; e.Handled = true; return; } if (e.Key == Key.Escape) { RtCommand.Document.Blocks.Clear(); e.Handled = true; return; } }
public byte[] CreateLink(IStoredItem item) { if (item == _bof) { return new byte[] { 1 } } ; if (item == _eof) { return new byte[] { 2 } } ; var fileItem = (FileStoreItem)item; var stream = new MemoryStream(); var writer = new BinaryWriter(stream, Encoding.UTF8, true); var stateTag = fileItem.StateTag; var position = fileItem.Position; writer.Write(stateTag != null); if (stateTag != null) { writer.Write(stateTag); } writer.Write(position); return(stream.ToArray()); }
public IStoredItem GetPrevious(IStoredItem item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (item == _bof) { throw new ArgumentException("Cannot read before BOF."); } if (item == _eof) { if (_last != null && _inner.StateTag != _last.StateTag) { // Inner store have changed. We must clear the last. SetLast(null); Debug.Assert(_last == null); } if (_last == null) { // Read the last from inner store, and initialize the cached last. var fromInner = _inner.GetPrevious(_inner.Eof); var newLast = fromInner == _inner.Bof ? _bof : new CacheEntry(fromInner, null, _eof); SetLast(newLast); if (newLast == _bof) { // Optimize for the empty case. _first = _eof; } } return(_last); } var bm = (CacheEntry)item; // If we don't have the previous, get it from the inner store. if (bm._previous == null) { var fromInner = _inner.GetPrevious(bm._inner); // Set the previous to either BOF or a wrapped entry. bm._previous = fromInner == _inner.Bof ? _bof : new CacheEntry(fromInner, null, bm); if (bm._previous == _bof) { // We have reach EOF. Cache the first. _first = bm; } } return(bm._previous); }
private void SetCommand(IStoredItem item) { if (item != _dataStore.Bof && item != _dataStore.Eof) { RtCommand.Document.Blocks.Clear(); var text = item.Command.TrimEnd('\r', '\n', ' ', '\t'); RtCommand.Document.Blocks.Add(new Paragraph(new Run(text))); RtCommand.CaretPosition = RtCommand.CaretPosition.DocumentStart; } }
public IStoredItem GetNext(IStoredItem item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (item == _eof) { throw new ArgumentException("Cannot read before BOF."); } if (item == _bof) { if (_first == null) { // Read the first from inner store, and initialize the cached first. var fromInner = _inner.GetNext(_inner.Bof); _first = fromInner == _inner.Eof ? _eof : new CacheEntry(fromInner, _bof, null); if (_first == _eof) { // Optimize for the empty case. _last = _bof; } } return(_first); } var bm = (CacheEntry)item; if (bm._next == _eof && _inner.StateTag != bm.StateTag) { // Inner store have changed. We must clear the last. SetLast(null); Debug.Assert(bm._next == null); } // If we don't have the next, get it from the inner store. if (bm._next == null) { var fromInner = _inner.GetNext(bm._next); // Set the next to either EOF or a wrapped entry. bm._next = fromInner == _inner.Eof ? _eof : new CacheEntry(fromInner, bm, null); if (bm._next == _eof) { // We have reached EOF. Cache the last. SetLast(bm); } } return(bm._next); }
private static bool SameCommand(IStoredItem a, IStoredItem b) { if (!(a.Payload is CommandPayload)) { return(false); } if (!(b.Payload is CommandPayload)) { return(false); } return(a.Command == b.Command); }
private static int MostRecentFirst(IStoredItem x, IStoredItem y) { if (x.WhenExecuted < y.WhenExecuted) { return(1); } if (x.WhenExecuted > y.WhenExecuted) { return(-1); } return(string.Compare(x.Command, y.Command, StringComparison.Ordinal)); }
private void MiSearch_Click(object sender, RoutedEventArgs e) { var dialog = new SearchWindow(_searcher); dialog.Owner = this; dialog.ShowDialog(); var selectedCommand = dialog.SelectedItem; if (selectedCommand != null) { SetCommand(dialog.SelectedItem); _position = _dataStore.Eof; } }
private void Window_Loaded(object sender, RoutedEventArgs e) { _trace.TraceVerbose("Reading configuration..."); var config = Configuration.LoadDefault(_args) ?? Configuration.CreateDefault(_args); // TODO: Should be provided by config. var inboundFile = new FileInfo(Path.Combine(config.LocalDbDirectory.FullName, "inbound.dat")); IDataStore inboundStore = new FileStore(inboundFile); inboundStore = new FilteredDataStore(inboundStore, IsCommand); IDataStore localStore = new FileStore(config); localStore = new FilteredDataStore(localStore, IsCommand); _dataStore = new NewOnTailDataStore(localStore); _position = _dataStore.Eof; // The localStore must be the last. var searchStore = new CachedDataStore(new MergedDataStore(new[] { inboundStore, localStore })); _searcher = new Searcher(searchStore); if (config.SharedDirectory != null) { _trace.TraceInformation("Using shared folder: {0}", config.SharedDirectory.FullName); _inboundMonitor = new InboundReplication(config.SharedDirectory, config.LocalDbDirectory, TimeSpan.FromSeconds(10), IsNotMyDataFile); _inboundMonitor.Start(); _outboundMonitor = new ReplicationJob("OutboundReplication", config.LocalDbDirectory, config.SharedDirectory, TimeSpan.FromSeconds(30), IsMyDataFile); _outboundMonitor.Start(); } else { _trace.TraceWarning("Shared folder not found: {0}\r\n\r\nReplication is disabled.", config.SharedDirectory?.FullName); } _parentProcessObserver.Start(); _consoleWindowObserver.Start(); _trace.TraceInformation("Initialization finished."); }
public IStoredItem GetNext(IStoredItem item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (item == Eof) { throw new ArgumentException("Cannot read before BOF."); } for ( ;;) { item = _inner.GetNext(item); if (item == Bof || Accept(item.Payload)) { return(item); } } }
public IStoredItem GetNext(IStoredItem item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (item == _eof) { throw new ArgumentException("Cannot read after EOF."); } var bm = (FileStoreItem)item; using (var reader = GetReader()) { long position; var binarySize = 0; if (bm == _bof) { position = 0L; } else { // Skip the specified record. position = bm.Position; ReadNextRecord(reader, ref position, ref binarySize); } for ( ;;) { var lastPosition = position; var record = ReadNextRecord(reader, ref position, ref binarySize); if (record == null) { return(_eof); } return(new FileStoreItem(StateTag, lastPosition, ToPayload(record), binarySize)); } } }
public MergedDataStore(IReadOnlyList <IDataStore> stores) { if (stores == null) { throw new ArgumentNullException(nameof(stores)); } _innerStores = new IDataStore[stores.Count]; var bofItems = new IStoredItem[_innerStores.Length]; var eofItems = new IStoredItem[_innerStores.Length]; for (var i = 0; i < _innerStores.Length; ++i) { _innerStores[i] = stores[i]; bofItems[i] = _innerStores[i].Bof; eofItems[i] = _innerStores[i].Eof; } Bof = new MergedDataStoreItem(bofItems, _innerStores.Length - 1); Eof = new MergedDataStoreItem(eofItems, 0); }
public IStoredItem GetPrevious(IStoredItem item) { var mergedItem = (MergedDataStoreItem)item; var activeItems = (IStoredItem[])mergedItem.Items.Clone(); var activeItemIndex = mergedItem.ItemIndex; for (var i = 0; i < _innerStores.Length; ++i) { --activeItemIndex; if (activeItemIndex == -1) { activeItemIndex = _innerStores.Length - 1; } var innerItem = activeItems[activeItemIndex]; var innerStore = _innerStores[activeItemIndex]; if (innerItem == innerStore.Bof) { continue; } innerItem = activeItems[activeItemIndex] = innerStore.GetPrevious(innerItem); if (innerItem != innerStore.Bof) { return(new MergedDataStoreItem(activeItems, activeItemIndex)); } } for (var i = 0; i < _innerStores.Length; ++i) { Debug.Assert(activeItems[i] == _innerStores[i].Bof); } return(Bof); }
public NewAtTailEntry(IStoredItem inner, NewAtTailEntry previous, NewAtTailEntry next) { _inner = inner ?? throw new ArgumentNullException(nameof(inner)); _previous = previous; _next = next; }
public byte[] CreateLink(IStoredItem item) { throw new NotImplementedException(); }
public Command(IStoredItem stored) { _stored = stored ?? throw new ArgumentNullException(nameof(stored)); }
private void CloseAndReturnNull(object sender, ExecutedRoutedEventArgs e) { SelectedItem = null; _searcher.CancelSearch(); Close(); }
public IStoredItem GetNext(IStoredItem item) { throw new NotImplementedException(); }
public MergedDataStoreItem(IStoredItem[] items, int itemIndex) { _items = items; _itemIndex = itemIndex; _current = items[itemIndex]; }