Skip to content

Commit

Permalink
fix prompt to work in S3 and ec2 details page
Browse files Browse the repository at this point in the history
  • Loading branch information
aanamshaikh committed Jan 30, 2023
1 parent 1791dd6 commit 263ec66
Showing 1 changed file with 103 additions and 6 deletions.
109 changes: 103 additions & 6 deletions internal/view/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,18 @@ func (a *App) DisplayEc2InstanceJson(sess *session.Session, instanceId string) {
tvForEc2Json.SetTitleColor(tcell.ColorLightSkyBlue)
tvForEc2Json.SetText(aws.GetSingleInstance(*sess, instanceId).GoString())
flex.AddItem(a.Views()["pAndRMenu"], 0, 2, false)
inputPrompt := a.Views()["cmd"]
inputPrompt := tview.NewInputField().
SetLabel("🐶>").
SetAcceptanceFunc(func(textToCheck string, lastChar rune) bool {
return true // accept any input
})
inputPrompt.SetFieldBackgroundColor(tcell.ColorBlack)
inputPrompt.SetBorder(true)

flex.AddItem(inputPrompt, 0, 1, false)
buckets, _ := aws.ListBuckets(*sess)
ins, _ := aws.GetInstances(*sess)
a.SearchUtility(inputPrompt, sess, buckets, flex, nil, ins)
flex.AddItem(tvForEc2Json, 0, 9, true)
a.Main.AddAndSwitchToPage("main:ece2-json", flex, true)
flex.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
Expand Down Expand Up @@ -395,24 +405,37 @@ func (a *App) DisplayS3Buckets(sess *session.Session, buckets []aws.BucketResp)
bucketInfo := aws.GetInfoAboutBucket(*sess, bucketName, "/", "")
folderArrayInfo, fileArrayInfo := getBucLevelInfo(bucketInfo)
if len(folderArrayInfo) == 0 && len(fileArrayInfo) == 0 {
a.DisplayS3ObjectForEmptyBuc(s3DataT, flex, bucketName)
a.DisplayS3ObjectForEmptyBuc(s3DataT, flex, bucketName, *sess)
} else {
a.setTableHeaderForS3(s3DataT, bucketName)
a.setTableContentForS3(s3DataT, bucketInfo.CommonPrefixes, bucketInfo.Contents)

flex.AddItem(a.Views()["pAndRMenu"], 0, 2, false)
inputPrompt := a.Views()["cmd"]

//extract to method
inputPrompt := tview.NewInputField().
SetLabel("🐶>").
SetAcceptanceFunc(func(textToCheck string, lastChar rune) bool {
return true // accept any input
})
inputPrompt.SetFieldBackgroundColor(tcell.ColorBlack)
inputPrompt.SetBorder(true)

flex.AddItem(inputPrompt, 0, 1, false)
flex.AddItem(s3DataT, 0, 9, true)
a.Main.AddAndSwitchToPage("s3data", flex, true)

//extract to method
flex.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyTab {
a.Application.SetFocus(inputPrompt)
}
return event
})

ins, _ := aws.GetInstances(*sess)
a.SearchUtility(inputPrompt, sess, buckets, flex, table, ins)

if len(bucketInfo.CommonPrefixes) != 0 || len(bucketInfo.Contents) != 0 {
s3DataT.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { // Empty
if event.Key() == tcell.KeyEnter { //d
Expand Down Expand Up @@ -458,9 +481,25 @@ func (a *App) DisplayS3Objects(s3DataTable *tview.Table, flex *tview.Flex, folde
a.setTableContentForS3(s3DataT, bucketInfo.CommonPrefixes, bucketInfo.Contents)

flex.AddItem(a.Views()["pAndRMenu"], 0, 2, false)
flex.AddItem(a.Views()["cmd"], 0, 1, false)
inputPrompt := tview.NewInputField().
SetLabel("🐶>").
SetAcceptanceFunc(func(textToCheck string, lastChar rune) bool {
return true // accept any input
})
inputPrompt.SetFieldBackgroundColor(tcell.ColorBlack)
inputPrompt.SetBorder(true)

flex.AddItem(inputPrompt, 0, 1, false)
flex.AddItem(s3DataT, 0, 9, true)
buckets, _ := aws.ListBuckets(sess)

a.SearchUtility(inputPrompt, &sess, buckets, flex, s3DataT, nil)
flex.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyTab {
a.Application.SetFocus(inputPrompt)
}
return event
})
s3DataT.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { //Tiger
if event.Key() == tcell.KeyEnter {
r, _ := s3DataT.GetSelection()
Expand Down Expand Up @@ -501,14 +540,32 @@ func (a *App) DisplayS3Objects(s3DataTable *tview.Table, flex *tview.Flex, folde
}
}

func (a *App) DisplayS3ObjectForEmptyBuc(s3DataT *tview.Table, flex *tview.Flex, bucketName string) {
func (a *App) DisplayS3ObjectForEmptyBuc(s3DataT *tview.Table, flex *tview.Flex, bucketName string, sess session.Session) {
s3DataT.SetTitle(bucketName)
s3DataT.SetTitleColor(tcell.ColorYellow)
s3DataT.SetCell(1, 0, tview.NewTableCell("No Data Found inside the Bucket").SetTextColor(tcell.ColorPeachPuff).SetAlign(tview.AlignCenter))
flex.AddItem(a.Views()["pAndRMenu"], 0, 2, false)
flex.AddItem(a.Views()["cmd"], 0, 1, false)
flex.AddItem(a.Views()["pAndRMenu"], 0, 2, false)
inputPrompt := tview.NewInputField().
SetLabel("🐶>").
SetAcceptanceFunc(func(textToCheck string, lastChar rune) bool {
return true // accept any input
})
inputPrompt.SetFieldBackgroundColor(tcell.ColorBlack)
inputPrompt.SetBorder(true)

flex.AddItem(inputPrompt, 0, 1, false)
flex.AddItem(s3DataT, 0, 9, true)
s3DataT.SetBorderFocusColor(tcell.ColorSpringGreen)
buckets, _ := aws.ListBuckets(sess)
ins, _ := aws.GetInstances(sess)
a.SearchUtility(inputPrompt, &sess, buckets, flex, s3DataT, ins)
flex.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyTab {
a.Application.SetFocus(inputPrompt)
}
return event
})
a.Main.AddAndSwitchToPage("s3data", flex, true)
s3DataT.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { // Empty
if event.Key() == tcell.KeyESC {
Expand All @@ -522,6 +579,46 @@ func (a *App) DisplayS3ObjectForEmptyBuc(s3DataT *tview.Table, flex *tview.Flex,
})
}

func (a *App) SearchUtility(inputField *tview.InputField, sess *session.Session, buckets []aws.BucketResp, servicePage *tview.Flex, servicePageContent *tview.Table, ins []aws.EC2Resp) {
inputField.SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEnter {
serviceName := inputField.GetText()

switch serviceName {
case "S3", "s3":
a.Flash().Info("Loading S3 Buckets...")
servicePage.Clear()
servicePageContent = a.DisplayS3Buckets(sess, buckets)
servicePage.AddItem(a.Views()["pAndRMenu"], 0, 2, false)
servicePage.AddItem(inputField, 0, 1, false)
servicePage.AddItem(servicePageContent, 0, 6, true)
a.Application.SetFocus(servicePageContent)
inputField.SetText("")

case "EC2", "ec2", "Ec2", "eC2":
a.Flash().Info("Loading EC2 instacnes...")
servicePage.Clear()
servicePageContent = a.DisplayEc2Instances(ins, sess)
servicePage.AddItem(a.Views()["pAndRMenu"], 0, 2, false)
servicePage.AddItem(inputField, 0, 1, false)
servicePage.AddItem(servicePageContent, 0, 6, true)
a.Application.SetFocus(servicePageContent)
inputField.SetText("")

default:
inputField.SetText("")
a.Flash().Err(fmt.Errorf("NO SERVICE..."))
}
}
servicePage.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyTab {
a.Application.SetFocus(inputField)
}
return event
})
})
}

func (a *App) setTableHeaderForS3(s3DataT *tview.Table, tableTitle string) *tview.Table {
s3DataT.SetTitle(tableTitle)
s3DataT.SetTitleColor(tcell.ColorYellow)
Expand Down

0 comments on commit 263ec66

Please sign in to comment.