-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Saves and restores main window size and position
- Loading branch information
1 parent
a2e96b6
commit a548f37
Showing
4 changed files
with
141 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace NetPad.Data; | ||
|
||
/// <summary> | ||
/// Represents a key-value data store. | ||
/// </summary> | ||
public interface IKeyValueDataStore | ||
{ | ||
TValue? Get<TValue>(string key) where TValue : class; | ||
void Set<TValue>(string key, TValue value); | ||
bool Contains(string key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/Infrastructure/NetPad.Infrastructure/FileSystemKeyValueDataStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using NetPad.Common; | ||
using NetPad.Configuration; | ||
using NetPad.Data; | ||
using NetPad.IO; | ||
|
||
namespace NetPad; | ||
|
||
public class FileSystemKeyValueDataStore : IKeyValueDataStore | ||
{ | ||
private static readonly FilePath _storeFilePath = AppDataProvider.AppDataDirectoryPath.CombineFilePath("key-values.txt"); | ||
private static readonly object _fileLock = new(); | ||
|
||
public TValue? Get<TValue>(string key) where TValue : class | ||
{ | ||
lock (_fileLock) | ||
{ | ||
if (!_storeFilePath.Exists()) | ||
{ | ||
return null; | ||
} | ||
|
||
using var reader = File.OpenText(_storeFilePath.Path); | ||
while (reader.ReadLine() is { } line) | ||
{ | ||
if (line.Length == 0 || !line.StartsWith(key)) | ||
{ | ||
continue; | ||
} | ||
|
||
var parts = line.Split('=', 2); | ||
if (parts.Length < 2) | ||
{ | ||
continue; | ||
} | ||
|
||
return JsonSerializer.Deserialize<TValue>(parts[1].Trim()); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public void Set<TValue>(string key, TValue value) | ||
{ | ||
lock (_fileLock) | ||
{ | ||
var lines = _storeFilePath.Exists() | ||
? File.ReadAllLines(_storeFilePath.Path).ToList() | ||
: new List<string>(); | ||
|
||
var iLine = lines.FindIndex(l => l.StartsWith(key)); | ||
|
||
if (iLine >= 0) | ||
{ | ||
lines[iLine] = $"{key}={JsonSerializer.Serialize(value)}"; | ||
} | ||
else | ||
{ | ||
lines.Add($"{key}={JsonSerializer.Serialize(value)}"); | ||
} | ||
|
||
File.WriteAllLines(_storeFilePath.Path, lines); | ||
} | ||
} | ||
|
||
public bool Contains(string key) | ||
{ | ||
lock (_fileLock) | ||
{ | ||
if (!_storeFilePath.Exists()) | ||
{ | ||
return false; | ||
} | ||
|
||
using var reader = File.OpenText(_storeFilePath.Path); | ||
while (reader.ReadLine() is { } line) | ||
{ | ||
if (line.Length == 0 || !line.StartsWith(key)) | ||
{ | ||
continue; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |