-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from Deepanshu276/terminal-command
OSD-9083: Preferred terminal sub-command
- Loading branch information
Showing
10 changed files
with
167 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Copyright © 2021 Red Hat, Inc | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package terminal | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/openshift/pagerduty-short-circuiter/pkg/config" | ||
"github.com/openshift/pagerduty-short-circuiter/pkg/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "terminal", | ||
Short: "This command lets the user choose their preferred terminal emulator .", | ||
Args: cobra.NoArgs, | ||
RunE: selectTerminal, | ||
} | ||
|
||
func selectTerminal(cmd *cobra.Command, args []string) error { | ||
|
||
cfg, err := config.Load() | ||
if err != nil { | ||
err = fmt.Errorf("configuration file not found, run the 'kite login' command") | ||
return err | ||
|
||
} | ||
|
||
// List all the available terminal emulators and prompt the user to select one. | ||
selectedTerminal := utils.InitTerminalEmulator() | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
return err | ||
} | ||
if selectedTerminal == "" { | ||
os.Exit(1) | ||
} | ||
// Update the terminal configuration value in the config file. | ||
cfg.Terminal = selectedTerminal | ||
|
||
err = config.Save(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println("Emulator selected successfully") | ||
return nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package terminal | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gdamore/tcell/v2" | ||
"github.com/rivo/tview" | ||
) | ||
|
||
type EUI struct { | ||
app *tview.Application | ||
list *tview.List | ||
} | ||
|
||
var ( | ||
selectedTerminal string | ||
selected bool | ||
) | ||
|
||
func (eui *EUI) UiEmulator(terminals []string) string { | ||
eui.app = tview.NewApplication() | ||
|
||
eui.list = tview.NewList().ShowSecondaryText(false) | ||
|
||
// Add the available terminal emulators to the list. | ||
for i, t := range terminals { | ||
func(t string) { | ||
eui.list.AddItem(fmt.Sprintf("%d. %s", i+1, t), "", 0, func() { | ||
selectedTerminal = t | ||
selected = true | ||
eui.app.Stop() | ||
}) | ||
}(t) | ||
} | ||
|
||
eui.app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { | ||
if event.Rune() == 'q' || event.Rune() == 'Q' { | ||
|
||
selected = false | ||
eui.app.Stop() | ||
return nil | ||
} | ||
return event | ||
}) | ||
|
||
// Set the list as the root of the application and focus on it. | ||
eui.app.SetRoot(eui.list, true).SetFocus(eui.list) | ||
|
||
// Add a footer with a quit message. | ||
footer := tview.NewTextView().SetText("Q [Quit]") | ||
eui.app.SetRoot( | ||
tview.NewFlex(). | ||
SetDirection(tview.FlexRow). | ||
AddItem(eui.list, 0, 1, true). | ||
AddItem(footer, 1, 0, false), | ||
true, | ||
) | ||
|
||
// Run the application and wait for the user to make a selection. | ||
if err := eui.app.Run(); err != nil { | ||
fmt.Println("Error:", err) | ||
} | ||
|
||
if selected { | ||
return selectedTerminal | ||
} | ||
|
||
// Return an empty string if the user quits. | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters