commit 8b412c0c35263c9a92799e097637373926fe99fa
parent 1014d633f7be0113b1a5e8bf018f53af095631b9
Author: Nick Econopouly <wry@mm.st>
Date: Sun, 5 Apr 2020 01:59:02 -0400
Improve the export filename choosing experience
Diffstat:
M | main.go | | | 75 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 75 insertions(+), 0 deletions(-)
diff --git a/main.go b/main.go
@@ -88,6 +88,81 @@ func addButtonSetup(v view, filepaths *[]string, window *gtk.Window) {
})
}
+// connect the "Merge Datasets" button to the correct output file
+// chooser and the logic for merging and exporting the datasets
+func mergeButtonSetup(v view, filenames *[]string, window *gtk.Window) {
+ var err error
+
+ // another native file chooser
+ v.saveDialog, err = gtk.FileChooserNativeDialogNew("save",window,gtk.FILE_CHOOSER_ACTION_SAVE,"save","cancel")
+ if err != nil {
+ log.Fatal("Can't make saveDialog: ", err)
+ }
+
+ // unfortunately gtk doesn't seem to provide a way to
+ // enforce file extensions when creating a file with a
+ // gtkfilechooser, so we have to do it manually. These
+ // filters just amount to helpful hints for the user
+ CsvFilter, err := gtk.FileFilterNew(); CsvFilter.AddPattern("*.csv"); CsvFilter.SetName("Comma Separated Values")
+ ExcelFilter, err := gtk.FileFilterNew(); ExcelFilter.AddPattern("*.xlsx"); ExcelFilter.SetName("Microsoft Excel")
+ v.saveDialog.AddFilter(CsvFilter)
+ v.saveDialog.AddFilter(ExcelFilter)
+ v.saveDialog.SetFilter(CsvFilter)
+ v.saveDialog.SetFilename("merged.csv")
+ v.saveDialog.SetDoOverwriteConfirmation(true) // kinda important
+
+ _, err = v.mergeButton.Connect("clicked", func() {
+ // clear old error
+ v.errorLabel.SetText("")
+
+ if goodFileExtensions(filenames) && len(*filenames) != 0 {
+ response := gtk.ResponseType(v.saveDialog.Run())
+
+ if response == gtk.RESPONSE_ACCEPT {
+ outputFile := v.saveDialog.GetFilename()
+
+ // supported file extensions and their associated function for pulling the raw data
+ fileFormat := map[string]func(string) [][]string{
+ ".xlsx": pullExcel,
+ ".csv": pullCSV,
+ ".ods": pullODS,
+ }
+
+ // map of basenames to the [][]string data
+ raws := make(map[string][][]string)
+
+ // all datasets
+ datasets := make(map[string]*Dataset)
+
+ // import dataset based on file extension
+ for _, path := range *filenames {
+ basename := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))
+ ext := strings.ToLower(filepath.Ext(path))
+ if _, ok := fileFormat[ext]; ok {
+ raws[basename] = fileFormat[ext](path)
+ }
+ for name, data := range raws {
+ datasets[name] = ImportDataset(name, data)
+ }
+ }
+
+ // merge them into a single Dataset
+ var dataset Dataset
+ dataset.height = 1 // row of terms, even though it's empty
+ dataset.data = make(map[string][]string)
+ for _, d := range datasets {
+ mergeDatasets(&dataset,d)
+ }
+
+ // export dataset
+ exportDataset(&dataset, outputFile)
+ }
+ } else {
+ v.errorLabel.SetText("There was a problem with the files you chose. Make sure to choose supported spreadsheet formats.")
+ }
+ })
+}
+
func rebuildDatasetListBox(list *gtk.ListBox, filenames *[]string, window *gtk.Window) {
// clear list
list.GetChildren().Foreach(func(item interface{}) {