Skip to content

Instantly share code, notes, and snippets.

@codekoriko
Last active December 28, 2023 05:23
Show Gist options
  • Save codekoriko/d6caacf14291918f9696a207ac9ae18c to your computer and use it in GitHub Desktop.
Save codekoriko/d6caacf14291918f9696a207ac9ae18c to your computer and use it in GitHub Desktop.
helper to provide context to ChatGPT requests
param (
[string[]]$Paths,
[string[]]$Exclude
)
# Check if at least one path is provided
if (-not $Paths) {
Write-Host "Please provide at least one file or directory path."
Exit
}
# Initialize the string to store the file information
$stringToCopy = "Given this list of files and their contents:`n`n"
function IsFileExcluded($fileName, $excludePatterns) {
foreach ($pattern in $excludePatterns) {
if ($fileName -like $pattern) {
return $true
}
}
return $false
}
function ProcessPath($path, $excludePatterns) {
$fileList = @()
if (Test-Path -Path $path -PathType Container) {
# Path is a directory
$items = Get-ChildItem -Path $path -Recurse -File -ErrorAction SilentlyContinue
foreach ($item in $items) {
if ($excludePatterns -and (IsFileExcluded $item.Name $excludePatterns)) {
continue
}
$fileList += $item.FullName
}
}
elseif (Test-Path -Path $path -PathType Leaf) {
# Path is a file
if ($excludePatterns -and (IsFileExcluded (Split-Path $path -Leaf) $excludePatterns)) {
return $fileList
}
$fileList += $path
}
else {
Write-Host "Invalid path: $path"
}
return $fileList
}
function ProcessFileList($fileList) {
$numFile = 1 # Initialize the file counter
foreach ($filePath in $fileList) {
$content = Get-Content -Path $filePath -Raw
$script:stringToCopy += "Path file${numFile}: '${filePath}'`nContent file${numFile}:`n{$content}`n`n"
$numFile++ # Increment the file counter
}
}
# Collect files from all paths
$filePaths = @()
$Exclude = $Exclude -split ','
foreach ($path in $Paths) {
$filePaths += ProcessPath $path $Exclude
}
# Process collected file paths
ProcessFileList $filePaths
$TokenCount = [Regex]::Matches($stringToCopy, '\w+|[.,!?;]').Count
# Copy the information to the clipboard
$stringToCopy | Set-Clipboard
# Print a message
Write-Host "Tokens Copied to the clipboard: $TokenCount"
# Initialize an empty variable to hold the final text
clipboardContent="I am seeking assistance with a Vue3 application. Below is the TypeScript code split of one or more file(s) with their abolute path(s) enclosed in triple quotes for clarity:\n\n"
# Loop through each argument provided to the script
for filePath in "$@"
do
# Check if the file exists
if [[ -f "$filePath" ]]; then
# Read the content of the file
fileContent=$(cat "$filePath")
# Append the formatted content to the clipboard content variable
clipboardContent+="I have the file \"$filePath\" that contains:\n\"\"\"$fileContent\"\"\"\n\n"
else
echo "File not found: $filePath"
fi
done
# Copy the final content to the Windows clipboard
echo -e "$clipboardContent" | clip.exe
# Count the number of characters
numChars=$(echo -n "$clipboardContent" | wc -c)
# Assuming 4 characters per token as a rough estimate
numTokens=$((numChars / 4))
# Output the number of tokens copied
echo "${numTokens}/8192 tokens copied to clipboard"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment