Skip to content

Commit 95f5bda

Browse files
committed
refactor: Simplify argument handling and improve code readability in Launcher and related modules
1 parent 864198c commit 95f5bda

File tree

3 files changed

+63
-47
lines changed

3 files changed

+63
-47
lines changed

extLauncher/Domain.fs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,31 +73,35 @@ module File =
7373
let triggered file = { file with Triggered = file.Triggered + 1 }
7474

7575
type Choose =
76-
| File = 0
77-
| Directory = 1
76+
| File
77+
| Directory
7878

7979
module Choose =
80-
let init =
80+
let fromIntCode =
8181
function
8282
| 0 -> Choose.File
8383
| 1 -> Choose.Directory
8484
| _ -> failwith "Invalid value"
8585

86+
let toIntCode =
87+
function
88+
| Choose.File -> 0
89+
| Choose.Directory -> 1
90+
8691
type Launcher = {
8792
Name: string
8893
Path: FilePath
89-
Arguments: string
94+
Arguments: string option
9095
Choose: Choose
9196
} with
9297

9398
static member name this = this.Name
9499

95100
module Launcher =
96101
let buildArgs launcher tolaunch =
97-
if String.IsNullOrEmpty launcher.Arguments then
98-
tolaunch
99-
else
100-
launcher.Arguments.Replace("%s", $"\"%s{tolaunch}\"")
102+
match launcher.Arguments with
103+
| None -> ""
104+
| Some args -> args.Replace("%s", $"\"%s{tolaunch}\"")
101105

102106
type Pattern =
103107
| WildcardPattern of string

extLauncher/Infra.fs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module IO =
2323
|> Seq.filter (fun filePath ->
2424
not
2525
<| (foldersToIgnore
26-
|> Array.exists (fun folderToIgnore -> filePath.StartsWith(folderToIgnore.value, StringComparison.CurrentCultureIgnoreCase)))
26+
|> Array.exists (fun folderToIgnore -> filePath.StartsWith(folderToIgnore.value, StringComparison.CurrentCultureIgnoreCase)))
2727
)
2828

2929
let private enumerateFiles (path: FolderPath) (foldersToIgnore: FolderPath array) =
@@ -47,7 +47,7 @@ module IO =
4747
|> Seq.filter (Path.GetFileName >> regex.IsMatch)
4848
|> (filterIgnoredFolders foldersToIgnore)
4949

50-
let getFiles folderPath foldersToIgnore pattern =
50+
let getFiles folderPath foldersToIgnore pattern =
5151
enumerateFiles folderPath foldersToIgnore pattern
5252
|> Seq.map (fun path -> FilePath path, path |> Path.GetFileNameWithoutExtension |> FileName)
5353
|> Seq.toArray
@@ -57,22 +57,22 @@ module Db =
5757
type LauncherDb = {
5858
Name: string
5959
Path: string
60-
Arguments: string
60+
Arguments: string | null
6161
Choose: int
6262
} with
6363

6464
static member fromDomain(launcher: Launcher) = {
6565
Name = launcher.Name
6666
Path = launcher.Path.value
67-
Arguments = launcher.Arguments
68-
Choose = int launcher.Choose
67+
Arguments = launcher.Arguments |> Option.toObj
68+
Choose = Choose.toIntCode launcher.Choose
6969
}
7070

7171
static member toDomain(launcherDb: LauncherDb) : Launcher = {
7272
Name = launcherDb.Name
7373
Path = FilePath launcherDb.Path
74-
Arguments = launcherDb.Arguments
75-
Choose = Choose.init launcherDb.Choose
74+
Arguments = launcherDb.Arguments |> Option.ofObj
75+
Choose = Choose.fromIntCode launcherDb.Choose
7676
}
7777

7878
type FileDb = {

extLauncher/Program.fs

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ module private Implementations =
1010
open System.Diagnostics
1111
type Path = System.IO.Path
1212

13-
let markup value = AnsiConsole.MarkupLineInterpolated value
13+
let markup value =
14+
AnsiConsole.MarkupLineInterpolated value
1415

1516
let notInitialized () =
1617
markup $"Folder not yet indexed: [yellow]{IO.AppName}[/] index [gray]--help[/]"
@@ -22,26 +23,31 @@ module private Implementations =
2223

2324
match launcher with
2425
| None ->
26+
let containingFolder = file.Path.folder
27+
2528
let psi = ProcessStartInfo file.Path.value
2629
psi.UseShellExecute <- true
30+
psi.WorkingDirectory <- containingFolder.value
2731
Process.Start psi |> ignore
2832
| Some launcher ->
29-
let path =
33+
let path, workingDirectory =
3034
match launcher.Choose with
31-
| Choose.File -> file.Path.value
32-
| Choose.Directory -> Path.GetDirectoryName file.Path.value
33-
| _ -> NotImplementedException() |> raise
35+
| Choose.File -> file.Path.value, file.Path.folder.value
36+
| Choose.Directory ->
37+
let dir = file.Path.folder.value
38+
dir, dir
3439

3540
let psi = ProcessStartInfo launcher.Path.value
3641
psi.Arguments <- Launcher.buildArgs launcher path
42+
psi.WorkingDirectory <- workingDirectory
3743
Process.Start psi |> ignore
3844

3945
let chooseLauncher folder file =
4046
match folder.Launchers with
4147
| [||] -> run file None
4248
| [| launcher |] -> run file (Some launcher)
4349
| launchers ->
44-
Helpers.searchByName launchers (fun l -> l.Name)
50+
Helpers.searchByName launchers _.Name
4551
|> Console.prompt Console.Terminal "With which launcher?" Launcher.name 10
4652
|> function
4753
| Some launcher -> run file (Some launcher)
@@ -78,7 +84,10 @@ module private Implementations =
7884
let toCount str num =
7985
if num > 1 then $"%i{num} %s{str}s" else $"%i{num} %s{str}"
8086

81-
let noNull s = if isNull s then "" else s
87+
let renderArgs args =
88+
match args with
89+
| None -> "-"
90+
| Some s -> s |> Markup.Escape |> _.Replace("%s", "[white bold]%s[/]")
8291

8392
let printLaunchers (folder: Folder) =
8493
let launchers =
@@ -97,10 +106,10 @@ module private Implementations =
97106
for l in folder.Launchers do
98107
launchers.AddRow(
99108
[|
100-
l.Name
109+
l.Name.EscapeMarkup()
101110
string l.Choose
102-
l.Path.value
103-
noNull l.Arguments
111+
l.Path.value.EscapeMarkup()
112+
renderArgs l.Arguments
104113
|]
105114
)
106115
|> ignore
@@ -128,7 +137,7 @@ type IndexSettings() =
128137

129138
[<CommandOption "-i|--ignore-folder">]
130139
[<Description "Folders to ignore during indexing.">]
131-
member val FoldersToIgnore : string array = [||] with get, set
140+
member val FoldersToIgnore: string array = [||] with get, set
132141

133142
type IndexCommand() =
134143
inherit Command<IndexSettings>()
@@ -168,8 +177,9 @@ type SetLauncherSettings() =
168177
member val Path = "" with get, set
169178

170179
[<CommandOption "-a|--args">]
171-
[<Description "Launcher command line arguments.">]
172-
member val Arguments = "" with get, set
180+
[<Description "Launcher command line arguments. Default is '%s' where the launched file or directory path will be inserted.">]
181+
[<DefaultValue("%s")>]
182+
member val Arguments = "%s" with get, set
173183

174184
[<CommandOption "-c|--choose">]
175185
[<Description "Which should be launched, the 'file' [italic](default)[/] or the 'directory'?">]
@@ -190,7 +200,10 @@ type SetLauncherCommand() =
190200
{
191201
Name = settings.Name
192202
Path = FilePath settings.Path
193-
Arguments = settings.Arguments
203+
Arguments =
204+
match settings.Arguments with
205+
| x when String.IsNullOrWhiteSpace(x) -> None
206+
| args -> Some args
194207
Choose = settings.Choose
195208
}
196209
|> fun launcher ->
@@ -322,18 +335,12 @@ module Program =
322335
app.Configure(fun conf ->
323336
conf.SetApplicationName(IO.AppName) |> ignore
324337

325-
conf
326-
.AddCommand<PromptCommand>("prompt")
327-
.WithDescription(
328-
"[italic](default command)[/] Type to search. Arrows Up/Down to navigate. Enter to launch. Escape to quit."
329-
)
338+
conf.AddCommand<PromptCommand>("prompt").WithDescription("[italic](default command)[/] Type to search. Arrows Up/Down to navigate. Enter to launch. Escape to quit.")
330339
|> ignore
331340

332341
conf
333342
.AddCommand<IndexCommand>("index")
334-
.WithDescription(
335-
"Indexes all files recursively with a specific pattern which can be a wildcard [italic](default)[/] or a regular expression."
336-
)
343+
.WithDescription("Indexes all files recursively with a specific pattern which can be a wildcard [italic](default)[/] or a regular expression.")
337344
|> ignore
338345

339346
conf.AddBranch<LauncherSettings>(
@@ -352,9 +359,7 @@ module Program =
352359
conf.AddCommand<DeindexCommand>("deindex").WithDescription("Clears the current index.")
353360
|> ignore
354361

355-
conf
356-
.AddCommand<InfoCommand>("info")
357-
.WithDescription("Prints the current pattern and all the indexed files.")
362+
conf.AddCommand<InfoCommand>("info").WithDescription("Prints the current pattern and all the indexed files.")
358363
|> ignore
359364

360365
conf.AddCommand<RefreshCommand>("refresh").WithDescription("Updates the current index.")
@@ -365,15 +370,17 @@ module Program =
365370
"index"
366371
"*.sln"
367372
|]
368-
) |> ignore
373+
)
374+
|> ignore
369375

370376
conf.AddExample(
371377
[|
372378
"index"
373379
"\"(.*)[.](fs|cs)proj$\""
374380
"--regex"
375381
|]
376-
) |> ignore
382+
)
383+
|> ignore
377384

378385
conf.AddExample(
379386
[|
@@ -382,15 +389,17 @@ module Program =
382389
"set"
383390
"execpath"
384391
|]
385-
) |> ignore
392+
)
393+
|> ignore
386394

387395
conf.AddExample(
388396
[|
389397
"launcher"
390398
"mylauncher"
391399
"remove"
392400
|]
393-
) |> ignore
401+
)
402+
|> ignore
394403

395404
conf.AddExample(
396405
[|
@@ -402,7 +411,8 @@ module Program =
402411
"file"
403412
"--args=\"-r %s\""
404413
|]
405-
) |> ignore
414+
)
415+
|> ignore
406416

407417
conf.AddExample(
408418
[|
@@ -413,7 +423,8 @@ module Program =
413423
"--choose"
414424
"directory"
415425
|]
416-
) |> ignore
426+
)
427+
|> ignore
417428

418429
conf.AddExample(
419430
[|
@@ -424,7 +435,8 @@ module Program =
424435
"--choose"
425436
"directory"
426437
|]
427-
) |> ignore
438+
)
439+
|> ignore
428440

429441
#if DEBUG
430442
conf.ValidateExamples() |> ignore

0 commit comments

Comments
 (0)