Skip to content

Commit

Permalink
dont access model values directly
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakenelf committed Sep 11, 2021
1 parent 02f3a49 commit 8600cd1
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 42 deletions.
5 changes: 5 additions & 0 deletions internal/colorimage/colorimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func ImageToString(width, height uint, img image.Image) (string, error) {
return str.String(), nil
}

// GetImage returns the currently set image.
func (m Model) GetImage() image.Image {
return m.Image
}

// SetContent sets the content of the ascii image.
func (m *Model) SetContent(content string) {
m.Content = content
Expand Down
10 changes: 0 additions & 10 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ var Versions = VersionTypes{
AppVersion: "0.3.0",
}

// DimensionTypes contains the different types of dimensions.
type DimensionTypes struct {
StatusBarHeight int
}

// Dimensions contains the different kinds of dimensions and their values.
var Dimensions = DimensionTypes{
StatusBarHeight: 1,
}

// ColorTypes contains the different types of colors.
type ColorTypes struct {
White string
Expand Down
5 changes: 5 additions & 0 deletions internal/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func (m *Model) SetContent(content string) {
m.Content = content
}

// GetContent returns the markdown content.
func (m Model) GetContent() string {
return m.Content
}

// SetSize sets the width of the markdown.
func (m *Model) SetSize(width int) {
m.Width = width
Expand Down
15 changes: 15 additions & 0 deletions internal/pane/pane.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ func (m *Model) SetSize(width, height int) {
m.Viewport.Height = height - lipgloss.Width(border.Bottom+border.Top)
}

// GetHorizontalFrameSize returns the horizontal frame size of the pane.
func (m Model) GetHorizontalFrameSize() int {
return m.Style.GetHorizontalFrameSize()
}

// GetIsActive returns the active state of the pane.
func (m Model) GetIsActive() bool {
return m.IsActive
}

// SetActive sets the active state of the pane.
func (m *Model) SetActive(isActive bool) {
m.IsActive = isActive
}

// SetContent sets the content of the pane.
func (m *Model) SetContent(content string) {
borderColor := m.InactiveBorderColor
Expand Down
5 changes: 5 additions & 0 deletions internal/statusbar/statusbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ func (m Model) getStatusbarContent() (string, string, string, string) {
logo
}

// GetHeight returns the height of the statusbar.
func (m Model) GetHeight() int {
return m.Height
}

// SetContent sets the content of the statusbar.
func (m *Model) SetContent(totalFiles, cursor int, textInput string, showIcons, showCommandBar, inMoveMode bool, selectedFile, itemToMove fs.DirEntry) {
m.TotalFiles = totalFiles
Expand Down
5 changes: 5 additions & 0 deletions internal/text/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func (m *Model) SetContent(content string) {
m.Content = content
}

// GetContent returns the content of the text.
func (m Model) GetContent() string {
return m.Content
}

// View returns a string representation of text.
func (m *Model) View() string {
return lipgloss.NewStyle().
Expand Down
77 changes: 45 additions & 32 deletions internal/ui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,30 +132,36 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Any time the window is resized this is called, including when the app
// is first started.
case tea.WindowSizeMsg:
m.primaryPane.SetSize(msg.Width/2, msg.Height-constants.Dimensions.StatusBarHeight)
m.secondaryPane.SetSize(msg.Width/2, msg.Height-constants.Dimensions.StatusBarHeight)
m.primaryPane.SetSize(msg.Width/2, msg.Height-m.statusBar.GetHeight())
m.secondaryPane.SetSize(msg.Width/2, msg.Height-m.statusBar.GetHeight())
m.dirTree.SetSize(m.primaryPane.GetWidth())
m.statusBar.SetSize(msg.Width, constants.Dimensions.StatusBarHeight)
m.text.SetSize(m.secondaryPane.GetWidth() - m.secondaryPane.Style.GetHorizontalFrameSize())
m.markdown.SetSize(m.secondaryPane.GetWidth() - m.secondaryPane.Style.GetHorizontalFrameSize())
m.statusBar.SetSize(msg.Width, 1)
m.text.SetSize(m.secondaryPane.GetWidth() - m.secondaryPane.GetHorizontalFrameSize())
m.markdown.SetSize(m.secondaryPane.GetWidth() - m.secondaryPane.GetHorizontalFrameSize())
m.primaryPane.SetContent(m.dirTree.View())
m.help.Width = msg.Width

if m.colorimage.Image != nil {
resizeImageCmd := m.redrawImage(m.secondaryPane.GetWidth()-m.secondaryPane.Style.GetHorizontalFrameSize(), m.secondaryPane.GetHeight())
if m.colorimage.GetImage() != nil {
resizeImageCmd := m.redrawImage(
m.secondaryPane.GetWidth()-m.secondaryPane.GetHorizontalFrameSize(),
m.secondaryPane.GetHeight(),
)
cmds = append(cmds, resizeImageCmd)
}

if m.markdown.Content != "" {
if m.markdown.GetContent() != "" {
m.secondaryPane.SetContent(m.markdown.View())
}

if m.text.Content != "" {
if m.text.GetContent() != "" {
m.secondaryPane.SetContent(m.text.View())
}

if m.text.Content == "" && m.markdown.Content == "" && m.colorimage.Image == nil {
m.secondaryPane.SetContent(lipgloss.NewStyle().Width(m.secondaryPane.GetWidth() - m.secondaryPane.Style.GetHorizontalFrameSize()).Render(m.help.View(m.keys)))
if m.text.GetContent() == "" && m.markdown.GetContent() == "" && m.colorimage.GetImage() == nil {
m.secondaryPane.SetContent(lipgloss.NewStyle().
Width(m.secondaryPane.GetWidth() - m.secondaryPane.GetHorizontalFrameSize()).
Render(m.help.View(m.keys)),
)
}

if !m.ready {
Expand All @@ -171,7 +177,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.MouseWheelUp:
// The command bar is not open and the primary pane is active
// so scroll the dirtree up and update the primary panes content.
if !m.showCommandBar && m.primaryPane.IsActive {
if !m.showCommandBar && m.primaryPane.GetIsActive() {
m.dirTree.GoUp()
m.scrollPrimaryPane()
m.primaryPane.SetContent(m.dirTree.View())
Expand All @@ -185,7 +191,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.MouseWheelDown:
// Command bar is not shown and the primary pane is active
// so scroll the dirtree down and update the primary panes content.
if !m.showCommandBar && m.primaryPane.IsActive {
if !m.showCommandBar && m.primaryPane.GetIsActive() {
m.dirTree.GoDown()
m.scrollPrimaryPane()
m.primaryPane.SetContent(m.dirTree.View())
Expand All @@ -202,7 +208,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if msg.String() == "g" && m.previousKey.String() == "g" {
// If the command bar is not shown and the primary pane is active,
// reset the previous key, go to the top of the dirtree and pane.
if !m.showCommandBar && m.primaryPane.IsActive {
if !m.showCommandBar && m.primaryPane.GetIsActive() {
m.previousKey = tea.KeyMsg{}
m.dirTree.GotoTop()
m.primaryPane.GotoTop()
Expand Down Expand Up @@ -230,16 +236,17 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// If the command bar is not shown and the primary pane is active
// set the previous directory to the current directory,
// and update the directory listing to go back one directory.
if !m.showCommandBar && m.primaryPane.IsActive {
if !m.showCommandBar && m.primaryPane.GetIsActive() {
m.previousDirectory, _ = directory.GetWorkingDirectory()

return m, m.updateDirectoryListing(fmt.Sprintf("%s/%s", m.previousDirectory, constants.Directories.PreviousDirectory))
return m, m.updateDirectoryListing(
fmt.Sprintf("%s/%s", m.previousDirectory, constants.Directories.PreviousDirectory),
)
}

case key.Matches(msg, m.keys.Down):
// Scroll down in pane.
if !m.showCommandBar {
if m.primaryPane.IsActive {
if m.primaryPane.GetIsActive() {
m.dirTree.GoDown()
m.scrollPrimaryPane()
m.primaryPane.SetContent(m.dirTree.View())
Expand All @@ -251,7 +258,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, m.keys.Up):
// Scroll up in pane.
if !m.showCommandBar {
if m.primaryPane.IsActive {
if m.primaryPane.GetIsActive() {
m.dirTree.GoUp()
m.scrollPrimaryPane()
m.primaryPane.SetContent(m.dirTree.View())
Expand All @@ -262,18 +269,24 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

case key.Matches(msg, m.keys.Right):
// Open directory or read file content.
if !m.showCommandBar && m.primaryPane.IsActive {
if !m.showCommandBar && m.primaryPane.GetIsActive() {
if m.dirTree.GetSelectedFile().IsDir() && !m.textInput.Focused() {
currentDir, _ := directory.GetWorkingDirectory()
return m, m.updateDirectoryListing(fmt.Sprintf("%s/%s", currentDir, m.dirTree.GetSelectedFile().Name()))
return m, m.updateDirectoryListing(
fmt.Sprintf("%s/%s", currentDir, m.dirTree.GetSelectedFile().Name()),
)
}

return m, m.readFileContent(m.dirTree.GetSelectedFile(), m.secondaryPane.GetWidth()-m.secondaryPane.Style.GetHorizontalFrameSize(), m.secondaryPane.GetHeight())
return m, m.readFileContent(
m.dirTree.GetSelectedFile(),
m.secondaryPane.GetWidth()-m.secondaryPane.Style.GetHorizontalFrameSize(),
m.secondaryPane.GetHeight(),
)
}

case key.Matches(msg, m.keys.GotoBottom):
// Go to the bottom of the pane.
if !m.showCommandBar && m.primaryPane.IsActive {
if !m.showCommandBar && m.primaryPane.GetIsActive() {
m.dirTree.GotoBottom()
m.primaryPane.GotoBottom()
m.primaryPane.SetContent(m.dirTree.View())
Expand Down Expand Up @@ -378,21 +391,21 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

// Toggle hidden files and folders.
case key.Matches(msg, m.keys.ToggleHidden):
if !m.showCommandBar && m.primaryPane.IsActive {
if !m.showCommandBar && m.primaryPane.GetIsActive() {
m.dirTree.ToggleHidden()
return m, m.updateDirectoryListing(constants.Directories.CurrentDirectory)
}

// Toggle between the two panes if the command bar is not currently active.
case key.Matches(msg, m.keys.Tab):
if !m.showCommandBar {
m.primaryPane.IsActive = !m.primaryPane.IsActive
m.secondaryPane.IsActive = !m.secondaryPane.IsActive
m.primaryPane.SetActive(!m.primaryPane.GetIsActive())
m.secondaryPane.SetActive(!m.secondaryPane.GetIsActive())
}

// Enter move mode.
case key.Matches(msg, m.keys.EnterMoveMode):
if !m.showCommandBar && m.primaryPane.IsActive {
if !m.showCommandBar && m.primaryPane.GetIsActive() {
m.inMoveMode = true
m.primaryPane.SetActiveBorderColor(constants.Colors.Blue)
m.initialMoveDirectory, _ = directory.GetWorkingDirectory()
Expand All @@ -401,7 +414,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

// Zip up the currently selected item.
case key.Matches(msg, m.keys.Zip):
if !m.showCommandBar && m.primaryPane.IsActive {
if !m.showCommandBar && m.primaryPane.GetIsActive() {
return m, tea.Sequentially(
m.zipDirectory(m.dirTree.GetSelectedFile().Name()),
m.updateDirectoryListing(constants.Directories.CurrentDirectory),
Expand All @@ -410,7 +423,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

// Unzip the currently selected zip file.
case key.Matches(msg, m.keys.Unzip):
if !m.showCommandBar && m.primaryPane.IsActive {
if !m.showCommandBar && m.primaryPane.GetIsActive() {
return m, tea.Sequentially(
m.unzipDirectory(m.dirTree.GetSelectedFile().Name()),
m.updateDirectoryListing(constants.Directories.CurrentDirectory),
Expand All @@ -419,7 +432,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

// Copy the currently selected item.
case key.Matches(msg, m.keys.Copy):
if !m.showCommandBar && m.primaryPane.IsActive {
if !m.showCommandBar && m.primaryPane.GetIsActive() {
if m.dirTree.GetSelectedFile().IsDir() {
return m, tea.Sequentially(
m.copyDirectory(m.dirTree.GetSelectedFile().Name()),
Expand All @@ -439,9 +452,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.inMoveMode = false
m.itemToMove = nil
m.initialMoveDirectory = ""
m.primaryPane.IsActive = true
m.secondaryPane.IsActive = false
m.help.ShowAll = true
m.primaryPane.SetActive(true)
m.secondaryPane.SetActive(false)
m.textInput.Blur()
m.textInput.Reset()
m.secondaryPane.GotoTop()
Expand Down

0 comments on commit 8600cd1

Please sign in to comment.