From a6c9dba25ad9204ebde302728eb2a75532497f37 Mon Sep 17 00:00:00 2001 From: Aria Date: Fri, 3 Feb 2023 12:14:41 +0000 Subject: initial commit --- confirm.go | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 confirm.go (limited to 'confirm.go') diff --git a/confirm.go b/confirm.go new file mode 100644 index 0000000..a7d4748 --- /dev/null +++ b/confirm.go @@ -0,0 +1,92 @@ +package main + +import ( + "fmt" + + "github.com/charmbracelet/bubbles/viewport" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" +) + +type confirmModel struct { + ready bool + doRun bool + selected item + viewport viewport.Model + returnTo selectingModel +} + +func switchToConfirm(m selectingModel) (confirmModel, tea.Cmd) { + model := confirmModel{ + selected: m.list.SelectedItem().(item), + returnTo: m, + } + return model, model.Init() +} + +func (m confirmModel) View() string { + if !m.ready { + return "Initialising..." + } + + return appStyle.Render(fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.viewport.View(), m.footerView())) +} + +func (m confirmModel) Init() tea.Cmd { + return func() tea.Msg { + // FIXME: Hacky way to get a WindowSizeMsg event sent + frame_h, frame_v := appStyle.GetFrameSize() + return tea.WindowSizeMsg{ + Height: m.returnTo.list.Height() + frame_v, + Width: m.returnTo.list.Width() + frame_h, + } + } +} + +func (m confirmModel) headerView() string { + return confirmTitleStyle.Render(m.selected.title) +} +func (m confirmModel) footerView() string { + out := areYouSureTextStyle.Render("Are you sure?") + out += subtleTextStyle.Render(" RET to continue, q to go back, ctrl+c to quit") + return out +} + +func (m confirmModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + var cmds []tea.Cmd + + switch msg := msg.(type) { + case tea.KeyMsg: + switch msg.String() { + + case "enter": + m.doRun = true + return m, tea.Quit + case "ctrl-c": + return m, tea.Quit + case "q": + return m.returnTo, nil + } + case tea.WindowSizeMsg: + padding_h, padding_v := appStyle.GetFrameSize() + headerHeight := lipgloss.Height(m.headerView()) + footerHeight := lipgloss.Height(m.footerView()) + verticalMarginHeight := headerHeight + footerHeight + padding_v + + if !m.ready { + m.viewport = viewport.New(msg.Width-padding_h, msg.Height-verticalMarginHeight) + m.viewport.YPosition = headerHeight + padding_v + 1 + m.viewport.SetContent(m.selected.contents) + m.ready = true + } else { + m.viewport.Width = msg.Width - padding_h + m.viewport.Height = msg.Height - verticalMarginHeight - padding_v + } + } + + // Handle keyboard and mouse events in the viewport + var cmd tea.Cmd + m.viewport, cmd = m.viewport.Update(msg) + cmds = append(cmds, cmd) + return m, tea.Batch(cmds...) +} -- cgit v1.2.3