filetree

package
v0.0.0-...-00ab8b8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 13 Imported by: 0

README

FileTree Package

This package handles the representation of file trees. There are two ways to render files: one is to render them flat, so something like this:

dir1/file1
dir1/file2
file3

And the other is to render them as a tree

dir1/
  file1
  file2
file3

Internally we represent each of the above as a tree, but with the flat approach there's just a single root node and every path is a direct child of that root. Viewing in 'tree' mode (as opposed to 'flat' mode) allows for collapsing and expanding directories, and lets you perform actions on directories e.g. staging a whole directory. But it takes up more vertical space and sometimes you just want to have a flat view where you can go flick through your files one by one to see the diff.

This package is not concerned about rendering the tree: only representing its internal state.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CollapsedPaths

type CollapsedPaths struct {
	// contains filtered or unexported fields
}

func NewCollapsedPaths

func NewCollapsedPaths() *CollapsedPaths

func (*CollapsedPaths) Collapse

func (self *CollapsedPaths) Collapse(path string)

func (*CollapsedPaths) ExpandToPath

func (self *CollapsedPaths) ExpandToPath(path string)

func (*CollapsedPaths) IsCollapsed

func (self *CollapsedPaths) IsCollapsed(path string) bool

func (*CollapsedPaths) ToggleCollapsed

func (self *CollapsedPaths) ToggleCollapsed(path string)

type CommitFileNode

type CommitFileNode struct {
	*Node[models.CommitFile]
}

CommitFileNode wraps a node and provides some commit-file-specific methods for it.

func NewCommitFileNode

func NewCommitFileNode(node *Node[models.CommitFile]) *CommitFileNode

func (*CommitFileNode) Raw

func (self *CommitFileNode) Raw() *Node[models.CommitFile]

returns the underlying node, without any commit-file-specific methods attached

type CommitFileTree

type CommitFileTree struct {
	// contains filtered or unexported fields
}

func NewCommitFileTree

func NewCommitFileTree(getFiles func() []*models.CommitFile, log *logrus.Entry, showTree bool) *CommitFileTree

func (*CommitFileTree) CollapsedPaths

func (self *CommitFileTree) CollapsedPaths() *CollapsedPaths

func (*CommitFileTree) ExpandToPath

func (self *CommitFileTree) ExpandToPath(path string)

func (*CommitFileTree) Get

func (self *CommitFileTree) Get(index int) *CommitFileNode

func (*CommitFileTree) GetAllFiles

func (self *CommitFileTree) GetAllFiles() []*models.CommitFile

func (*CommitFileTree) GetAllItems

func (self *CommitFileTree) GetAllItems() []*CommitFileNode

func (*CommitFileTree) GetFile

func (self *CommitFileTree) GetFile(path string) *models.CommitFile

func (*CommitFileTree) GetIndexForPath

func (self *CommitFileTree) GetIndexForPath(path string) (int, bool)

func (*CommitFileTree) GetItem

func (self *CommitFileTree) GetItem(index int) types.HasUrn

func (*CommitFileTree) GetRoot

func (self *CommitFileTree) GetRoot() *CommitFileNode

func (*CommitFileTree) InTreeMode

func (self *CommitFileTree) InTreeMode() bool

func (*CommitFileTree) IsCollapsed

func (self *CommitFileTree) IsCollapsed(path string) bool

func (*CommitFileTree) Len

func (self *CommitFileTree) Len() int

func (*CommitFileTree) SetTree

func (self *CommitFileTree) SetTree()

func (*CommitFileTree) ToggleCollapsed

func (self *CommitFileTree) ToggleCollapsed(path string)

func (*CommitFileTree) ToggleShowTree

func (self *CommitFileTree) ToggleShowTree()

type CommitFileTreeViewModel

type CommitFileTreeViewModel struct {
	sync.RWMutex
	types.IListCursor
	ICommitFileTree
	// contains filtered or unexported fields
}

func NewCommitFileTreeViewModel

func NewCommitFileTreeViewModel(getFiles func() []*models.CommitFile, log *logrus.Entry, showTree bool) *CommitFileTreeViewModel

func (*CommitFileTreeViewModel) GetCanRebase

func (self *CommitFileTreeViewModel) GetCanRebase() bool

func (*CommitFileTreeViewModel) GetRef

func (self *CommitFileTreeViewModel) GetRef() types.Ref

func (*CommitFileTreeViewModel) GetSelected

func (self *CommitFileTreeViewModel) GetSelected() *CommitFileNode

func (*CommitFileTreeViewModel) GetSelectedFile

func (self *CommitFileTreeViewModel) GetSelectedFile() *models.CommitFile

func (*CommitFileTreeViewModel) GetSelectedItemId

func (self *CommitFileTreeViewModel) GetSelectedItemId() string

func (*CommitFileTreeViewModel) GetSelectedItemIds

func (self *CommitFileTreeViewModel) GetSelectedItemIds() ([]string, int, int)

func (*CommitFileTreeViewModel) GetSelectedItems

func (self *CommitFileTreeViewModel) GetSelectedItems() ([]*CommitFileNode, int, int)

func (*CommitFileTreeViewModel) GetSelectedPath

func (self *CommitFileTreeViewModel) GetSelectedPath() string

func (*CommitFileTreeViewModel) SetCanRebase

func (self *CommitFileTreeViewModel) SetCanRebase(canRebase bool)

func (*CommitFileTreeViewModel) SetRef

func (self *CommitFileTreeViewModel) SetRef(ref types.Ref)

func (*CommitFileTreeViewModel) ToggleShowTree

func (self *CommitFileTreeViewModel) ToggleShowTree()

duplicated from file_tree_view_model.go. Generics will help here

type FileNode

type FileNode struct {
	*Node[models.File]
}

FileNode wraps a node and provides some file-specific methods for it.

func NewFileNode

func NewFileNode(node *Node[models.File]) *FileNode

func (*FileNode) GetHasInlineMergeConflicts

func (self *FileNode) GetHasInlineMergeConflicts() bool

func (*FileNode) GetHasStagedChanges

func (self *FileNode) GetHasStagedChanges() bool

func (*FileNode) GetHasStagedOrTrackedChanges

func (self *FileNode) GetHasStagedOrTrackedChanges() bool

func (*FileNode) GetHasUnstagedChanges

func (self *FileNode) GetHasUnstagedChanges() bool

func (*FileNode) GetIsFile

func (self *FileNode) GetIsFile() bool

func (*FileNode) GetIsTracked

func (self *FileNode) GetIsTracked() bool

func (*FileNode) GetPreviousPath

func (self *FileNode) GetPreviousPath() string

func (*FileNode) Raw

func (self *FileNode) Raw() *Node[models.File]

returns the underlying node, without any file-specific methods attached

type FileTree

type FileTree struct {
	// contains filtered or unexported fields
}

func NewFileTree

func NewFileTree(getFiles func() []*models.File, log *logrus.Entry, showTree bool) *FileTree

func (*FileTree) CollapsedPaths

func (self *FileTree) CollapsedPaths() *CollapsedPaths

func (*FileTree) ExpandToPath

func (self *FileTree) ExpandToPath(path string)

func (*FileTree) FilterFiles

func (self *FileTree) FilterFiles(test func(*models.File) bool) []*models.File

func (*FileTree) Get

func (self *FileTree) Get(index int) *FileNode

func (*FileTree) GetAllFiles

func (self *FileTree) GetAllFiles() []*models.File

func (*FileTree) GetAllItems

func (self *FileTree) GetAllItems() []*FileNode

note: this gets all items when the filter is taken into consideration. There may be hidden files that aren't included here. Files off the screen however will be included

func (*FileTree) GetFile

func (self *FileTree) GetFile(path string) *models.File

func (*FileTree) GetFilter

func (self *FileTree) GetFilter() FileTreeDisplayFilter

func (*FileTree) GetIndexForPath

func (self *FileTree) GetIndexForPath(path string) (int, bool)

func (*FileTree) GetItem

func (self *FileTree) GetItem(index int) types.HasUrn

func (*FileTree) GetRoot

func (self *FileTree) GetRoot() *FileNode

func (*FileTree) InTreeMode

func (self *FileTree) InTreeMode() bool

func (*FileTree) IsCollapsed

func (self *FileTree) IsCollapsed(path string) bool

func (*FileTree) Len

func (self *FileTree) Len() int

func (*FileTree) SetStatusFilter

func (self *FileTree) SetStatusFilter(filter FileTreeDisplayFilter)

func (*FileTree) SetTree

func (self *FileTree) SetTree()

func (*FileTree) ToggleCollapsed

func (self *FileTree) ToggleCollapsed(path string)

func (*FileTree) ToggleShowTree

func (self *FileTree) ToggleShowTree()

func (*FileTree) Tree

func (self *FileTree) Tree() *FileNode

type FileTreeDisplayFilter

type FileTreeDisplayFilter int
const (
	DisplayAll FileTreeDisplayFilter = iota
	DisplayStaged
	DisplayUnstaged
	// this shows files with merge conflicts
	DisplayConflicted
)

type FileTreeViewModel

type FileTreeViewModel struct {
	sync.RWMutex
	types.IListCursor
	IFileTree
}

This combines our FileTree struct with a cursor that retains information about which item is selected. It also contains logic for repositioning that cursor after the files are refreshed

func NewFileTreeViewModel

func NewFileTreeViewModel(getFiles func() []*models.File, log *logrus.Entry, showTree bool) *FileTreeViewModel

func (*FileTreeViewModel) GetSelected

func (self *FileTreeViewModel) GetSelected() *FileNode

func (*FileTreeViewModel) GetSelectedFile

func (self *FileTreeViewModel) GetSelectedFile() *models.File

func (*FileTreeViewModel) GetSelectedItemId

func (self *FileTreeViewModel) GetSelectedItemId() string

func (*FileTreeViewModel) GetSelectedItemIds

func (self *FileTreeViewModel) GetSelectedItemIds() ([]string, int, int)

func (*FileTreeViewModel) GetSelectedItems

func (self *FileTreeViewModel) GetSelectedItems() ([]*FileNode, int, int)

func (*FileTreeViewModel) GetSelectedPath

func (self *FileTreeViewModel) GetSelectedPath() string

func (*FileTreeViewModel) SetStatusFilter

func (self *FileTreeViewModel) SetStatusFilter(filter FileTreeDisplayFilter)

func (*FileTreeViewModel) SetTree

func (self *FileTreeViewModel) SetTree()

func (*FileTreeViewModel) ToggleShowTree

func (self *FileTreeViewModel) ToggleShowTree()

If we're going from flat to tree we want to select the same file. If we're going from tree to flat and we have a file selected we want to select that. If instead we've selected a directory we need to select the first file in that directory.

type ICommitFileTree

type ICommitFileTree interface {
	ITree[models.CommitFile]

	Get(index int) *CommitFileNode
	GetFile(path string) *models.CommitFile
	GetAllItems() []*CommitFileNode
	GetAllFiles() []*models.CommitFile
	GetRoot() *CommitFileNode
}

type ICommitFileTreeViewModel

type ICommitFileTreeViewModel interface {
	ICommitFileTree
	types.IListCursor

	GetRef() types.Ref
	SetRef(types.Ref)
	GetCanRebase() bool
	SetCanRebase(bool)
}

type IFileTree

type IFileTree interface {
	ITree[models.File]

	FilterFiles(test func(*models.File) bool) []*models.File
	SetStatusFilter(filter FileTreeDisplayFilter)
	Get(index int) *FileNode
	GetFile(path string) *models.File
	GetAllItems() []*FileNode
	GetAllFiles() []*models.File
	GetFilter() FileTreeDisplayFilter
	GetRoot() *FileNode
}

type IFileTreeViewModel

type IFileTreeViewModel interface {
	IFileTree
	types.IListCursor
}

type ITree

type ITree[T any] interface {
	InTreeMode() bool
	ExpandToPath(path string)
	ToggleShowTree()
	GetIndexForPath(path string) (int, bool)
	Len() int
	GetItem(index int) types.HasUrn
	SetTree()
	IsCollapsed(path string) bool
	ToggleCollapsed(path string)
	CollapsedPaths() *CollapsedPaths
}

type Node

type Node[T any] struct {
	// File will be nil if the node is a directory.
	File *T

	// If the node is a directory, Children contains the contents of the directory,
	// otherwise it's nil.
	Children []*Node[T]

	// path of the file/directory
	Path string

	// rather than render a tree as:
	// a/
	//   b/
	//     file.blah
	//
	// we instead render it as:
	// a/b/
	//	 file.blah
	// This saves vertical space. The CompressionLevel of a node is equal to the
	// number of times a 'compression' like the above has happened, where two
	// nodes are squished into one.
	CompressionLevel int
}

Represents a file or directory in a file tree.

func BuildFlatTreeFromCommitFiles

func BuildFlatTreeFromCommitFiles(files []*models.CommitFile) *Node[models.CommitFile]

func BuildFlatTreeFromFiles

func BuildFlatTreeFromFiles(files []*models.File) *Node[models.File]

func BuildTreeFromCommitFiles

func BuildTreeFromCommitFiles(files []*models.CommitFile) *Node[models.CommitFile]

func BuildTreeFromFiles

func BuildTreeFromFiles(files []*models.File) *Node[models.File]

func (*Node[T]) Compress

func (self *Node[T]) Compress()

func (*Node[T]) Description

func (self *Node[T]) Description() string

func (*Node[T]) Every

func (self *Node[T]) Every(test func(*Node[T]) bool) bool

func (*Node[T]) EveryFile

func (self *Node[T]) EveryFile(test func(*T) bool) bool

func (*Node[T]) Flatten

func (self *Node[T]) Flatten(collapsedPaths *CollapsedPaths) []*Node[T]

func (*Node[T]) ForEachFile

func (self *Node[T]) ForEachFile(cb func(*T) error) error

func (*Node[T]) GetFile

func (self *Node[T]) GetFile() *T

func (*Node[T]) GetFilePathsMatching

func (self *Node[T]) GetFilePathsMatching(test func(*T) bool) []string

func (*Node[T]) GetIndexForPath

func (self *Node[T]) GetIndexForPath(path string, collapsedPaths *CollapsedPaths) (int, bool)

func (*Node[T]) GetLeaves

func (self *Node[T]) GetLeaves() []*Node[T]

func (*Node[T]) GetNodeAtIndex

func (self *Node[T]) GetNodeAtIndex(index int, collapsedPaths *CollapsedPaths) *Node[T]

func (*Node[T]) GetPath

func (self *Node[T]) GetPath() string

func (*Node[T]) GetPathsMatching

func (self *Node[T]) GetPathsMatching(test func(*Node[T]) bool) []string

func (*Node[T]) ID

func (self *Node[T]) ID() string

func (*Node[T]) IsFile

func (self *Node[T]) IsFile() bool

func (*Node[T]) Name

func (self *Node[T]) Name() string

func (*Node[T]) Size

func (self *Node[T]) Size(collapsedPaths *CollapsedPaths) int

func (*Node[T]) Some

func (self *Node[T]) Some(test func(*Node[T]) bool) bool

func (*Node[T]) SomeFile

func (self *Node[T]) SomeFile(test func(*T) bool) bool

func (*Node[T]) Sort

func (self *Node[T]) Sort()

func (*Node[T]) SortChildren

func (self *Node[T]) SortChildren()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL