Skip to content

Commit

Permalink
Refactor code to allow other terminal implementations.
Browse files Browse the repository at this point in the history
Introduce Terminal Interface to the File Browser shell commands.
  • Loading branch information
hernanmd committed Nov 29, 2024
1 parent 51fcf0e commit 44574b9
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,27 @@ 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' }
StFileBrowserOpenTerminalCommandTest >> testTerminalShellCommand [
"tests for a current platform"

self terminalShellCommandTest: [ :command :path |
command terminalShellCommand: path ]
command terminalInterface terminalShellCommand: path ]
]
83 changes: 5 additions & 78 deletions src/NewTools-FileBrowser/StFileBrowserOpenTerminalCommand.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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 <String> 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
]
8 changes: 7 additions & 1 deletion src/NewTools-FileBrowser/StFileBrowserSettings.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ StFileBrowserSettings class >> defaultDirectoryOn: aBuilder [

]

{ #category : 'settings' }
StFileBrowserSettings class >> defaultTerminalProgram [

^ 'Terminal'
]

{ #category : 'settings' }
StFileBrowserSettings class >> groupSettingsOn: aBuilder [
<systemsettings>
Expand Down Expand Up @@ -76,7 +82,7 @@ StFileBrowserSettings class >> linuxTerminalProgram: aString [
StFileBrowserSettings class >> macTerminalProgram [

^ MacTerminalProgram
ifNil: [ MacTerminalProgram := 'Terminal' ]
ifNil: [ MacTerminalProgram := self defaultTerminalProgram ]
]

{ #category : 'settings' }
Expand Down
88 changes: 88 additions & 0 deletions src/NewTools-FileBrowser/StTerminalInterface.class.st
Original file line number Diff line number Diff line change
@@ -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 <String> 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.
]

0 comments on commit 44574b9

Please sign in to comment.