Skip to content

Commit 4c2de77

Browse files
committed
Added more unit tests, enabled mouse support for input fields, and modified bulk loading of images.
1 parent 891f6bd commit 4c2de77

File tree

9 files changed

+155
-59
lines changed

9 files changed

+155
-59
lines changed

constants/constants.go

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ const FrameStyleNormal = 0
118118
const FrameStyleRaised = 1
119119
const FrameStyleSunken = 2
120120
const CellTypeButton = 1
121+
const CellTypeTextInput = 2
121122

122123
const VirtualFileSystemZip = 1
123124
const VirtualFileSystemRar = 2

debug.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ func dumpScreenToFile() {
1818
dumpLayerToFile(commonResource.screenLayer)
1919
}
2020

21+
/*
22+
DumpScreenToTerminal allows you to dump the current visible screen layer to the
23+
terminal. In addition, the following information should be noted:
24+
25+
- This is a method just for internal testing and normally is not used unless
26+
required for debugging.
27+
*/
28+
func dumpScreenToTerminal() {
29+
log.Println(commonResource.screenLayer.GetBasicAnsiStringAsBase64())
30+
}
31+
2132
/*
2233
DumpLayerToFile allows you to dump a display layer to files on a disk. One
2334
file is a limited ansi representation of the screen layer. The other is
@@ -49,4 +60,4 @@ func printDebugLog(fileName string, textToPrint string) {
4960
if _, err := f.WriteString(textToPrint + "\n"); err != nil {
5061
log.Println(err)
5162
}
52-
}
63+
}

debug_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestPrintDebugLog(test *testing.T) {
1919
assert.NoErrorf(test, err, "Failed to delete the file '%s': ", fileName)
2020
}
2121

22-
func TestDumpScreenToFile(test *testing.T) {
22+
func TestDumpScreen(test *testing.T) {
2323
commonResource.isDebugEnabled = true
2424
layerAlias1 := "Layer1"
2525
layerWidth := 100

image.go

+7-36
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,13 @@ and further loading will stop.
7373
func LoadImagesInBulk(assetList memory.AssetListType) error {
7474
var err error
7575
for _, currentAsset := range assetList.ImageList {
76-
err = LoadImage(currentAsset.FileAlias, currentAsset.FileAlias)
76+
err = LoadImage(currentAsset.FileName, currentAsset.FileAlias)
77+
if err != nil {
78+
return err
79+
}
80+
}
81+
for _, currentAsset := range assetList.PreloadedImageList {
82+
err = LoadPreRenderedImage(currentAsset.FileName, currentAsset.FileAlias, currentAsset.WidthInCharacters, currentAsset.HeightInCharacters, currentAsset.BlurSigma)
7783
if err != nil {
7884
return err
7985
}
@@ -123,41 +129,6 @@ func LoadPreRenderedImage(imageFile string, imageAlias string, widthInCharacters
123129
return err
124130
}
125131

126-
/*
127-
LoadPreRenderedImagesInBulk allows you to load multiple pre-rendered images
128-
into memory at once. This is useful since it eliminates the need for error
129-
checking over each image as they are loaded. An example use of this method
130-
is as follows:
131-
132-
// Create a new asset list.
133-
assetList := dosktop.NewAssetList()
134-
// Add a pre-rendered image to our asset list, with a filename of
135-
// 'MyImageFile', an image alias of 'MyImageAlias', a size in
136-
// characters of 20x20, and a blur sigma of 0.5.
137-
assetList.AddImage("MyImageFile", "MyImageAlias", 20, 20, 0.5)
138-
// Load the list of images into memory.
139-
err := dosktop.LoadImagesInBulk(assetList)
140-
141-
In addition, the following information should be noted:
142-
143-
- This method works by reading in the provided asset list and then calling
144-
'LoadPreRenderedImage' accordingly each time. For more information about the
145-
loading of images, please see 'LoadPreRenderedImage' for more details.
146-
147-
- In the event an error occurs, it will be returned to the user immediately
148-
and further loading will stop.
149-
*/
150-
func LoadPreRenderedImagesInBulk(assetList memory.AssetListType) error {
151-
var err error
152-
for _, currentAsset := range assetList.PreloadedImageList {
153-
err = LoadPreRenderedImage(currentAsset.FileAlias, currentAsset.FileAlias, currentAsset.WidthInCharacters, currentAsset.HeightInCharacters, currentAsset.BlurSigma)
154-
if err != nil {
155-
return err
156-
}
157-
}
158-
return err
159-
}
160-
161132
/*
162133
LoadBase64Image allows you to load a base64 encoded image into memory without
163134
performing any ansi conversions ahead of time. This takes up more memory for

image_test.go

+70
Large diffs are not rendered by default.

input_menu.go

+22-5
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,17 @@ func GetInput(layerAlias string, styleEntry memory.TuiStyleEntryType, xLocation
481481
memory.KeyboardMemory.AddKeystrokeToKeyboardBuffer("end")
482482
}
483483
for currentKeyPressed != "enter" {
484+
mouseXLocation, mouseYLocation, mouseButtonPressed, _ := memory.MouseMemory.GetMouseStatus()
485+
mouseCellIdentifier := getCellIdByLayerAlias(layerAlias, mouseXLocation, mouseYLocation)
486+
if mouseCellIdentifier != constants.NullCellId {
487+
if mouseButtonPressed == 1 {
488+
cursorPosition = mouseCellIdentifier
489+
isScreenUpdateRequired = true
490+
}
491+
}
484492
currentKeyPressed = Inkey()
485493
if currentKeyPressed != "" {
494+
// If character is pressed.
486495
if len(currentKeyPressed) == 1 {
487496
if len(inputString) < maxLengthAllowed {
488497
inputString = inputString[:viewportPosition+cursorPosition] + currentKeyPressed + inputString[viewportPosition+cursorPosition:]
@@ -583,12 +592,12 @@ func GetInput(layerAlias string, styleEntry memory.TuiStyleEntryType, xLocation
583592
}
584593
}
585594
if isScreenUpdateRequired {
586-
drawInputString(layerEntry, styleEntry, xLocation, yLocation, width, 0, stringformat.GetFilledString(width, " "))
595+
drawInputString(layerEntry, styleEntry, xLocation, yLocation, width, 0, false, stringformat.GetFilledString(width, " "))
587596
if IsPasswordProtected {
588597
passwordProtectedString := stringformat.GetFilledString(len(inputString), "*")
589-
drawInputString(layerEntry, styleEntry, xLocation, yLocation, widthOfViewport, viewportPosition, passwordProtectedString)
598+
drawInputString(layerEntry, styleEntry, xLocation, yLocation, widthOfViewport, viewportPosition, true, passwordProtectedString)
590599
} else {
591-
drawInputString(layerEntry, styleEntry, xLocation, yLocation, widthOfViewport, viewportPosition, inputString)
600+
drawInputString(layerEntry, styleEntry, xLocation, yLocation, widthOfViewport, viewportPosition, true, inputString)
592601
}
593602
drawCursor(layerEntry, styleEntry, xLocation, yLocation, cursorPosition, false)
594603
UpdateDisplay()
@@ -639,10 +648,11 @@ the text layer, then only the visible portion will be displayed.
639648
start. If the remainder of your string is too long for the specified width,
640649
then only the visible portion will be displayed.
641650
*/
642-
func drawInputString(layerEntry *memory.LayerEntryType, styleEntry memory.TuiStyleEntryType, xLocation int, yLocation int, width int, stringPosition int, inputString string) {
651+
func drawInputString(layerEntry *memory.LayerEntryType, styleEntry memory.TuiStyleEntryType, xLocation int, yLocation int, width int, stringPosition int, isCellIdsRequired bool, inputString string) {
643652
attributeEntry := memory.NewAttributeEntry()
644653
attributeEntry.ForegroundColor = styleEntry.TextInputForegroundColor
645654
attributeEntry.BackgroundColor = styleEntry.TextInputBackgroundColor
655+
attributeEntry.CellType = constants.CellTypeTextInput
646656
runeSlice := []rune(inputString)
647657
var safeSubstring string
648658
if stringPosition+width <= len(inputString) {
@@ -651,5 +661,12 @@ func drawInputString(layerEntry *memory.LayerEntryType, styleEntry memory.TuiSty
651661
safeSubstring = string(runeSlice[stringPosition : stringPosition+len(inputString)-stringPosition])
652662
}
653663
arrayOfRunes := stringformat.GetRunesFromString(safeSubstring)
654-
printLayer(layerEntry, attributeEntry, xLocation, yLocation, arrayOfRunes)
664+
// Here we loop over each character to draw since we need to accommodate for unique
665+
// cell IDs (if required for mouse location detection).
666+
for currentRuneIndex := 0; currentRuneIndex < len(arrayOfRunes); currentRuneIndex ++ {
667+
if isCellIdsRequired {
668+
attributeEntry.CellId = currentRuneIndex
669+
}
670+
printLayer(layerEntry, attributeEntry, xLocation + currentRuneIndex, yLocation, []rune{arrayOfRunes[currentRuneIndex]})
671+
}
655672
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This is line 1
2+
This is line 2
3+
This is line 3
4+
This is line 4
5+
This is line 5

virtual_file_system.go

+18-8
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,8 @@ func getMD5Hash(text string) string {
106106
return hex.EncodeToString(hash[:])
107107
}
108108

109-
110109
/*
111-
SetVirtualFileSystem allows you to specify a virtual file system to mount.
110+
mountVirtualFileSystem allows you to specify a virtual file system to mount.
112111
A virtual file system is a ZIP or RAR archive that contains all files in which
113112
you wish to access. This is useful since instead of distributing multiple
114113
files and folders with your application, you can simply package everything
@@ -139,7 +138,7 @@ see the method 'GetScrambledPassword' for details.
139138
error will be returned so that your application can handle this case
140139
appropriately.
141140
*/
142-
func SetVirtualFileSystem(archivePath string, password string, scrambleKey string) error {
141+
func mountVirtualFileSystem(archivePath string, password string, scrambleKey string) error {
143142
err := isArchiveFormatZip(archivePath)
144143
if err == nil {
145144
virtualFileSystemArchiveType = constants.VirtualFileSystemZip
@@ -160,6 +159,18 @@ func SetVirtualFileSystem(archivePath string, password string, scrambleKey strin
160159
return err
161160
}
162161

162+
/*
163+
UnmountVirtualFileSystem allows you to reset the virtual file system to an
164+
unmounted state. This is useful for when you want to access the physical
165+
file system directly.
166+
*/
167+
func UnmountVirtualFileSystem() {
168+
virtualFileSystemArchiveType = 0
169+
virtualFileSystemArchive = ""
170+
virtualFileSystemPassword = ""
171+
virtualFileSystemEncryptionKey = ""
172+
}
173+
163174
/*
164175
isArchiveFormatZip allows you to detect if the provided archive is in ZIP
165176
format or not.
@@ -263,15 +274,14 @@ func getFileDataFromFileSystem(fileName string) ([]byte, error) {
263274
var err error
264275
if virtualFileSystemArchiveType == constants.VirtualFileSystemZip {
265276
fileData, err = getFileDataFromZipArchive(fileName)
277+
return fileData, err
266278
} else if virtualFileSystemArchiveType == constants.VirtualFileSystemRar {
267279
fileData, err = getFileDataFromRarArchive(fileName)
280+
return fileData, err
268281
}
282+
fileData, err = getFileDataFromLocalFileSystem(fileName)
269283
if err != nil {
270-
archiveError := err
271-
fileData, err = getFileDataFromLocalFileSystem(fileName)
272-
if err != nil {
273-
err = errors.New(fmt.Sprintf("Could not open the file '%s': %s, %s", fileName, archiveError.Error(), err.Error()))
274-
}
284+
err = errors.New(fmt.Sprintf("Could not open the file '%s': %s", fileName, err.Error()))
275285
}
276286
return fileData, err
277287
}

virtual_file_system_test.go

+19-8
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,53 @@ func TestGetUnscrambledPassword(test *testing.T) {
2121
assert.Equalf(test, obtainedResult, expectedResult, "The unscrambled password did not match what was expected!")
2222
}
2323

24-
func TestSetVirtualFileSystem(test *testing.T) {
24+
func TestMountVirtualFileSystem(test *testing.T) {
2525
var expectedResult error
2626
scrambledPassword := "TAFDRw=="
2727
scrambleKey := "SampleScrambleKey"
2828

2929
// Verify valid statements.
30-
obtainedResult := SetVirtualFileSystem(BASE_DIRECTORY + "valid.zip", scrambledPassword, scrambleKey)
30+
obtainedResult := mountVirtualFileSystem(BASE_DIRECTORY + "valid.zip", scrambledPassword, scrambleKey)
3131
assert.Equalf(test, obtainedResult, expectedResult, "Failed to open a valid ZIP filesystem!")
3232
_, err := getImageFromFileSystem("myFolder-1/myFolder-2/sample3.png")
3333
assert.NoErrorf(test, err, "Failed to obtain image data from ZIP file system.")
34-
obtainedResult = SetVirtualFileSystem(BASE_DIRECTORY + "valid.rar", scrambledPassword, scrambleKey)
34+
obtainedResult = mountVirtualFileSystem(BASE_DIRECTORY + "valid.rar", scrambledPassword, scrambleKey)
3535
_, err = getImageFromFileSystem("myFolder-1/myFolder-2/sample3.png")
3636
assert.NoErrorf(test, err, "Failed to obtain image data from RAR file system.")
3737

3838
// Verify invalid archive passwords generate errors.
3939
scrambleKey = "SampleScrambleKey_BAD"
40-
obtainedResult = SetVirtualFileSystem(BASE_DIRECTORY + "valid.zip", scrambledPassword, scrambleKey)
40+
obtainedResult = mountVirtualFileSystem(BASE_DIRECTORY + "valid.zip", scrambledPassword, scrambleKey)
4141
assert.Equalf(test, obtainedResult, expectedResult, "Failed to open a valid ZIP filesystem!")
4242
_, err = getImageFromFileSystem("myFolder-1/myFolder-2/sample3.png")
4343
assert.Errorf(test, err, "Expected an error retrieving a file from ZIP file system with a bad password.")
44-
obtainedResult = SetVirtualFileSystem(BASE_DIRECTORY + "valid.rar", scrambledPassword, scrambleKey)
44+
obtainedResult = mountVirtualFileSystem(BASE_DIRECTORY + "valid.rar", scrambledPassword, scrambleKey)
4545
_, err = getImageFromFileSystem("myFolder-1/myFolder-2/sample3.png")
4646
assert.Errorf(test, err, "Expected an error retrieving a file from RAR file system with a bad password.")
4747

4848
// Verify invalid file requests generate errors.
4949
scrambleKey = "SampleScrambleKey"
50-
obtainedResult = SetVirtualFileSystem(BASE_DIRECTORY + "valid.zip", scrambledPassword, scrambleKey)
50+
obtainedResult = mountVirtualFileSystem(BASE_DIRECTORY + "valid.zip", scrambledPassword, scrambleKey)
5151
assert.Equalf(test, obtainedResult, expectedResult, "Failed to open a valid ZIP filesystem!")
5252
_, err = getImageFromFileSystem("myFolder-1/myFolder-2/sample3.png_BAD")
5353
assert.Errorf(test, err, "Expected an error retrieving a file from ZIP file system that does not exist.")
54-
obtainedResult = SetVirtualFileSystem(BASE_DIRECTORY + "valid.rar", scrambledPassword, scrambleKey)
54+
obtainedResult = mountVirtualFileSystem(BASE_DIRECTORY + "valid.rar", scrambledPassword, scrambleKey)
5555
_, err = getImageFromFileSystem("myFolder-1/myFolder-2/sample3.png_BAD")
5656
assert.Errorf(test, err, "Expected an error retrieving a file from RAR file system that does not exist.")
5757

5858
// Verify invalid archives.
5959
assert.Equalf(test, obtainedResult, expectedResult, "Failed to open a valid ZIP filesystem!")
60-
obtainedResult = SetVirtualFileSystem(BASE_DIRECTORY + "invalid.zip", scrambledPassword, scrambleKey)
60+
obtainedResult = mountVirtualFileSystem(BASE_DIRECTORY + "invalid.zip", scrambledPassword, scrambleKey)
6161
assert.NotNil(test, obtainedResult, "Opening an invalid ZIP file was expected to fail when it didn't!")
62+
UnmountVirtualFileSystem()
63+
}
64+
65+
func TestGetFileDataFromLocalFileSystem(test *testing.T) {
66+
_, err := getFileDataFromLocalFileSystem(BASE_DIRECTORY + "valid.rar")
67+
assert.NoErrorf(test, err, "Did not expect an error reading a file that should exist!")
68+
}
69+
70+
func TestGetTextFromFileSystem(test *testing.T) {
71+
_, err := getTextFromFileSystem(BASE_DIRECTORY + "text_file.txt")
72+
assert.NoErrorf(test, err, "Did not expect an error reading a text file that should exist!")
6273
}

0 commit comments

Comments
 (0)