mirror of
https://github.com/rwinkhart/MUTN.git
synced 2026-08-27 20:36:29 -04:00
Split into separate packages
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#!/bin/sh
|
||||
gofmt -l -w -s ./src/*.go
|
||||
git add -f extra src/*.go src/*.mod .gitignore commit.sh LICENSE README.md
|
||||
gofmt -l -w -s ./src/cli/*.go
|
||||
gofmt -l -w -s ./src/offline/*.go
|
||||
git add -f extra src .gitignore commit.sh go.mod go.sum LICENSE main.go README.md
|
||||
git commit -m "$1"
|
||||
git push
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
module github.com/rwinkhart/MUTN
|
||||
|
||||
go 1.22.0
|
||||
|
||||
require golang.org/x/crypto v0.20.0
|
||||
|
||||
require (
|
||||
golang.org/x/sys v0.17.0 // indirect
|
||||
golang.org/x/term v0.17.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,6 @@
|
||||
golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg=
|
||||
golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ=
|
||||
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
|
||||
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
|
||||
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
|
||||
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/rwinkhart/MUTN/src/cli"
|
||||
"os"
|
||||
)
|
||||
|
||||
@@ -11,26 +12,26 @@ func main() {
|
||||
|
||||
if argsCount == 0 {
|
||||
|
||||
EntryListGen()
|
||||
cli.EntryListGen()
|
||||
|
||||
} else if argsCount == 1 {
|
||||
|
||||
switch args[0] {
|
||||
|
||||
case "help", "--help", "-h":
|
||||
HelpMain()
|
||||
cli.HelpMain()
|
||||
case "add":
|
||||
HelpAdd()
|
||||
cli.HelpAdd()
|
||||
case "edit":
|
||||
HelpEdit()
|
||||
cli.HelpEdit()
|
||||
case "copy":
|
||||
HelpCopy()
|
||||
cli.HelpCopy()
|
||||
case "gen":
|
||||
HelpGen()
|
||||
cli.HelpGen()
|
||||
case "version", "-v":
|
||||
Version()
|
||||
cli.Version()
|
||||
default:
|
||||
HelpMain()
|
||||
cli.HelpMain()
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
//go:build !windows
|
||||
|
||||
package main
|
||||
|
||||
// EntryRoot path to libmutton entry directory TODO to be moved to libmutton
|
||||
var EntryRoot = home + "/.local/share/libmutton"
|
||||
|
||||
// exported constants TODO to be moved to libmutton
|
||||
const (
|
||||
PathSeparator = "/"
|
||||
Windows = false
|
||||
)
|
||||
@@ -1,12 +0,0 @@
|
||||
//go:build windows
|
||||
|
||||
package main
|
||||
|
||||
// EntryRoot path to libmutton entry directory TODO to be moved to libmutton
|
||||
var EntryRoot = home + "\\AppData\\Local\\libmutton\\entries"
|
||||
|
||||
// exported constants TODO to be moved to libmutton
|
||||
const (
|
||||
PathSeparator = "\\"
|
||||
Windows = true
|
||||
)
|
||||
@@ -1,14 +1,14 @@
|
||||
package main
|
||||
package cli
|
||||
|
||||
import (
|
||||
"github.com/rwinkhart/MUTN/src/offline"
|
||||
termy "golang.org/x/crypto/ssh/terminal"
|
||||
"os"
|
||||
)
|
||||
|
||||
// global variables used across multiple files
|
||||
var (
|
||||
home, _ = os.UserHomeDir()
|
||||
rootLength = len(EntryRoot)
|
||||
rootLength = len(offline.EntryRoot)
|
||||
width, _, _ = termy.GetSize(int(os.Stdout.Fd()))
|
||||
)
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package main
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/rwinkhart/MUTN/src/offline"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -20,7 +21,7 @@ func indentSubtractor(skippedDirList []bool, dirList []string, currentDirIndex i
|
||||
var trimmedDirectory = dirList[currentDirIndex]
|
||||
|
||||
for i, skipped := range skippedDirList[:currentDirIndex] { // checks each skipped directory to determine if it is a parent to the current directory
|
||||
if strings.HasPrefix(trimmedDirectory, dirList[i]+PathSeparator) { // if the current directory is the child of this iteration's directory...
|
||||
if strings.HasPrefix(trimmedDirectory, dirList[i]+offline.PathSeparator) { // if the current directory is the child of this iteration's directory...
|
||||
if skipped { // ...and this iteration's directory was skipped...
|
||||
subtractor++ // increment the subtractor to indicate that the visual indentation should be reduced
|
||||
} else {
|
||||
@@ -81,14 +82,14 @@ func EntryListGen() {
|
||||
var dirList []string
|
||||
|
||||
// walk entry directory
|
||||
_ = filepath.WalkDir(EntryRoot,
|
||||
_ = filepath.WalkDir(offline.EntryRoot,
|
||||
func(fullPath string, entry fs.DirEntry, err error) error {
|
||||
|
||||
// check for errors encountered while walking directory
|
||||
if err != nil {
|
||||
// create EntryRoot if the error is the result of it not existing on the system
|
||||
if os.IsNotExist(err) {
|
||||
_ = os.Mkdir(EntryRoot, 0700)
|
||||
_ = os.Mkdir(offline.EntryRoot, 0700)
|
||||
dirList = append(dirList, "")
|
||||
} else {
|
||||
// otherwise, print the source of the error
|
||||
@@ -134,11 +135,11 @@ func EntryListGen() {
|
||||
}
|
||||
|
||||
// determine directory's indentation multiplier based on PathSeparator occurrences
|
||||
indent = strings.Count(directory, PathSeparator) - 1 // subtract 1 to avoid indenting root-level directories
|
||||
indent = strings.Count(directory, offline.PathSeparator) - 1 // subtract 1 to avoid indenting root-level directories
|
||||
|
||||
// check if next directory is within the current one
|
||||
if dirListLength > i+1 {
|
||||
if nextDir := dirList[i+1]; directory == nextDir[:strings.LastIndex(nextDir, PathSeparator)] {
|
||||
if nextDir := dirList[i+1]; directory == nextDir[:strings.LastIndex(nextDir, offline.PathSeparator)] {
|
||||
containsSubdirectory = true
|
||||
} else {
|
||||
containsSubdirectory = false
|
||||
@@ -152,17 +153,17 @@ func EntryListGen() {
|
||||
for _, file := range fileList {
|
||||
|
||||
// print the current file if it belongs in the current directory - otherwise, break the loop and move on to the next directory
|
||||
if lastSlash := strings.LastIndex(file, PathSeparator) + 1; file[:lastSlash-1] == directory {
|
||||
if lastSlash := strings.LastIndex(file, offline.PathSeparator) + 1; file[:lastSlash-1] == directory {
|
||||
|
||||
// print directory header if this is the first run of the loop
|
||||
if !containsFiles {
|
||||
containsFiles = true
|
||||
skippedDirList[i] = false // the directory header is being printed, indicate that it is not being skipped
|
||||
indent, vanityDirectory = indentSubtractor(skippedDirList, dirList, i, indent) // calculate the final indentation multiplier
|
||||
if !Windows { // for consistency, format directories with UNIX-style path separators on all platforms
|
||||
if !offline.Windows { // for consistency, format directories with UNIX-style path separators on all platforms
|
||||
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", vanityDirectory)
|
||||
} else {
|
||||
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", strings.ReplaceAll(vanityDirectory, PathSeparator, "/"))
|
||||
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", strings.ReplaceAll(vanityDirectory, offline.PathSeparator, "/"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,10 +176,10 @@ func EntryListGen() {
|
||||
if dirListLength > 1 { // and directories besides the root-level exist... display directory header and empty directory warning
|
||||
skippedDirList[i] = false // the directory header is being printed, indicate that it is not being skipped
|
||||
indent, vanityDirectory = indentSubtractor(skippedDirList, dirList, i, indent) // calculate the final indentation multiplier
|
||||
if !Windows { // for consistency, format directories with UNIX-style path separators on all platforms
|
||||
if !offline.Windows { // for consistency, format directories with UNIX-style path separators on all platforms
|
||||
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", vanityDirectory)
|
||||
} else {
|
||||
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", strings.ReplaceAll(vanityDirectory, PathSeparator, "/"))
|
||||
fmt.Printf("\n\n"+strings.Repeat(" ", indent*2)+"\033[38;5;7;48;5;8m%s/"+ansiReset+"\n", strings.ReplaceAll(vanityDirectory, offline.PathSeparator, "/"))
|
||||
}
|
||||
fmt.Print(strings.Repeat(" ", indent*2) + "\033[38;5;11m-empty directory-" + ansiReset)
|
||||
} else { // warn if the only thing that exists is the root-level directory
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package cli
|
||||
|
||||
import "fmt"
|
||||
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
module main
|
||||
|
||||
go 1.21.6
|
||||
|
||||
require golang.org/x/crypto v0.18.0
|
||||
|
||||
require (
|
||||
golang.org/x/sys v0.16.0 // indirect
|
||||
golang.org/x/term v0.16.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,10 @@
|
||||
package offline
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// global variables used across multiple files
|
||||
var (
|
||||
home, _ = os.UserHomeDir()
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
//go:build !windows
|
||||
|
||||
package offline
|
||||
|
||||
// EntryRoot path to libmutton entry directory
|
||||
var EntryRoot = home + "/.local/share/libmutton"
|
||||
|
||||
// exported constants
|
||||
const (
|
||||
PathSeparator = "/"
|
||||
Windows = false
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
//go:build windows
|
||||
|
||||
package offline
|
||||
|
||||
// EntryRoot path to libmutton entry directory
|
||||
var EntryRoot = home + "\\AppData\\Local\\libmutton\\entries"
|
||||
|
||||
// exported constants
|
||||
const (
|
||||
PathSeparator = "\\"
|
||||
Windows = true
|
||||
)
|
||||
Reference in New Issue
Block a user