Skip to content

Commit

Permalink
Fix issues with line feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
electrikmilk committed May 23, 2024
1 parent 465a455 commit 82b0fee
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 132 deletions.
28 changes: 15 additions & 13 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func checkRequiredArgs() {
default:
suffix = "th"
}
parserError(fmt.Sprintf("Missing required %d%s argument '%s' for action '%s'.%c%s", argIndex, suffix, param.name, currentActionIdentifier, EOL_RUNE, generateActionDefinition(param, false, true)))
parserError(fmt.Sprintf("Missing required %d%s argument '%s' for action '%s'.%s%s", argIndex, suffix, param.name, currentActionIdentifier, EOL, generateActionDefinition(param, false, true)))
}
}
}
Expand All @@ -283,11 +283,11 @@ func checkEnum(param *parameterDefinition, argument *actionArgument) {
if !slices.Contains(param.enum, value.(string)) {
parserError(
fmt.Sprintf(
"Invalid argument '%s' for %s.%c%c%s",
"Invalid argument '%s' for %s.%s%s%s",
value,
param.name,
EOL_RUNE,
EOL_RUNE,
EOL,
EOL,
generateActionDefinition(*param, false, true),
),
)
Expand Down Expand Up @@ -335,12 +335,12 @@ func typeCheck(param *parameterDefinition, argument *actionArgument) {
return
}
if argValueType != param.validType && param.validType != Variable && getVar.variableType != "Ask" {
parserError(fmt.Sprintf("Invalid variable value %v (%s) for argument '%s' (%s).%c%s",
parserError(fmt.Sprintf("Invalid variable value %v (%s) for argument '%s' (%s).%s%s",
argVal,
argValueType,
param.name,
param.validType,
EOL_RUNE,
EOL,
generateActionDefinition(*param, false, false),
))
}
Expand All @@ -351,12 +351,12 @@ func typeCheck(param *parameterDefinition, argument *actionArgument) {
if argValueType == String {
argVal = "\"" + argVal.(string) + "\""
}
parserError(fmt.Sprintf("Invalid value %v (%s) for argument '%s' (%s).%c%s",
parserError(fmt.Sprintf("Invalid value %v (%s) for argument '%s' (%s).%s%s",
argVal,
argValueType,
param.name,
param.validType,
EOL_RUNE,
EOL,
generateActionDefinition(*param, false, false),
))
}
Expand Down Expand Up @@ -417,9 +417,9 @@ func checkArg(param *parameterDefinition, argument *actionArgument) {
if param.defaultValue != nil && stringDefaultValue == realValue {
parserWarning(
fmt.Sprintf(
"Value for action argument '%s' is the same as the default value.%c%s",
"Value for action argument '%s' is the same as the default value.%s%s",
param.name,
EOL_RUNE,
EOL,
generateActionDefinition(*param, false, false),
),
)
Expand Down Expand Up @@ -500,7 +500,7 @@ func generateActionRestrictions() string {
func generateActionParamEnums(focus parameterDefinition) string {
var definition strings.Builder
if len(currentAction.parameters) != 0 {
definition.WriteRune(EOL_RUNE)
definition.WriteString(EOL)
}
var hasEnum = false
for _, param := range currentAction.parameters {
Expand All @@ -511,9 +511,11 @@ func generateActionParamEnums(focus parameterDefinition) string {
continue
}
hasEnum = true
definition.WriteString(ansi(fmt.Sprintf("%cAvailable %ss:%c", EOL_RUNE, param.name, EOL_RUNE), yellow))
definition.WriteString(EOL)
definition.WriteString(ansi(fmt.Sprintf("Available %ss:%s", param.name, EOL), yellow))
for _, e := range param.enum {
definition.WriteString(fmt.Sprintf("- %s%c", e, EOL_RUNE))
definition.WriteString(fmt.Sprintf("- %s", e))
definition.WriteString(EOL)
}
}
if hasEnum {
Expand Down
11 changes: 6 additions & 5 deletions actions_std.go
Original file line number Diff line number Diff line change
Expand Up @@ -5401,19 +5401,20 @@ var actions = map[string]*actionDefinition{

var vcard strings.Builder
vcard.WriteString("BEGIN:VCARD")
vcard.WriteRune(EOL_RUNE)
vcard.WriteString(EOL)
vcard.WriteString("VERSION:3.0")
vcard.WriteRune(EOL_RUNE)
vcard.WriteString(EOL)
vcard.WriteString(fmt.Sprintf("N;CHARSET=utf-8:%s", title))
vcard.WriteRune(EOL_RUNE)
vcard.WriteString(EOL)
vcard.WriteString(fmt.Sprintf("ORG:%s", subtitle))
vcard.WriteRune(EOL_RUNE)
vcard.WriteString(EOL)

if len(args) > 2 {
var iconFile = getArgValue(args[2]).(string)
var bytes, readErr = os.ReadFile(iconFile)
handle(readErr)
vcard.WriteString(fmt.Sprintf("PHOTO;ENCODING=b:%s%c", base64.StdEncoding.EncodeToString(bytes), EOL_RUNE))
vcard.WriteString(fmt.Sprintf("PHOTO;ENCODING=b:%s", base64.StdEncoding.EncodeToString(bytes)))
vcard.WriteString(EOL)
}
vcard.WriteString("END:VCARD")
args[0] = actionArgument{
Expand Down
7 changes: 5 additions & 2 deletions cherri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ func TestCherri(_ *testing.T) {

func TestSingleFile(_ *testing.T) {
currentTest = "tests/conditionals.cherri"
fmt.Printf("⚙️ Compiling %s...%c", ansi(currentTest, bold), EOL_RUNE)
fmt.Printf("⚙️ Compiling %s...", ansi(currentTest, bold))
fmt.Print(EOL)
os.Args[1] = currentTest
main()
fmt.Printf(ansi("✅ PASSED", green, bold)+"%c%c", EOL_RUNE, EOL_RUNE)
fmt.Printf(ansi("✅ PASSED", green, bold))
fmt.Print(EOL)
fmt.Print(EOL)
}

func TestActionList(_ *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion copy_paste.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func collectCopy() {

func pasteCopy() {
var identifier = collectIdentifier()
if char == EOL_RUNE {
if isToken(EOL) {
idx--
lineIdx--
lineCharIdx = len(lines[lineIdx])
Expand Down
52 changes: 26 additions & 26 deletions custom_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,25 +131,25 @@ func makeCustomActionsHeader() {
var outputActionRegex = regexp.MustCompile(`(?:must)?[o|O]utput(?:OrClipboard)?\((.*?)\)`)
var customActionsHeader strings.Builder
customActionsHeader.WriteString("if ShortcutInput {")
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)
customActionsHeader.WriteString(" const _cherri_inputType = typeOf(ShortcutInput)")
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)
customActionsHeader.WriteString(" if _cherri_inputType == \"Dictionary\" {")
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)
customActionsHeader.WriteString(" const _cherri_input = getDictionary(ShortcutInput)")
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)
customActionsHeader.WriteString(" const _cherri_identifier = getValue(_cherri_input, \"cherri_functions\")")
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)
customActionsHeader.WriteString(" const _cherri_valid = number(_cherri_identifier)")
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)
customActionsHeader.WriteString(" if _cherri_valid == true {")
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)
customActionsHeader.WriteString(" const _cherri_function = getValue(_cherri_input, \"function\")")
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)
customActionsHeader.WriteString(" const _cherri_function_name = \"{_cherri_function}\"")
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)
customActionsHeader.WriteString(" const _cherri_function_args = getValue(_cherri_input, \"arguments\")")
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)

for identifier, customAction := range customActions {
if !customAction.used {
Expand All @@ -159,55 +159,55 @@ func makeCustomActionsHeader() {
customActionsHeader.WriteString(" if _cherri_function_name == \"")
customActionsHeader.WriteString(identifier)
customActionsHeader.WriteString("\" {")
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)

for i, param := range customAction.definition.parameters {
var idx = i + 1
var argumentReference = fmt.Sprintf("_cherri_%s_arg_%d_%s", identifier, idx, param.name)

customActionsHeader.WriteString(fmt.Sprintf(" const %s = getListItem(_cherri_function_args, %d)", argumentReference, idx))
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(fmt.Sprintf(" @%s: %s%c ", param.name, param.validType, EOL_RUNE))
customActionsHeader.WriteString(EOL)
customActionsHeader.WriteString(fmt.Sprintf(" @%s: %s%s ", param.name, param.validType, EOL))

switch param.validType {
case String:
customActionsHeader.WriteString(fmt.Sprintf("@%s = \"{%s}\"%s", param.name, argumentReference, EOL))
case Integer:
customActionsHeader.WriteString(fmt.Sprintf("@%s = number(%s)%c", param.name, argumentReference, EOL_RUNE))
customActionsHeader.WriteString(fmt.Sprintf("@%s = number(%s)%s", param.name, argumentReference, EOL))
case Dict:
customActionsHeader.WriteString(fmt.Sprintf("@%s = getDictionary(%s)%c", param.name, argumentReference, EOL_RUNE))
customActionsHeader.WriteString(fmt.Sprintf("@%s = getDictionary(%s)%s", param.name, argumentReference, EOL))
case Arr:
customActionsHeader.WriteString(fmt.Sprintf("const %s_array_dictionary = getDictionary(%s)", argumentReference, argumentReference))
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)
customActionsHeader.WriteString(fmt.Sprintf(" const %s_array = getValue(%s,\"array\")", argumentReference, argumentReference))
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)
customActionsHeader.WriteString(fmt.Sprintf(" for %s_array_item in %s_array {", argumentReference, argumentReference))
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)
customActionsHeader.WriteString(fmt.Sprintf(" @%s += %s_array_item", param.name, argumentReference))
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)
customActionsHeader.WriteString(" }")
default:
customActionsHeader.WriteString(argumentReference)
}

customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)
}

customActionsHeader.WriteString(customAction.body)

if !outputActionRegex.MatchString(customAction.body) {
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)
customActionsHeader.WriteString("output(nil)")
}

customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)
customActionsHeader.WriteString(" }")
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(EOL)
}

customActionsHeader.WriteString(" output(nil)")
customActionsHeader.WriteRune(EOL_RUNE)
customActionsHeader.WriteString(fmt.Sprintf(" }%c }%c}", EOL_RUNE, EOL_RUNE))
customActionsHeader.WriteString(EOL)
customActionsHeader.WriteString(fmt.Sprintf(" }%s }%s}", EOL, EOL))

lines = append([]string{customActionsHeader.String()}, lines...)

Expand Down Expand Up @@ -240,7 +240,7 @@ func handleCustomActionRef(identifier *string) action {
constant: true,
}

advanceUntil(EOL_RUNE)
advanceUntil('\n')

return action{
ident: "runSelf",
Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ go 1.22
require github.com/google/uuid v1.6.0

require github.com/electrikmilk/args-parser v1.1.3

require (
sourcecode.social/reiver/go-eol v0.0.0-20240326064055-6080dd101592 // indirect
sourcecode.social/reiver/go-erorr v0.0.0-20230922202459-231149d185a1 // indirect
sourcecode.social/reiver/go-opt v0.0.0-20231106172254-6b4ca5231f41 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ github.com/electrikmilk/args-parser v1.1.3 h1:66xFYBDmsO3mZvIM1ThO/AV/3zTRwfE45A
github.com/electrikmilk/args-parser v1.1.3/go.mod h1:mb/l2JFNNBSA8GVYL0eQCJF6Jd7UtAJTpenLZs6iIJ0=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
sourcecode.social/reiver/go-eol v0.0.0-20240326064055-6080dd101592 h1:5lIRVxewoVEKA8AB/xLirgx16VVGAvYY15BWZzLQdh0=
sourcecode.social/reiver/go-eol v0.0.0-20240326064055-6080dd101592/go.mod h1:2OmaB7yZ5/S7rix/3mhFkLZ1HwkgCBt6NqdB4fFGQjI=
sourcecode.social/reiver/go-erorr v0.0.0-20230922202459-231149d185a1 h1:wpnz4JicQBLWrgGphYBls7DysIFCcnWgDz/vce/sY8E=
sourcecode.social/reiver/go-erorr v0.0.0-20230922202459-231149d185a1/go.mod h1:NFtd7fzEf0r6A6R7JXYZfayRhPaJy0zt/18VWoLzrxA=
sourcecode.social/reiver/go-opt v0.0.0-20231106172254-6b4ca5231f41 h1:nWUCtY3Krm+bLKXEEFvVz+vm256v3D/iJXgfoWhcOEw=
sourcecode.social/reiver/go-opt v0.0.0-20231106172254-6b4ca5231f41/go.mod h1:O6WKM2UcKkheRT/dA6A2b1tW0m+WenSbxdcXE+idxzI=
21 changes: 16 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ func handle(err error) {
return
}

fmt.Print(ansi(fmt.Sprintf("%cProgram panic :(%c%c", EOL_RUNE, EOL_RUNE, EOL_RUNE), red, bold))
fmt.Print(EOL)
fmt.Print(ansi("Program panic :(", red, bold))
fmt.Print(EOL)
fmt.Print(EOL)
fmt.Print(ansi("Please report this: https://github.com/electrikmilk/cherri/issues/new"+EOL+EOL, red))

if args.Using("debug") {
Expand Down Expand Up @@ -187,14 +190,21 @@ func panicDebug(err error) {
func printActionDefinitions() {
var identifier = args.Value("action")
if _, found := actions[identifier]; !found {
fmt.Println(ansi(fmt.Sprintf("%cAction %s() does not exist or has not yet been defined.", EOL_RUNE, identifier), red))
fmt.Print(EOL)
fmt.Println(ansi(fmt.Sprintf("Action %s() does not exist or has not yet been defined.", identifier), red))

switch identifier {
case "text":
fmt.Printf("%cText actions are abstracted into string statements. For example:%c%c@variable = \"Hello, Cherri!\"%c%c", EOL_RUNE, EOL_RUNE, EOL_RUNE, EOL_RUNE, EOL_RUNE)
fmt.Print(EOL)
fmt.Println("Text actions are abstracted into string statements. For example:")
fmt.Print(EOL)
fmt.Println("@variable = \"Hello, Cherri!\"")
os.Exit(1)
case "dictionary":
fmt.Printf("%cDictionary actions are abstracted into JSON object statements. For example:%c%c@variable = {\"test\":5\", \"key\":\"value\"}%c%c", EOL_RUNE, EOL_RUNE, EOL_RUNE, EOL_RUNE, EOL_RUNE)
fmt.Print(EOL)
fmt.Println("Dictionary actions are abstracted into JSON object statements. For example:")
fmt.Print(EOL)
fmt.Println("@variable = {\"test\":5\", \"key\":\"value\"}")
os.Exit(1)
}

Expand All @@ -215,7 +225,8 @@ func printActionDefinitions() {
case strings.Contains(actionIdentifier, lowercase):
identifier = strings.ReplaceAll(actionIdentifier, lowercase, ansi(lowercase, red))
}
actionSearchResults.WriteString(fmt.Sprintf("- %s%s%c", identifier, definition, EOL_RUNE))
actionSearchResults.WriteString(fmt.Sprintf("- %s%s", identifier, definition))
actionSearchResults.WriteString(EOL)
}
}
if actionSearchResults.Len() > 0 {
Expand Down
34 changes: 22 additions & 12 deletions output.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/electrikmilk/args-parser"
"os"
"os/exec"
"sourcecode.social/reiver/go-eol"
)

// writeFile writes plist in bytes to filename.
Expand Down Expand Up @@ -58,19 +59,24 @@ func openShortcut() {

func printChar(ch rune) {
var currentChar string
switch ch {
case ' ':
currentChar = "SPACE"
case '\t':
currentChar = "TAB"
case EOL_RUNE:

if eol.IsEOL(char) {
currentChar = "LF"
case -1:
currentChar = "EMPTY"
default:
currentChar = string(ch)
} else {
switch ch {
case ' ':
currentChar = "SPACE"
case '\t':
currentChar = "TAB"
case -1:
currentChar = "EMPTY"
default:
currentChar = string(ch)
}
}
fmt.Printf("%s %d:%d%c", currentChar, lineIdx+1, lineCharIdx, EOL_RUNE)

fmt.Printf("%s %d:%d", currentChar, lineIdx+1, lineCharIdx)
fmt.Print(EOL)
}

type outputType int
Expand Down Expand Up @@ -101,7 +107,11 @@ func ansi(message string, typeOf ...outputType) string {
}

func exit(message string) {
fmt.Printf("%c%s%c%c", EOL_RUNE, ansi("Error: "+message, red), EOL_RUNE, EOL_RUNE)
fmt.Print(EOL)
fmt.Printf(ansi("Error: "+message, red))
fmt.Print(EOL)
fmt.Print(EOL)

if args.Using("debug") {
panicDebug(nil)
} else {
Expand Down
Loading

0 comments on commit 82b0fee

Please sign in to comment.