Skip to content

Commit

Permalink
Automatically wrap the body
Browse files Browse the repository at this point in the history
Fixes #52
  • Loading branch information
sonnyp committed Mar 17, 2022
1 parent 17c76ce commit bd733c6
Show file tree
Hide file tree
Showing 16 changed files with 212 additions and 85 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ install/
flatpak/
node_modules/
repo/
src/wordwrap.js
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ install/
flatpak/
node_modules/
repo/
src/wordwrap.js
4 changes: 4 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@
14. [ ] detect non imperative mood

https://tekin.co.uk/2020/03/git-commit-verbose-mode
https://robertcooper.me/post/git-commit-messages
https://www.freecodecamp.org/news/how-to-write-better-git-commit-messages/
https://wiki.gnome.org/Git/CommitMessages
https://cbea.ms/git-commit/
9 changes: 8 additions & 1 deletion data/re.sonny.Commit.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
<schema id="re.sonny.Commit" path="/re/sonny/Commit/">
<key name='title-length-hint' type='i'>
<default>50</default>
<range min="1" />
<range min="50" max="200" />
<summary>Message title length hint</summary>
</key>

<key name='body-length-wrap' type='i'>
<default>75</default>
<range min="72" max="200" />
<summary>Message body length wrap</summary>
</key>

<key name='dark-mode' type='b'>
<default>false</default>
<summary>Use dark mode</summary>
Expand Down
3 changes: 2 additions & 1 deletion data/re.sonny.Commit.metainfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@
<li>Add keyboard shortcuts for moving lines up and down</li>
<li>Highlight syntax for Git, Mercurial and diffs</li>
<li>Add a shortcuts window</li>
<li>Support for multiple comments</li>
<li>Support multiple comments</li>
<li>Auto capitalize the commit title</li>
<li>Add a setting to automatically wrap commit message body</li>
<li>Various fixes</li>
</ul>
</description>
Expand Down
2 changes: 1 addition & 1 deletion re.sonny.Commit.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"sources": [
{
"type": "dir",
"path": "./"
"path": "."
}
]
}
Expand Down
6 changes: 3 additions & 3 deletions src/Editor.js → src/CommitEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Adw from "gi://Adw";

import { relativePath } from "./util.js";

const file = Gio.File.new_for_path(relativePath("./Editor.ui"));
const file = Gio.File.new_for_path(relativePath("./CommitEditor.ui"));
const [, template] = file.load_contents(null);

const scheme_manager = GtkSource.StyleSchemeManager.get_default();
Expand All @@ -19,7 +19,7 @@ language_manager.set_search_path([

export default GObject.registerClass(
{
GTypeName: "Editor",
GTypeName: "CommitEditor",
Properties: {
language: GObject.ParamSpec.string(
"language",
Expand All @@ -35,7 +35,7 @@ export default GObject.registerClass(
"style-updated": {},
},
},
class Editor extends Gtk.ScrolledWindow {
class CommitEditor extends Gtk.ScrolledWindow {
_init(params = {}) {
super._init(params);

Expand Down
24 changes: 24 additions & 0 deletions src/CommitEditor.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" ?>
<interface>
<template class="CommitEditor" parent="GtkScrolledWindow">
<property name="vexpand">true</property>
<child>
<object class="GtkSourceView" id="view">
<property name="buffer">
<object class="GtkSourceBuffer" id="buffer" />
</property>
<property name="monospace">true</property>
<!-- <property name="show-right-margin">true</property> -->
<property name="wrap_mode">word-char</property>
<property name="top-margin">6</property>
<property name="left-margin">6</property>
<property name="right-margin">6</property>
<property name="bottom-margin">6</property>
<property name="input_hints">
GTK_INPUT_HINT_SPELLCHECK | GTK_INPUT_HINT_WORD_COMPLETION |
GTK_INPUT_HINT_EMOJI | GTK_INPUT_HINT_UPPERCASE_SENTENCES
</property>
</object>
</child>
</template>
</interface>
41 changes: 0 additions & 41 deletions src/Editor.ui

This file was deleted.

2 changes: 1 addition & 1 deletion src/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function openEditor({ file, application, readonly }) {
try {
[, commitMessage] = GLib.file_get_contents(filePath);
} catch (err) {
printerr(err);
logError(err);
application.quit();
return;
}
Expand Down
18 changes: 4 additions & 14 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,20 @@ import Gtk from "gi://Gtk";
import GLib from "gi://GLib";
import GtkSource from "gi://GtkSource";

import Editor from "./Editor.js";
import CommitEditor from "./CommitEditor.js";

import { settings } from "./util.js";
import { parse, hasCommitMessage } from "./scm.js";
import { hasCommitMessage } from "./scm.js";

const HIGHLIGHT_BACKGROUND_TAG_NAME = "highlightBackground";

export default function editor({
builder,
commitButton,
type,
commitMessage,
window,
parsed,
}) {
let parsed = {};

try {
parsed = parse(commitMessage, type);
} catch (err) {
if (__DEV__) {
logError(err);
}
}

const {
body,
comment,
Expand All @@ -51,7 +41,7 @@ export default function editor({
let previousNumberOfLinesInCommitMessage = 1;

const main = builder.get_object("main");
const widget = new Editor({ language });
const widget = new CommitEditor({ language });
main.append(widget);
const source_view = widget.view;

Expand Down
7 changes: 7 additions & 0 deletions src/scm.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ export function parse(commit, type) {
read_only_index,
language,
capitalize,
wrap: [
"hg",
"commit",
"git-merge-squash",
"git-rebase-squash",
"merge",
].includes(type),
};
}

Expand Down
19 changes: 15 additions & 4 deletions src/welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,26 @@ export default function Welcome({ application }) {

loadStyleSheet(relativePath("./style.css"));

const spinButton = builder.get_object("spinButton");
spinButton.set_range(50, 200);
spinButton.set_increments(1, 10);
const button_hint = builder.get_object("button_hint");
button_hint.set_range(50, 200);
button_hint.set_increments(1, 10);
settings.bind(
"title-length-hint",
spinButton,
button_hint,
"value",
Gio.SettingsBindFlags.DEFAULT,
);

const button_wrap = builder.get_object("button_wrap");
button_wrap.set_range(72, 200);
button_wrap.set_increments(1, 10);
settings.bind(
"body-length-wrap",
button_wrap,
"value",
Gio.SettingsBindFlags.DEFAULT,
);

const darkSwitch = builder.get_object("darkSwitch");
Adw.StyleManager.get_default().bind_property(
"dark",
Expand Down
25 changes: 21 additions & 4 deletions src/welcome.ui
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,28 @@
<property
name="title"
translatable="yes"
>Message title length hint</property>
>Commit title max length</property>
<child>
<object class="GtkSpinButton" id="spinButton">
<property name="value">50</property>
<property name='numeric'>True</property>
<object class="GtkSpinButton" id="button_hint">
<property name='numeric'>true</property>
<property name='valign'>center</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="AdwPreferencesRow">
<child>
<object class="AdwActionRow">
<property
name="title"
translatable="yes"
>Message body max length</property>
<child>
<object class="GtkSpinButton" id="button_wrap">
<property name='numeric'>true</property>
<property name='valign'>center</property>
</object>
</child>
Expand Down
46 changes: 31 additions & 15 deletions src/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Gio from "gi://Gio";
import Editor from "./editor.js";

import { relativePath } from "./util.js";
import { parse } from "./scm.js";
import wordwrap from "./wordwrap.js";

export default function Window({
application,
Expand All @@ -13,20 +15,36 @@ export default function Window({
type,
readonly,
}) {
const builder = Gtk.Builder.new_from_file(relativePath("./window.ui"));
let parsed = {};
try {
parsed = parse(commitMessage, type);
} catch (err) {
if (__DEV__) {
logError(err);
}
}

const builder = Gtk.Builder.new_from_file(relativePath("./window.ui"));
const window = builder.get_object("window");
const cancelButton = builder.get_object("cancelButton");
const commitButton = builder.get_object("commitButton");

const { buffer, source_view } = Editor({
builder,
commitButton,
type,
window,
parsed,
});

window.set_application(application);

const cancelAction = new Gio.SimpleAction({
name: "cancel",
parameter_type: null,
});
cancelAction.connect("activate", () => {
save({ file, application, value: "", readonly });
save({ file, window, value: "", readonly });
});
window.add_action(cancelAction);

Expand All @@ -35,33 +53,31 @@ export default function Window({
parameter_type: null,
});
commitAction.connect("activate", () => {
const value = buffer.text;
save({ file, application, value, readonly });
const { text } = buffer;
const value = parsed.wrap ? wordwrap(0, 75, { mode: "hard" })(text) : text;
save({
file,
window,
value,
readonly,
});
});
window.add_action(commitAction);

const { buffer, source_view } = Editor({
builder,
commitButton,
type,
commitMessage,
window,
});

// https://github.com/sonnyp/Commit/issues/33
window.set_focus(source_view);

return { window, cancelButton, commitButton, buffer };
}

function save({ file, value, application, readonly }) {
function save({ file, value, window, readonly }) {
if (!readonly) {
try {
GLib.file_set_contents(file.get_path(), value);
} catch (err) {
printerr(err);
logError(err);
}
}

application.quit();
window.close();
}
Loading

0 comments on commit bd733c6

Please sign in to comment.