Skip to content

Commit ba90815

Browse files
committed
Finish the version 1.0.0 !!!
1 parent 3c0cdea commit ba90815

File tree

6 files changed

+95
-21
lines changed

6 files changed

+95
-21
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.24.6
44

55
require (
66
github.com/abdfnx/gosh v0.4.0
7+
github.com/christianhujer/isheadless v0.0.0-20181114123347-cbcc774d58b8
78
github.com/sqweek/dialog v0.0.0-20240226140203-065105509627
89
golang.org/x/term v0.35.0
910
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/TheTitanrain/w32 v0.0.0-20180517000239-4f5cfb03fabf h1:FPsprx82rdrX2j
22
github.com/TheTitanrain/w32 v0.0.0-20180517000239-4f5cfb03fabf/go.mod h1:peYoMncQljjNS6tZwI9WVyQB3qZS6u79/N3mBOcnd3I=
33
github.com/abdfnx/gosh v0.4.0 h1:cBvmHw8yV3oXiAcORpi7Oip+MT6/5HBMOrvpn0cZNYM=
44
github.com/abdfnx/gosh v0.4.0/go.mod h1:MUTJRMc7FpBb/bFnZ2U0hj48zj8284J1cS3AUiIRZ/o=
5+
github.com/christianhujer/isheadless v0.0.0-20181114123347-cbcc774d58b8 h1:DnYXuSpOSrPTdugQCwRSMC98/Bw3XlZWkUMX57K/OTU=
6+
github.com/christianhujer/isheadless v0.0.0-20181114123347-cbcc774d58b8/go.mod h1:wuBdElDmt1bBV1BNwQG5suCgEFosFhqkvpSJJemGz1A=
57
github.com/sqweek/dialog v0.0.0-20240226140203-065105509627 h1:2JL2wmHXWIAxDofCK+AdkFi1KEg3dgkefCsm7isADzQ=
68
github.com/sqweek/dialog v0.0.0-20240226140203-065105509627/go.mod h1:/qNPSY91qTz/8TgHEMioAUc6q7+3SOybeKczHMXFcXw=
79
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=

pkg/GoFileEncoder.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// src/GoFileEncoder.go
2-
31
package main
42

53
import (

pkg/communFunctions/communFunctions.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,64 @@ import (
44
"fmt"
55
"log"
66
"time"
7+
8+
"github.com/christianhujer/isheadless"
9+
"github.com/sqweek/dialog"
10+
)
11+
12+
type (
13+
SelectFilePathFilter [2]string
14+
SelectFilePathFilters []SelectFilePathFilter
15+
)
16+
17+
const (
18+
Save string = "save"
19+
Load string = "load"
720
)
821

22+
func SelectFilePath(
23+
title string,
24+
filters SelectFilePathFilters,
25+
startFile string,
26+
startDir string,
27+
actionType string,
28+
) (string, error) {
29+
30+
if isheadless.IsHeadless() {
31+
fmt.Print("Aucune interface graphique n'a été détectée sur votre ordinateur. Entrez le chemin absolu vers votre fichier : ")
32+
var path string
33+
fmt.Scanln(&path)
34+
return path, nil
35+
} else {
36+
var popup *dialog.FileBuilder = dialog.File()
37+
38+
if title != "" {
39+
popup = popup.Title(title)
40+
}
41+
42+
for _, filter := range filters {
43+
popup = popup.Filter(filter[0], filter[1])
44+
}
45+
46+
if startFile != "" {
47+
popup = popup.SetStartFile(startFile)
48+
}
49+
50+
if startDir != "" {
51+
popup = popup.SetStartDir(startDir)
52+
}
53+
54+
switch actionType {
55+
case Save:
56+
return popup.Save()
57+
case Load:
58+
return popup.Load()
59+
default:
60+
return "", fmt.Errorf("'actionType' must be either \"save\" ('Save') or \"load\" ('Load'), so \"%s\" is incorrect", actionType)
61+
}
62+
}
63+
}
64+
965
func tern[T any](cond bool, ifTrue T, ifFalse T) T {
1066
if cond {
1167
return ifTrue

pkg/decoder/decoder.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/eliotttak/GoFileEncoder/pkg/communFunctions"
1313

14-
"github.com/sqweek/dialog"
1514
"golang.org/x/term"
1615
)
1716

@@ -66,12 +65,18 @@ func Decoder() {
6665
var err error
6766

6867
communFunctions.Try(func() error {
69-
cryptedFilePath, err = dialog.File().
70-
Filter("Fichiers binaires encodés (.enc.bin)", "enc.bin").
71-
Filter("Tous les fichiers", "*").
72-
Title("Sélectionner un fichier").
73-
Load()
68+
cryptedFilePath, err = communFunctions.SelectFilePath(
69+
"Sélectionner un fichier",
70+
communFunctions.SelectFilePathFilters{
71+
{"Fichiers binaires encodés (.enc.bin)", "enc.bin"},
72+
{"Tous les fichiers", "*"},
73+
},
74+
"",
75+
"",
76+
communFunctions.Load,
77+
)
7478
return err
79+
7580
}, 3)
7681

7782
fmt.Printf("Vous avez sélectionné ce fichier : %s.\n\n", cryptedFilePath)
@@ -96,10 +101,13 @@ func Decoder() {
96101
var originalFilePath string
97102

98103
communFunctions.Try(func() error {
99-
originalFilePath, err = dialog.File().
100-
Title("Sauvegardez un fichier").
101-
SetStartFile(filepath.Base(originalFileProposition)).
102-
Save()
104+
originalFilePath, err = communFunctions.SelectFilePath(
105+
"Sauvegardez un fichier",
106+
communFunctions.SelectFilePathFilters{},
107+
filepath.Base(originalFileProposition),
108+
"",
109+
communFunctions.Save,
110+
)
103111
return err
104112
}, 3)
105113

@@ -142,7 +150,7 @@ func Decoder() {
142150
timeAfter = time.Now()
143151

144152
timeBetween = timeAfter.Sub(timeBefore)
145-
fmt.Printf("Fichier encodé en %s.\n", communFunctions.FormatDuration(timeBetween))
153+
fmt.Printf("Fichier décodé en %s.\n", communFunctions.FormatDuration(timeBetween))
146154
return nil
147155
}
148156

pkg/encoder/encoder.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/eliotttak/GoFileEncoder/pkg/communFunctions"
1212

13-
"github.com/sqweek/dialog"
1413
"golang.org/x/term"
1514
)
1615

@@ -53,7 +52,13 @@ func Encoder() {
5352

5453
communFunctions.Try(func() error {
5554
var err error
56-
originalFilePath, err = dialog.File().Title("Sélectionner un fichier").Load()
55+
originalFilePath, err = communFunctions.SelectFilePath(
56+
"Sélectionner un fichier",
57+
communFunctions.SelectFilePathFilters{},
58+
"",
59+
"",
60+
communFunctions.Load,
61+
)
5762
return err
5863
}, 3)
5964

@@ -70,12 +75,16 @@ func Encoder() {
7075

7176
communFunctions.Try(func() error {
7277
var err error
73-
cryptedFilePath, err = dialog.File().
74-
Title("Sauvegardez un fichier").
75-
Filter("Fichiers binaires encodés (.enc.bin)", "enc.bin").
76-
Filter("Tous les fichiers", "*").
77-
SetStartFile(filepath.Base(originalFilePath + ".enc.bin")).
78-
Save()
78+
cryptedFilePath, err = communFunctions.SelectFilePath(
79+
"Sauvegardez un fichier",
80+
communFunctions.SelectFilePathFilters{
81+
{"Fichiers binaires encodés (.enc.bin)", "enc.bin"},
82+
{"Tous les fichiers", "*"},
83+
},
84+
filepath.Base(originalFilePath+".enc.bin"),
85+
"",
86+
communFunctions.Save,
87+
)
7988
return err
8089
}, 3)
8190

0 commit comments

Comments
 (0)