Skip to content

Commit

Permalink
fix(gh): コードの表示位置がずれる問題を修正 (#25)
Browse files Browse the repository at this point in the history
Co-authored-by: かわえもん <me@kawaemon.dev>
  • Loading branch information
meru and kawaemon committed Aug 20, 2022
1 parent 24518ce commit 5793b96
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/bot/gh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,13 @@ impl CodePermalink {

let fragment = url.fragment()?; // a part after #
let captures = LINE_REGEX.captures(fragment)?;
let l1 = captures.name("l1").unwrap().as_str().parse().unwrap();
let l2 = captures.name("l2").map(|x| x.as_str().parse().unwrap());

let l1 = captures.name("l1").unwrap().as_str().parse().ok()?;

let l2 = match captures.name("l2") {
Some(l2) => Some(l2.as_str().parse().ok()?),
None => None,
};

Some(Self {
user: user.to_owned(),
Expand Down Expand Up @@ -246,17 +251,23 @@ impl CodePermalink {
.await
.context("failed to download rawcode")?;

const DEFAULT_RANGE: usize = 12;
const DEFAULT_SHOWN_LINES: usize = 12;
const OFFSET: usize = DEFAULT_SHOWN_LINES / 2;

let (l1, l2) = match self.l2 {
Some(l2) => (self.l1, l2),
None => (self.l1 - DEFAULT_RANGE / 2, self.l1 + DEFAULT_RANGE / 2),
None => (
self.l1.saturating_sub(OFFSET),
self.l1.saturating_add(OFFSET),
),
};

let skip = l1.saturating_sub(1);

Ok(code
.lines()
.skip(l1)
.take(l2 - l1)
.skip(skip)
.take(l2.saturating_sub(skip))
.collect::<Vec<&str>>()
.join("\n"))
}
Expand Down Expand Up @@ -310,13 +321,15 @@ mod test {
text,
r#"approvers/rusty-ponyo [02bb011de7d06e242a275dd9a9126a21effc6854] : Cargo.toml
```toml
[dependencies.serenity]
version = "0.10"
optional = true
default-features = false
features = ["rustls_backend", "client", "gateway", "model", "cache"]
```
approvers/rusty-ponyo [02bb011de7d06e242a275dd9a9126a21effc6854] : Cargo.toml
```toml
[dependencies.serenity]
version = "0.10"
optional = true
default-features = false
Expand Down

0 comments on commit 5793b96

Please sign in to comment.