Skip to content

Commit

Permalink
Merge pull request #464 from brunoCCOS/better-documentation
Browse files Browse the repository at this point in the history
Better documentation
  • Loading branch information
yorukot authored Nov 18, 2024
2 parents a388028 + bba0dfb commit 0a41f7b
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 17 deletions.
17 changes: 17 additions & 0 deletions src/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
internal "github.com/yorukot/superfile/src/internal"
)

// Run superfile app
func Run(content embed.FS) {

internal.LoadAllDefaultConfig(content)
Expand Down Expand Up @@ -91,6 +92,8 @@ func Run(content embed.FS) {
}
}

// Create proper directories for storing configuration and write default
// configurations to Config and Hotkeys toml
func InitConfigFile() {
// Create directories
if err := createDirectories(
Expand Down Expand Up @@ -134,6 +137,7 @@ func InitConfigFile() {
}

// Helper functions
// Create all dirs that does not already exists
func createDirectories(dirs ...string) error {
for _, dir := range dirs {
if _, err := os.Stat(dir); os.IsNotExist(err) {
Expand All @@ -150,6 +154,7 @@ func createDirectories(dirs ...string) error {
return nil
}

// Create all files if they do not exists yet
func createFiles(files ...string) error {
for _, file := range files {
if _, err := os.Stat(file); os.IsNotExist(err) {
Expand All @@ -161,6 +166,8 @@ func createFiles(files ...string) error {
return nil
}

// Check if is the first time initializing the app, if it is create
// use check file
func checkFirstUse() bool {
file := variable.FirstUseCheck
firstUse := false
Expand All @@ -173,6 +180,7 @@ func checkFirstUse() bool {
return firstUse
}

// Write data to the path file if it exists
func writeConfigFile(path, data string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.WriteFile(path, []byte(data), 0644); err != nil {
Expand All @@ -182,9 +190,14 @@ func writeConfigFile(path, data string) error {
return nil
}

// Check for the need of updates if AutoCheckUpdate is on, if its the first time
//that version is checked or if has more than 24h since the last version check,
//look into the repo if there's any more recent version
func CheckForUpdates() {
var Config internal.ConfigType


// Get AutoCheck flag from configuration files
data, err := os.ReadFile(variable.ConfigFile)
if err != nil {
log.Fatalf("Config file doesn't exist: %v", err)
Expand All @@ -199,6 +212,7 @@ func CheckForUpdates() {
return
}

// Check last time the version was checked
lastTime, err := readLastTimeCheckVersionFromFile(variable.LastCheckVersion)
if err != nil && !os.IsNotExist(err) {
fmt.Println("Error reading from file:", err)
Expand Down Expand Up @@ -249,6 +263,7 @@ func CheckForUpdates() {
}
}

// Convert version string to number
func versionToNumber(version string) int {
version = strings.ReplaceAll(version, "v", "")
version = strings.ReplaceAll(version, ".", "")
Expand All @@ -257,6 +272,7 @@ func versionToNumber(version string) int {
return num
}

// Check the last time the version file was checked
func readLastTimeCheckVersionFromFile(filename string) (time.Time, error) {
content, err := os.ReadFile(filename)
if err != nil {
Expand All @@ -273,6 +289,7 @@ func readLastTimeCheckVersionFromFile(filename string) (time.Time, error) {
return lastTime, nil
}

// Write content to filename
func writeToFile(filename, content string) error {
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/internal/default_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var (
DefaultThemeString string
)

// Generate and return model containing default configurations
// Generate and return model containing default configurations for interface
func defaultModelConfig(toggleDotFileBool bool, firstFilePanelDir string) model {
return model{
filePanelFocusIndex: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/internal/handle_file_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (m *model) deleteItemWarn() {
id := shortuuid.New()
message := channelMessage{
messageId: id,
messageType: snedWarnModal,
messageType: sendWarnModal,
}

if isExternalDiskPath(panel.location) {
Expand Down Expand Up @@ -630,4 +630,4 @@ func (m model) copyPath() {
if err := clipboard.WriteAll(panel.element[panel.cursor].location); err != nil {
outPutLog("Copy path error", panel.element[panel.cursor].location, err)
}
}
}
8 changes: 6 additions & 2 deletions src/internal/handle_modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (m *model) createItem() {
}

// Cancel rename file or directory
func (m *model) cancelReanem() {
func (m *model) cancelRename() {
panel := m.fileModel.filePanels[m.filePanelFocusIndex]
panel.rename.Blur()
panel.renaming = false
Expand Down Expand Up @@ -89,6 +89,7 @@ func (m *model) confirmSortOptions() {
m.fileModel.filePanels[m.filePanelFocusIndex] = panel
}

// Move the cursor up in the sort options menu
func (m *model) sortOptionsListUp() {
panel := m.fileModel.filePanels[m.filePanelFocusIndex]
if panel.sortOptions.cursor > 0 {
Expand All @@ -99,6 +100,7 @@ func (m *model) sortOptionsListUp() {
m.fileModel.filePanels[m.filePanelFocusIndex] = panel
}

// Move the cursor down in the sort options menu
func (m *model) sortOptionsListDown() {
panel := m.fileModel.filePanels[m.filePanelFocusIndex]
if panel.sortOptions.cursor < len(panel.sortOptions.data.options) - 1 {
Expand All @@ -123,7 +125,7 @@ func (m *model) cancelSearch() {
m.fileModel.filePanels[m.filePanelFocusIndex] = panel
}

// Confirm search
// Confirm search. This will exit the search bar and filter the files
func (m *model) confirmSearch() {
panel := m.fileModel.filePanels[m.filePanelFocusIndex]
panel.searchBar.Blur()
Expand Down Expand Up @@ -202,6 +204,8 @@ func (m *model) closeCommandLine() {
m.commandLine.input.Blur()
}

// Exec a command line input inside the pointing file dir. Like opening the
// focused file in the text editor
func (m *model) enterCommandLine() {
focusPanelDir := ""
for _, panel := range m.fileModel.filePanels {
Expand Down
15 changes: 13 additions & 2 deletions src/internal/key_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ func containsKey(v string, a []string) string {
return ""
}


// mainKey handles most of key commands in the regular state of the application. For
// keys that performs actions in multiple panels, like going up or down,
// check the state of model m and handle properly.
func (m *model) mainKey(msg string, cmd tea.Cmd) ( tea.Cmd) {
switch msg {

// If move up Key is pressed, check the current state and executes
case containsKey(msg, hotkeys.ListUp):
if m.focusPanel == sidebarFocus {
m.controlSideBarListUp(false)
Expand All @@ -30,6 +33,7 @@ func (m *model) mainKey(msg string, cmd tea.Cmd) ( tea.Cmd) {
}()
}

// If move down Key is pressed, check the current state and executes
case containsKey(msg, hotkeys.ListDown):
if m.focusPanel == sidebarFocus {
m.controlSideBarListDown(false)
Expand Down Expand Up @@ -176,6 +180,7 @@ func (m *model) normalAndBrowserModeKey(msg string) {
}
}

// Check the hotkey to cancel operation or create file
func (m *model) typingModalOpenKey(msg string) {
switch msg {
case containsKey(msg, hotkeys.CancelTyping):
Expand Down Expand Up @@ -219,6 +224,7 @@ func (m *model) warnModalOpenKey(msg string) {
}
}

// Handle key input to confirm or cancel and close quiting warn in SPF
func (m *model) confirmToQuitSuperfile(msg string) bool {
switch msg {
case containsKey(msg, hotkeys.Quit), containsKey(msg, hotkeys.CancelTyping):
Expand All @@ -232,6 +238,7 @@ func (m *model) confirmToQuitSuperfile(msg string) bool {
}
}

// Handles key inputs inside sort options menu
func (m *model) sortOptionsKey(msg string) {
switch msg {
case containsKey(msg, hotkeys.OpenSortOptionsMenu):
Expand All @@ -252,12 +259,13 @@ func (m *model) sortOptionsKey(msg string) {
func (m *model) renamingKey(msg string) {
switch msg {
case containsKey(msg, hotkeys.CancelTyping):
m.cancelReanem()
m.cancelRename()
case containsKey(msg, hotkeys.ConfirmTyping):
m.confirmRename()
}
}

// Check the key input and cancel or confirms the search
func (m *model) focusOnSearchbarKey(msg string) {
switch msg {
case containsKey(msg, hotkeys.CancelTyping):
Expand All @@ -267,6 +275,8 @@ func (m *model) focusOnSearchbarKey(msg string) {
}
}

// Check hotkey input in help menu. Possible actions are moving up, down
// and quiting the menu
func (m *model) helpMenuKey(msg string) {
switch msg {
case containsKey(msg, hotkeys.ListUp):
Expand All @@ -278,6 +288,7 @@ func (m *model) helpMenuKey(msg string) {
}
}

// Handle command line keys closing or entering command line
func (m *model) commandLineKey(msg string) {
switch msg {
case containsKey(msg, hotkeys.CancelTyping):
Expand Down
Loading

0 comments on commit 0a41f7b

Please sign in to comment.