From 44574b9768b1edfa02c8932692785da33c6ba56b Mon Sep 17 00:00:00 2001 From: Hernan Morales Durand <4825959+hernanmd@users.noreply.github.com> Date: Fri, 29 Nov 2024 16:46:16 +0100 Subject: [PATCH] Refactor code to allow other terminal implementations. Introduce Terminal Interface to the File Browser shell commands. --- ...ileBrowserOpenTerminalCommandTest.class.st | 8 +- .../StFileBrowserOpenTerminalCommand.class.st | 83 ++--------------- .../StFileBrowserSettings.class.st | 8 +- .../StTerminalInterface.class.st | 88 +++++++++++++++++++ 4 files changed, 104 insertions(+), 83 deletions(-) create mode 100644 src/NewTools-FileBrowser/StTerminalInterface.class.st diff --git a/src/NewTools-FileBrowser-Tests/StFileBrowserOpenTerminalCommandTest.class.st b/src/NewTools-FileBrowser-Tests/StFileBrowserOpenTerminalCommandTest.class.st index a7d96afb..314ec255 100644 --- a/src/NewTools-FileBrowser-Tests/StFileBrowserOpenTerminalCommandTest.class.st +++ b/src/NewTools-FileBrowser-Tests/StFileBrowserOpenTerminalCommandTest.class.st @@ -34,21 +34,21 @@ StFileBrowserOpenTerminalCommandTest >> terminalShellCommandTest: aBlock withPat StFileBrowserOpenTerminalCommandTest >> testOpenLinuxTerminalShellCommand [ self terminalShellCommandTest: [ :command :path | - command openLinuxTerminalShellCommand: path ] + command terminalInterface openLinuxTerminalShellCommand: path ] ] { #category : 'tests' } StFileBrowserOpenTerminalCommandTest >> testOpenMacTerminalShellCommand [ self terminalShellCommandTest: [ :command :path | - command openMacTerminalShellCommand: path ] + command terminalInterface openMacTerminalShellCommand: path ] ] { #category : 'tests' } StFileBrowserOpenTerminalCommandTest >> testOpenWindowsTerminalShellCommand [ self terminalShellCommandTest: [ :command :path | - command openWindowsTerminalCommand: path ] + command terminalInterface openWindowsTerminalCommand: path ] ] { #category : 'tests' } @@ -56,5 +56,5 @@ StFileBrowserOpenTerminalCommandTest >> testTerminalShellCommand [ "tests for a current platform" self terminalShellCommandTest: [ :command :path | - command terminalShellCommand: path ] + command terminalInterface terminalShellCommand: path ] ] diff --git a/src/NewTools-FileBrowser/StFileBrowserOpenTerminalCommand.class.st b/src/NewTools-FileBrowser/StFileBrowserOpenTerminalCommand.class.st index 853346b7..ddf11aa7 100644 --- a/src/NewTools-FileBrowser/StFileBrowserOpenTerminalCommand.class.st +++ b/src/NewTools-FileBrowser/StFileBrowserOpenTerminalCommand.class.st @@ -24,93 +24,20 @@ StFileBrowserOpenTerminalCommand >> execute [ "Execute the actions that should be done by the command. This method expect that the context has been put in #context inst. var. if any context is relevant." - self openTerminalOn: self context currentDirectory + self terminalInterface openTerminalOn: self context currentDirectory ] { #category : 'initialization' } StFileBrowserOpenTerminalCommand >> initialize [ + super initialize. self name: 'Open terminal here'; description: 'Open an OS terminal in this directory.' ] -{ #category : 'private' } -StFileBrowserOpenTerminalCommand >> openLinuxTerminalShellCommand: aPath [ - - ^ String streamContents: [ : stream | - stream - << 'gnome-terminal --working-directory='; - << aPath fullName; - << ' &' ] -] - -{ #category : 'private' } -StFileBrowserOpenTerminalCommand >> openMacTerminalShellCommand: aPath [ - - | escaped | - escaped := aPath fullName copyReplaceAll: ' ' with: '\ '. - ^ String streamContents: [ : stream | - stream - << 'open -a '; - << self preferredMacTerminalProgram; - space; - << escaped; - << ' &' ] -] - -{ #category : 'accessing' } -StFileBrowserOpenTerminalCommand >> openTerminalOn: aPath [ - - (self terminalShellCommand: aPath) - ifNotEmpty: [ : shellCmd | - (Delay forMilliseconds: 1000) wait. - LibC system: shellCmd ] - -] - -{ #category : 'private' } -StFileBrowserOpenTerminalCommand >> openWindowsTerminalCommand: aPath [ - - ^ String streamContents: [ : stream | - stream - << 'start cmd.exe /K "cd /d '; - << aPath fullName; - << '"' ] -] - -{ #category : 'private' } -StFileBrowserOpenTerminalCommand >> preferredLinuxTerminalProgram [ - - ^ StFileBrowserSettings linuxTerminalProgram - -] - -{ #category : 'private' } -StFileBrowserOpenTerminalCommand >> preferredMacTerminalProgram [ - - ^ StFileBrowserSettings macTerminalProgram - -] - -{ #category : 'private' } -StFileBrowserOpenTerminalCommand >> preferredWindowsTerminalProgram [ - - ^ StFileBrowserSettings windowsTerminalProgram - -] - -{ #category : 'accessing' } -StFileBrowserOpenTerminalCommand >> terminalShellCommand: aPath [ - "Answer a with the shell command to open a terminal for the receiver's OS" +{ #category : 'initialization' } +StFileBrowserOpenTerminalCommand >> terminalInterface [ - Smalltalk os isWindows - ifTrue: [ ^ self openWindowsTerminalCommand: aPath ]. - Smalltalk os isMacOS - ifTrue: [ ^ self openMacTerminalShellCommand: aPath ]. - (Smalltalk os version beginsWith: 'linux') - ifTrue: [ ^ self openLinuxTerminalShellCommand: aPath ]. - - self inform: 'No terminal for platform implemented'. - ^ String empty. + ^ StTerminalInterface default ] diff --git a/src/NewTools-FileBrowser/StFileBrowserSettings.class.st b/src/NewTools-FileBrowser/StFileBrowserSettings.class.st index a74ff3e8..f3c1deb6 100644 --- a/src/NewTools-FileBrowser/StFileBrowserSettings.class.st +++ b/src/NewTools-FileBrowser/StFileBrowserSettings.class.st @@ -49,6 +49,12 @@ StFileBrowserSettings class >> defaultDirectoryOn: aBuilder [ ] +{ #category : 'settings' } +StFileBrowserSettings class >> defaultTerminalProgram [ + + ^ 'Terminal' +] + { #category : 'settings' } StFileBrowserSettings class >> groupSettingsOn: aBuilder [ @@ -76,7 +82,7 @@ StFileBrowserSettings class >> linuxTerminalProgram: aString [ StFileBrowserSettings class >> macTerminalProgram [ ^ MacTerminalProgram - ifNil: [ MacTerminalProgram := 'Terminal' ] + ifNil: [ MacTerminalProgram := self defaultTerminalProgram ] ] { #category : 'settings' } diff --git a/src/NewTools-FileBrowser/StTerminalInterface.class.st b/src/NewTools-FileBrowser/StTerminalInterface.class.st new file mode 100644 index 00000000..d508c929 --- /dev/null +++ b/src/NewTools-FileBrowser/StTerminalInterface.class.st @@ -0,0 +1,88 @@ +Class { + #name : 'StTerminalInterface', + #superclass : 'Object', + #classVars : [ + 'Default' + ], + #category : 'NewTools-FileBrowser-Utilities', + #package : 'NewTools-FileBrowser', + #tag : 'Utilities' +} + +{ #category : 'instance creation' } +StTerminalInterface class >> default [ + + ^ Default + ifNil: [ Default := self new ] +] + +{ #category : 'accessing' } +StTerminalInterface class >> default: aClass [ + + Default := aClass +] + +{ #category : 'accessing' } +StTerminalInterface >> openLinuxTerminalShellCommand: aPath [ + + ^ String streamContents: [ : stream | + stream + << 'gnome-terminal --working-directory='; + << aPath fullName; + << ' &' ] +] + +{ #category : 'accessing' } +StTerminalInterface >> openMacTerminalShellCommand: aPath [ + + | escaped | + escaped := aPath fullName copyReplaceAll: ' ' with: '\ '. + ^ String streamContents: [ : stream | + stream + << 'open -a '; + << self preferredMacTerminalProgram; + space; + << escaped; + << ' &' ] +] + +{ #category : 'accessing' } +StTerminalInterface >> openTerminalOn: aPath [ + + (self terminalShellCommand: aPath) + ifNotEmpty: [ : shellCmd | + (Delay forMilliseconds: 1000) wait. + LibC system: shellCmd ] +] + +{ #category : 'accessing' } +StTerminalInterface >> openWindowsTerminalCommand: aPath [ + + ^ String streamContents: [ : stream | + stream + << 'start cmd.exe /K "cd /d '; + << aPath fullName; + << '"' ] +] + +{ #category : 'accessing' } +StTerminalInterface >> preferredMacTerminalProgram [ + + ^ StFileBrowserSettings macTerminalProgram + +] + +{ #category : 'accessing' } +StTerminalInterface >> terminalShellCommand: aPath [ + "Answer a with the shell command to open a terminal for the receiver's OS" + + Smalltalk os isWindows + ifTrue: [ ^ self openWindowsTerminalCommand: aPath ]. + Smalltalk os isMacOS + ifTrue: [ ^ self openMacTerminalShellCommand: aPath ]. + (Smalltalk os version beginsWith: 'linux') + ifTrue: [ ^ self openLinuxTerminalShellCommand: aPath ]. + + self inform: 'No terminal for platform implemented'. + ^ String empty. +]