-
Notifications
You must be signed in to change notification settings - Fork 2
/
CommonUtils.cs
116 lines (99 loc) · 3.94 KB
/
CommonUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Http;
using System.Resources;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
namespace TextForge
{
internal class CommonUtils
{
public static readonly HttpClient client = new HttpClient();
public static void DisplayError(Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
public static void DisplayWarning(Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
public static void DisplayInformation(Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public static bool GetInternetAccessPermission(string url)
{
var result = MessageBox.Show($"{Forge.CultureHelper.GetLocalizedString("[CommonUtils.cs] (GetInternetAccessPermission) MessageBox #1 Text")}{Environment.NewLine}{url}", Forge.CultureHelper.GetLocalizedString("[CommonUtils.cs] (GetInternetAccessPermission) MessageBox #1 Caption"), MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
return result == DialogResult.Yes;
}
public static Word.Application GetApplication()
{
return Globals.ThisAddIn.Application;
}
public static Document GetActiveDocument()
{
return Globals.ThisAddIn.Application.ActiveDocument;
}
public static Comments GetComments()
{
return GetActiveDocument().Comments;
}
public static Range GetSelectionRange()
{
return GetApplication().Selection.Range;
}
public static int GetWordPageCount()
{
int pageCount = GetActiveDocument().ComputeStatistics(Word.WdStatistic.wdStatisticPages, false);
return pageCount;
}
public static IEnumerable<string> SplitString(string str, int chunkSize)
{
List<string> result = new List<string>();
for (int i = 0; i < str.Length; i += chunkSize)
{
if (i + chunkSize > str.Length)
chunkSize = str.Length - i;
result.Add(str.Substring(i, chunkSize));
}
return result;
}
public static string SubstringTokens(string text, int maxTokens)
{
return SubstringWithoutBounds(text, TokensToCharCount(maxTokens));
}
private static string SubstringWithoutBounds(string text, int maxLen)
{
return (maxLen >= text.Length) ? text : text.Substring(0, maxLen);
}
public static int TokensToCharCount(int tokenCount)
{
return tokenCount * 4; // https://platform.openai.com/tokenizer
}
public static int CharToTokenCount(int charCount)
{
return charCount / 4; // https://platform.openai.com/tokenizer
}
public static void GetEnvironmentVariableIfAvailable(ref string dest, string variable)
{
var key = Environment.GetEnvironmentVariable(variable);
if (key != null)
dest = key;
}
}
public class CultureLocalizationHelper
{
private ResourceManager _resourceManager;
public CultureLocalizationHelper(string baseName, System.Reflection.Assembly assembly)
{
_resourceManager = new ResourceManager(baseName, assembly);
}
public string GetLocalizedString(string key, CultureInfo culture = null)
{
string localizedString = _resourceManager.GetString(key, culture);
return (localizedString == null) ? _resourceManager.GetString(key, CultureInfo.InvariantCulture) : localizedString;
}
}
}