-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from meysamhadeli/refactor/refactor-render-co…
…de-highlight-in-terminal refactor: refactor highlight code changes in terminal
- Loading branch information
Showing
6 changed files
with
55 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,34 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"github.com/alecthomas/chroma/v2/quick" | ||
"os" | ||
"strings" | ||
) | ||
|
||
var isCodeBlock = false | ||
|
||
// RenderAndPrintMarkdown handles the rendering of markdown content, | ||
func RenderAndPrintMarkdown(content string, language string, theme string) error { | ||
func RenderAndPrintMarkdown(line string, language string, theme string) error { | ||
|
||
err := quick.Highlight(os.Stdout, content, language, "terminal256", theme) | ||
if err != nil { | ||
return err | ||
if strings.HasPrefix(line, "```") { | ||
isCodeBlock = !isCodeBlock | ||
} | ||
// Process the line based on its prefix | ||
if strings.HasPrefix(line, "+") && isCodeBlock { | ||
line = "\x1b[92m" + line + "\x1b[0m" | ||
fmt.Print(line) | ||
} else if strings.HasPrefix(line, "-") && isCodeBlock { | ||
line = "\x1b[91m" + line + "\x1b[0m" | ||
fmt.Print(line) | ||
} else { | ||
// Render the processed line | ||
err := quick.Highlight(os.Stdout, line, language, "terminal256", theme) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |